From b0606dbe2351ff5b354a733e06e0cd9cee1b53ae Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 14 Feb 2012 16:35:36 +0100 Subject: No more autobox/type coercion issues with @Getter(lazy=true) and primitive types. (Issue #345). --- src/core/lombok/eclipse/handlers/HandleGetter.java | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/core/lombok/eclipse/handlers') diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index b7d9c5ed..92f1177f 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-2012 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -287,6 +287,7 @@ public class HandleGetter extends EclipseAnnotationHandler { } private static char[] valueName = "value".toCharArray(); + private static char[] actualValueName = "actualValue".toCharArray(); private Statement[] createLazyGetterBody(ASTNode source, EclipseNode fieldNode) { /* @@ -294,8 +295,9 @@ public class HandleGetter extends EclipseAnnotationHandler { if (value == null) { synchronized (this.fieldName) { value = this.fieldName.get(); - if (value == null) { - value = new java.util.concurrent.atomic.AtomicReference(new ValueType()); + if (value == null) { + final ValueType actualValue = new ValueType(); + value = new java.util.concurrent.atomic.AtomicReference(actualValue); this.fieldName.set(value); } } @@ -343,7 +345,8 @@ public class HandleGetter extends EclipseAnnotationHandler { synchronized (this.fieldName) { value = this.fieldName.get(); if (value == null) { - value = new java.util.concurrent.atomic.AtomicReference(new ValueType()); + final ValueType actualValue = new ValueType(); + value = new java.util.concurrent.atomic.AtomicReference(actualValue); this.fieldName.set(value); } } @@ -382,8 +385,18 @@ public class HandleGetter extends EclipseAnnotationHandler { setGeneratedBy(innerCond, source); Block innerThen = new Block(0); setGeneratedBy(innerThen, source); - innerThen.statements = new Statement[2]; - /*value = new java.util.concurrent.atomic.AtomicReference(new ValueType()); */ { + innerThen.statements = new Statement[3]; + /* final ValueType actualValue = new ValueType(); */ { + LocalDeclaration actualValueDecl = new LocalDeclaration(actualValueName, pS, pE); + setGeneratedBy(actualValueDecl, source); + actualValueDecl.type = copyType(field.type, source); + actualValueDecl.type.sourceStart = pS; actualValueDecl.type.sourceEnd = actualValueDecl.type.statementEnd = pE; + setGeneratedBy(actualValueDecl.type, source); + actualValueDecl.initialization = field.initialization; + actualValueDecl.modifiers = ClassFileConstants.AccFinal; + innerThen.statements[0] = actualValueDecl; + } + /* value = new java.util.concurrent.atomic.AtomicReference(actualValue); */ { AllocationExpression create = new AllocationExpression(); setGeneratedBy(create, source); create.sourceStart = pS; create.sourceEnd = create.statementEnd = pE; @@ -392,16 +405,17 @@ public class HandleGetter extends EclipseAnnotationHandler { create.type = new ParameterizedQualifiedTypeReference(AR, typeParams, 0, poss(source, 5)); create.type.sourceStart = pS; create.type.sourceEnd = create.type.statementEnd = pE; setGeneratedBy(create.type, source); - create.arguments = new Expression[] {field.initialization}; + create.arguments = new Expression[] {new SingleNameReference(actualValueName, p)}; + setGeneratedBy(create.arguments[0], source); Assignment innerAssign = new Assignment(new SingleNameReference(valueName, p), create, pE); innerAssign.sourceStart = pS; innerAssign.statementEnd = innerAssign.sourceEnd = pE; setGeneratedBy(innerAssign, source); setGeneratedBy(innerAssign.lhs, source); - innerThen.statements[0] = innerAssign; + innerThen.statements[1] = innerAssign; } - /*this.fieldName.set(value);*/ { + /* this.fieldName.set(value); */ { MessageSend setter = new MessageSend(); setGeneratedBy(setter, source); setter.sourceStart = pS; setter.sourceEnd = setter.statementEnd = pE; @@ -410,7 +424,7 @@ public class HandleGetter extends EclipseAnnotationHandler { setter.arguments = new Expression[] { new SingleNameReference(valueName, p)}; setGeneratedBy(setter.arguments[0], source); - innerThen.statements[1] = setter; + innerThen.statements[2] = setter; } IfStatement innerIf = new IfStatement(innerCond, innerThen, pS, pE); -- cgit