From 4372aedbd03cc4e43b90a4c0c4f703da01f322ef Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Thu, 20 Feb 2014 23:08:37 +0100 Subject: Issue 641: @Delegate is incorrectly allowed on static members in javac. Also added tests, Reinier. --- src/core/lombok/javac/handlers/HandleDelegate.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/javac/handlers/HandleDelegate.java b/src/core/lombok/javac/handlers/HandleDelegate.java index c9da4738..5e603777 100644 --- a/src/core/lombok/javac/handlers/HandleDelegate.java +++ b/src/core/lombok/javac/handlers/HandleDelegate.java @@ -62,6 +62,7 @@ import com.sun.tools.javac.code.Type.ClassType; import com.sun.tools.javac.code.Type.TypeVar; import com.sun.tools.javac.code.Types; import com.sun.tools.javac.model.JavacTypes; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; @@ -93,6 +94,8 @@ public class HandleDelegate extends JavacAnnotationHandler { "clone()", "finalize()")); + private static final String LEGALITY_OF_DELEGATE = "@Delegate is legal only on instance fields or no-argument instance methods."; + @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { deleteAnnotationIfNeccessary(annotationNode, Delegate.class); @@ -100,19 +103,24 @@ public class HandleDelegate extends JavacAnnotationHandler { Name delegateName = annotationNode.toName(annotationNode.up().getName()); DelegateReceiver delegateReceiver; JavacResolution reso = new JavacResolution(annotationNode.getContext()); + JCTree member = annotationNode.up().get(); if (annotationNode.up().getKind() == Kind.FIELD) { + if ((((JCVariableDecl) member).mods.flags & Flags.STATIC) != 0) { + annotationNode.addError(LEGALITY_OF_DELEGATE); + return; + } delegateReceiver = DelegateReceiver.FIELD; - delegateType = annotationNode.up().get().type; + delegateType = member.type; if (delegateType == null) reso.resolveClassMember(annotationNode.up()); - delegateType = annotationNode.up().get().type; + delegateType = member.type; } else if (annotationNode.up().getKind() == Kind.METHOD) { - if (!(annotationNode.up().get() instanceof JCMethodDecl)) { - annotationNode.addError("@Delegate is legal only on no-argument methods."); + if (!(member instanceof JCMethodDecl)) { + annotationNode.addError(LEGALITY_OF_DELEGATE); return; } - JCMethodDecl methodDecl = (JCMethodDecl) annotationNode.up().get(); - if (!methodDecl.params.isEmpty()) { - annotationNode.addError("@Delegate is legal only on no-argument methods."); + JCMethodDecl methodDecl = (JCMethodDecl) member; + if (!methodDecl.params.isEmpty() || (methodDecl.mods.flags & Flags.STATIC) != 0) { + annotationNode.addError(LEGALITY_OF_DELEGATE); return; } delegateReceiver = DelegateReceiver.METHOD; -- cgit