From 87cca2d322be09c51d1e63eea195addd1e99a7e2 Mon Sep 17 00:00:00 2001 From: peichhorn Date: Mon, 5 Dec 2011 22:42:50 +0100 Subject: [Issue 275] Allow @Delegate on no-argument methods --- .../lombok/eclipse/agent/PatchDelegate.java | 184 +++++++++++++++------ 1 file changed, 134 insertions(+), 50 deletions(-) (limited to 'src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index affcd4f2..d6310de1 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -103,25 +103,25 @@ public class PatchDelegate { return new String(decl.name); } - private static boolean hasDelegateMarkedFields(TypeDeclaration decl) { + private static boolean hasDelegateMarkedFieldsOrMethods(TypeDeclaration decl) { if (decl.fields != null) for (FieldDeclaration field : decl.fields) { if (field.annotations == null) continue; for (Annotation ann : field.annotations) { - if (ann.type == null) continue; - TypeBinding tb = ann.type.resolveType(decl.initializerScope); - if (tb == null) continue; - if (!charArrayEquals("lombok", tb.qualifiedPackageName())) continue; - if (!charArrayEquals("Delegate", tb.qualifiedSourceName())) continue; - return true; + if (isDelegate(ann, decl)) return true; + } + } + if (decl.methods != null) for (AbstractMethodDeclaration method : decl.methods) { + if (method.annotations == null) continue; + for (Annotation ann : method.annotations) { + if (isDelegate(ann, decl)) return true; } } - return false; } public static boolean handleDelegateForType(ClassScope scope) { if (TransformEclipseAST.disableLombok) return false; - if (!hasDelegateMarkedFields(scope.referenceContext)) return false; + if (!hasDelegateMarkedFieldsOrMethods(scope.referenceContext)) return false; List stack = visited.get(); StringBuilder corrupted = null; @@ -145,16 +145,23 @@ public class PatchDelegate { stack.add(entry); try { - List methodsToDelegate = new ArrayList(); TypeDeclaration decl = scope.referenceContext; if (decl != null) { CompilationUnitDeclaration cud = scope.compilationUnitScope().referenceContext; EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); - fillMethodBindings(cud, scope, methodsToDelegate); + List methodsToDelegate = new ArrayList(); + fillMethodBindingsForFields(cud, scope, methodsToDelegate); + if (entry.corruptedPath != null) { + eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath); + } else { + generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.FIELD); + } + methodsToDelegate.clear(); + fillMethodBindingsForMethods(cud, scope, methodsToDelegate); if (entry.corruptedPath != null) { eclipseAst.get(scope.referenceContext).addError("No @Delegate methods created because there's a loop: " + entry.corruptedPath); } else { - generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate); + generateDelegateMethods(eclipseAst.get(decl), methodsToDelegate, DelegateReceiver.METHOD); } } } finally { @@ -181,43 +188,18 @@ public class PatchDelegate { private static Map alreadyApplied = new WeakHashMap(); private static final Object MARKER = new Object(); - private static void fillMethodBindings(CompilationUnitDeclaration cud, ClassScope scope, List methodsToDelegate) { + private static void fillMethodBindingsForFields(CompilationUnitDeclaration cud, ClassScope scope, List methodsToDelegate) { TypeDeclaration decl = scope.referenceContext; if (decl == null) return; if (decl.fields != null) for (FieldDeclaration field : decl.fields) { if (field.annotations == null) continue; for (Annotation ann : field.annotations) { - if (ann.type == null) continue; - TypeBinding tb = ann.type.resolveType(decl.initializerScope); - if (!charArrayEquals("lombok", tb.qualifiedPackageName())) continue; - if (!charArrayEquals("Delegate", tb.qualifiedSourceName())) continue; + if (!isDelegate(ann, decl)) continue; if (alreadyApplied.put(ann, MARKER) == MARKER) continue; - List rawTypes = new ArrayList(); - List excludedRawTypes = new ArrayList(); - for (MemberValuePair pair : ann.memberValuePairs()) { - if (charArrayEquals("types", pair.name)) { - if (pair.value instanceof ArrayInitializer) { - for (Expression expr : ((ArrayInitializer)pair.value).expressions) { - if (expr instanceof ClassLiteralAccess) rawTypes.add((ClassLiteralAccess) expr); - } - } - if (pair.value instanceof ClassLiteralAccess) { - rawTypes.add((ClassLiteralAccess) pair.value); - } - } - if (charArrayEquals("excludes", pair.name)) { - if (pair.value instanceof ArrayInitializer) { - for (Expression expr : ((ArrayInitializer)pair.value).expressions) { - if (expr instanceof ClassLiteralAccess) excludedRawTypes.add((ClassLiteralAccess) expr); - } - } - if (pair.value instanceof ClassLiteralAccess) { - excludedRawTypes.add((ClassLiteralAccess) pair.value); - } - } - } + List rawTypes = rawTypes(ann, "types"); + List excludedRawTypes = rawTypes(ann, "excludes"); List methodsToExclude = new ArrayList(); for (ClassLiteralAccess cla : excludedRawTypes) { @@ -251,6 +233,88 @@ public class PatchDelegate { } } + private static void fillMethodBindingsForMethods(CompilationUnitDeclaration cud, ClassScope scope, List methodsToDelegate) { + TypeDeclaration decl = scope.referenceContext; + if (decl == null) return; + + if (decl.methods != null) for (AbstractMethodDeclaration methodDecl : decl.methods) { + if (methodDecl.annotations == null) continue; + for (Annotation ann : methodDecl.annotations) { + if (!isDelegate(ann, decl)) continue; + if (alreadyApplied.put(ann, MARKER) == MARKER) continue; + if (!(methodDecl instanceof MethodDeclaration)) { + EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); + eclipseAst.get(ann).addError("@Delegate is legal only on no-argument methods."); + continue; + } + if (methodDecl.arguments != null) { + EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); + eclipseAst.get(ann).addError("@Delegate is legal only on no-argument methods."); + continue; + } + MethodDeclaration method = (MethodDeclaration) methodDecl; + + List rawTypes = rawTypes(ann, "types"); + List excludedRawTypes = rawTypes(ann, "excludes"); + + List methodsToExclude = new ArrayList(); + for (ClassLiteralAccess cla : excludedRawTypes) { + addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet(), method.selector, ann); + } + + Set banList = new HashSet(); + for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized)); + + List methodsToDelegateForThisAnn = new ArrayList(); + + if (rawTypes.isEmpty()) { + addAllMethodBindings(methodsToDelegateForThisAnn, method.returnType.resolveType(decl.initializerScope), banList, method.selector, ann); + } else { + for (ClassLiteralAccess cla : rawTypes) { + addAllMethodBindings(methodsToDelegateForThisAnn, cla.type.resolveType(decl.initializerScope), banList, method.selector, ann); + } + } + + // Not doing this right now because of problems - see commented-out-method for info. + // removeExistingMethods(methodsToDelegate, decl, scope); + + String dupe = containsDuplicates(methodsToDelegateForThisAnn); + if (dupe != null) { + EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); + eclipseAst.get(ann).addError("The method '" + dupe + "' is being delegated by more than one specified type."); + } else { + methodsToDelegate.addAll(methodsToDelegateForThisAnn); + } + } + } + } + + private static boolean isDelegate(Annotation ann, TypeDeclaration decl) { + if (ann.type == null) return false; + TypeBinding tb = ann.type.resolveType(decl.initializerScope); + if (tb == null) return false; + if (!charArrayEquals("lombok", tb.qualifiedPackageName())) return false; + if (!charArrayEquals("Delegate", tb.qualifiedSourceName())) return false; + return true; + } + + private static List rawTypes(Annotation ann, String name) { + List rawTypes = new ArrayList(); + for (MemberValuePair pair : ann.memberValuePairs()) { + if (charArrayEquals(name, pair.name)) { + if (pair.value instanceof ArrayInitializer) { + for (Expression expr : ((ArrayInitializer)pair.value).expressions) { + if (expr instanceof ClassLiteralAccess) rawTypes.add((ClassLiteralAccess) expr); + } + } + if (pair.value instanceof ClassLiteralAccess) { + rawTypes.add((ClassLiteralAccess) pair.value); + } + } + } + return rawTypes; + } + /* * We may someday finish this method. Steps to be completed: * @@ -321,11 +385,11 @@ public class PatchDelegate { // } // } - private static void generateDelegateMethods(EclipseNode typeNode, List methods) { + private static void generateDelegateMethods(EclipseNode typeNode, List methods, DelegateReceiver delegateReceiver) { CompilationUnitDeclaration top = (CompilationUnitDeclaration) typeNode.top().get(); for (BindingTuple pair : methods) { EclipseNode annNode = typeNode.getAst().get(pair.responsible); - MethodDeclaration method = createDelegateMethod(pair.fieldName, typeNode, pair, top.compilationResult, annNode); + MethodDeclaration method = createDelegateMethod(pair.fieldName, typeNode, pair, top.compilationResult, annNode, delegateReceiver); if (method != null) { SetGeneratedByVisitor visitor = new SetGeneratedByVisitor(annNode.get()); method.traverse(visitor, ((TypeDeclaration)typeNode.get()).scope); @@ -473,7 +537,7 @@ public class PatchDelegate { } } - private static MethodDeclaration createDelegateMethod(char[] name, EclipseNode typeNode, BindingTuple pair, CompilationResult compilationResult, EclipseNode annNode) { + private static MethodDeclaration createDelegateMethod(char[] name, EclipseNode typeNode, BindingTuple pair, CompilationResult compilationResult, EclipseNode annNode, DelegateReceiver delegateReceiver) { /* public ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... { * (return) delegate.methodName(name1, name2); * } @@ -514,11 +578,7 @@ public class PatchDelegate { call.sourceStart = pS; call.sourceEnd = pE; call.nameSourcePosition = pos(source); setGeneratedBy(call, source); - FieldReference fieldRef = new FieldReference(name, pos(source)); - fieldRef.receiver = new ThisReference(pS, pE); - setGeneratedBy(fieldRef, source); - setGeneratedBy(fieldRef.receiver, source); - call.receiver = fieldRef; + call.receiver = delegateReceiver.get(source, name); call.selector = binding.selector; if (binding.typeVariables != null && binding.typeVariables.length > 0) { @@ -742,7 +802,31 @@ public class PatchDelegate { if (s.length() != c.length) return false; for (int i = 0; i < s.length(); i++) if (s.charAt(i) != c[i]) return false; return true; + } + + private enum DelegateReceiver { + METHOD { + public Expression get(final ASTNode source, char[] name) { + MessageSend call = new MessageSend(); + call.sourceStart = source.sourceStart; call.sourceEnd = source.sourceEnd; + call.nameSourcePosition = pos(source); + setGeneratedBy(call, source); + call.selector = name; + call.receiver = new ThisReference(source.sourceStart, source.sourceEnd); + setGeneratedBy(call.receiver, source); + return call; + } + }, + FIELD { + public Expression get(final ASTNode source, char[] name) { + FieldReference fieldRef = new FieldReference(name, pos(source)); + setGeneratedBy(fieldRef, source); + fieldRef.receiver = new ThisReference(source.sourceStart, source.sourceEnd); + setGeneratedBy(fieldRef.receiver, source); + return fieldRef; + } + }; - + public abstract Expression get(final ASTNode source, char[] name); } } -- cgit From acea9efcff67c7a0bdb85175b84e75a9dad172b8 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 19 Jan 2012 00:33:18 +0100 Subject: @Delegate is no longer legal on static entities (previously no immediate errors but I don't think it would work right anyway), and prettied up the error message when you use @Delegate wrong (on static items or methods with args). Also put back something I accidentally deleted with the previous merge. --- .../lombok/eclipse/agent/PatchDelegate.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index d6310de1..e510d37b 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -198,6 +198,12 @@ public class PatchDelegate { if (!isDelegate(ann, decl)) continue; if (alreadyApplied.put(ann, MARKER) == MARKER) continue; + if ((field.modifiers & ClassFileConstants.AccStatic) != 0) { + EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); + eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE); + break; + } + List rawTypes = rawTypes(ann, "types"); List excludedRawTypes = rawTypes(ann, "excludes"); @@ -233,6 +239,8 @@ public class PatchDelegate { } } + private static final String LEGALITY_OF_DELEGATE = "@Delegate is legal only on instance fields or no-argument instance methods."; + private static void fillMethodBindingsForMethods(CompilationUnitDeclaration cud, ClassScope scope, List methodsToDelegate) { TypeDeclaration decl = scope.referenceContext; if (decl == null) return; @@ -244,13 +252,18 @@ public class PatchDelegate { if (alreadyApplied.put(ann, MARKER) == MARKER) continue; if (!(methodDecl instanceof MethodDeclaration)) { EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); - eclipseAst.get(ann).addError("@Delegate is legal only on no-argument methods."); - continue; + eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE); + break; } if (methodDecl.arguments != null) { EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); - eclipseAst.get(ann).addError("@Delegate is legal only on no-argument methods."); - continue; + eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE); + break; + } + if ((methodDecl.modifiers & ClassFileConstants.AccStatic) != 0) { + EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true); + eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE); + break; } MethodDeclaration method = (MethodDeclaration) methodDecl; -- cgit From 3e7bb145d3984d7472baa177ead3baf574d42e6b Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 23 Jan 2012 23:10:56 +0100 Subject: Added null-check to prevent errors in eclipse when working on incomplete files --- src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index e510d37b..31ec52f4 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -281,6 +281,7 @@ public class PatchDelegate { List methodsToDelegateForThisAnn = new ArrayList(); if (rawTypes.isEmpty()) { + if (method.returnType == null) continue; addAllMethodBindings(methodsToDelegateForThisAnn, method.returnType.resolveType(decl.initializerScope), banList, method.selector, ann); } else { for (ClassLiteralAccess cla : rawTypes) { -- cgit From 2242de2bf9886fc80858486bbb3aa171a5496885 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Tue, 24 Jan 2012 02:52:13 +0100 Subject: Fix for issue 328: @Delegate on a field for which we also generate a getter will use the getter for delegation --- src/core/lombok/eclipse/handlers/HandleGetter.java | 23 +++++++++++-- src/core/lombok/javac/handlers/HandleGetter.java | 27 +++++++++++++++ .../lombok/eclipse/agent/PatchDelegate.java | 4 +++ src/utils/lombok/eclipse/Eclipse.java | 5 +-- .../resource/after-delombok/DelegateOnGetter.java | 35 +++++++++++++++++++ .../resource/after-ecj/DelegateOnGetter.java | 1 + .../resource/after-eclipse/DelegateOnGetter.java | 40 ++++++++++++++++++++++ .../resource/before/DelegateOnGetter.java | 18 ++++++++++ 8 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 test/transform/resource/after-delombok/DelegateOnGetter.java create mode 100644 test/transform/resource/after-ecj/DelegateOnGetter.java create mode 100644 test/transform/resource/after-eclipse/DelegateOnGetter.java create mode 100644 test/transform/resource/before/DelegateOnGetter.java (limited to 'src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java') diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 1d59afb4..b7d9c5ed 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -24,18 +24,23 @@ package lombok.eclipse.handlers; import static lombok.eclipse.Eclipse.*; import static lombok.eclipse.handlers.EclipseHandlerUtil.*; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import lombok.AccessLevel; +import lombok.Delegate; import lombok.Getter; +import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; -import lombok.core.AST.Kind; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; +import lombok.eclipse.agent.PatchDelegate; +import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; @@ -69,6 +74,8 @@ import org.mangosdk.spi.ProviderFor; */ @ProviderFor(EclipseAnnotationHandler.class) public class HandleGetter extends EclipseAnnotationHandler { + private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0]; + public boolean generateGetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelGetter) { if (checkForTypeLevelGetter) { if (typeNode != null) for (EclipseNode child : typeNode.down()) { @@ -205,13 +212,25 @@ public class HandleGetter extends EclipseAnnotationHandler { } MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), fieldNode, getterName, modifier, source, lazy); - Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN)); + Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode)); if (copiedAnnotations.length != 0) { method.annotations = copiedAnnotations; } injectMethod(fieldNode.up(), method); } + + private static Annotation[] findDelegatesAndMarkAsHandled(EclipseNode fieldNode) { + List delegates = new ArrayList(); + for (EclipseNode child : fieldNode.down()) { + if (annotationTypeMatches(Delegate.class, child)) { + Annotation delegate = (Annotation)child.get(); + PatchDelegate.markHandled(delegate); + delegates.add(delegate); + } + } + return delegates.toArray(EMPTY_ANNOTATIONS_ARRAY); + } private MethodDeclaration generateGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy) { diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index fe3b86a4..c9d67d7f 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.Map; import lombok.AccessLevel; +import lombok.Delegate; import lombok.Getter; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; @@ -240,16 +241,42 @@ public class HandleGetter extends JavacAnnotationHandler { List nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); List nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + List delegates = findDelegatesAndRemoveFromField(field); + List annsOnMethod = nonNulls.appendList(nullables); JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); if (toClearOfMarkers != null) recursiveSetGeneratedBy(toClearOfMarkers, null); + decl.mods.annotations = decl.mods.annotations.appendList(delegates); return decl; } + private static List findDelegatesAndRemoveFromField(JavacNode field) { + JCVariableDecl fieldNode = (JCVariableDecl) field.get(); + + List delegates = List.nil(); + for (JCAnnotation annotation : fieldNode.mods.annotations) { + if (typeMatches(Delegate.class, field, annotation.annotationType)) { + delegates = delegates.append(annotation); + } + } + + if (!delegates.isEmpty()) { + ListBuffer withoutDelegates = ListBuffer.lb(); + for (JCAnnotation annotation : fieldNode.mods.annotations) { + if (!delegates.contains(annotation)) { + withoutDelegates.append(annotation); + } + } + fieldNode.mods.annotations = withoutDelegates.toList(); + field.rebuild(); + } + return delegates; + } + private List createSimpleGetterBody(TreeMaker treeMaker, JavacNode field) { return List.of(treeMaker.Return(createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD))); } diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index 31ec52f4..87335b4e 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -188,6 +188,10 @@ public class PatchDelegate { private static Map alreadyApplied = new WeakHashMap(); private static final Object MARKER = new Object(); + public static void markHandled(Annotation annotation) { + alreadyApplied.put(annotation, MARKER); + } + private static void fillMethodBindingsForFields(CompilationUnitDeclaration cud, ClassScope scope, List methodsToDelegate) { TypeDeclaration decl = scope.referenceContext; if (decl == null) return; diff --git a/src/utils/lombok/eclipse/Eclipse.java b/src/utils/lombok/eclipse/Eclipse.java index fce0773b..7d21faa2 100644 --- a/src/utils/lombok/eclipse/Eclipse.java +++ b/src/utils/lombok/eclipse/Eclipse.java @@ -43,6 +43,7 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; public class Eclipse { + private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0]; /** * Eclipse's Parser class is instrumented to not attempt to fill in the body of any method or initializer * or field initialization if this flag is set. Set it on the flag field of @@ -120,7 +121,7 @@ public class Eclipse { */ public static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) { List result = new ArrayList(); - if (field.annotations == null) return new Annotation[0]; + if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; for (Annotation annotation : field.annotations) { TypeReference typeRef = annotation.type; if (typeRef != null && typeRef.getTypeName()!= null) { @@ -131,7 +132,7 @@ public class Eclipse { } } } - return result.toArray(new Annotation[0]); + return result.toArray(EMPTY_ANNOTATIONS_ARRAY); } /** diff --git a/test/transform/resource/after-delombok/DelegateOnGetter.java b/test/transform/resource/after-delombok/DelegateOnGetter.java new file mode 100644 index 00000000..ee34018a --- /dev/null +++ b/test/transform/resource/after-delombok/DelegateOnGetter.java @@ -0,0 +1,35 @@ +class DelegateOnGetter { + private final java.util.concurrent.atomic.AtomicReference> bar = new java.util.concurrent.atomic.AtomicReference>(); + private interface Bar { + void setList(java.util.ArrayList list); + int getInt(); + } + @java.lang.SuppressWarnings("all") + public Bar getBar() { + java.util.concurrent.atomic.AtomicReference value = this.bar.get(); + if (value == null) { + synchronized (this.bar) { + value = this.bar.get(); + if (value == null) { + value = new java.util.concurrent.atomic.AtomicReference(new Bar(){ + public void setList(java.util.ArrayList list) { + } + public int getInt() { + return 42; + } + }); + this.bar.set(value); + } + } + } + return value.get(); + } + @java.lang.SuppressWarnings("all") + public void setList(final java.util.ArrayList list) { + this.getBar().setList(list); + } + @java.lang.SuppressWarnings("all") + public int getInt() { + return this.getBar().getInt(); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/DelegateOnGetter.java b/test/transform/resource/after-ecj/DelegateOnGetter.java new file mode 100644 index 00000000..cb06d3c1 --- /dev/null +++ b/test/transform/resource/after-ecj/DelegateOnGetter.java @@ -0,0 +1 @@ +//ignore \ No newline at end of file diff --git a/test/transform/resource/after-eclipse/DelegateOnGetter.java b/test/transform/resource/after-eclipse/DelegateOnGetter.java new file mode 100644 index 00000000..7d5907e0 --- /dev/null +++ b/test/transform/resource/after-eclipse/DelegateOnGetter.java @@ -0,0 +1,40 @@ +import lombok.Delegate; +import lombok.Getter; +class DelegateOnGetter { + private interface Bar { + void setList(java.util.ArrayList list); + int getInt(); + } + private final @Delegate @Getter(lazy = true) java.util.concurrent.atomic.AtomicReference> bar = new java.util.concurrent.atomic.AtomicReference>(); + public @Delegate @java.lang.SuppressWarnings("all") Bar getBar() { + java.util.concurrent.atomic.AtomicReference value = this.bar.get(); + if ((value == null)) + { + synchronized (this.bar) + { + value = this.bar.get(); + if ((value == null)) + { + value = new java.util.concurrent.atomic.AtomicReference(new Bar() { + public void setList(java.util.ArrayList list) { + } + public int getInt() { + return 42; + } +}); + this.bar.set(value); + } + } + } + return value.get(); + } + public @java.lang.SuppressWarnings("all") int getInt() { + return this.getBar().getInt(); + } + public @java.lang.SuppressWarnings("all") void setList(final java.util.ArrayList list) { + this.getBar().setList(list); + } + DelegateOnGetter() { + super(); + } +} \ No newline at end of file diff --git a/test/transform/resource/before/DelegateOnGetter.java b/test/transform/resource/before/DelegateOnGetter.java new file mode 100644 index 00000000..afe09ff4 --- /dev/null +++ b/test/transform/resource/before/DelegateOnGetter.java @@ -0,0 +1,18 @@ +import lombok.Delegate; +import lombok.Getter; + +class DelegateOnGetter { + + @Delegate @Getter(lazy=true) private final Bar bar = new Bar() { + public void setList(java.util.ArrayList list) { + } + public int getInt() { + return 42; + } + }; + + private interface Bar { + void setList(java.util.ArrayList list); + int getInt(); + } +} \ No newline at end of file -- cgit