diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-02-04 00:08:48 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-02-04 00:08:48 +0100 |
commit | 58a7914027e3373faa942aa4ce7df2d3ebfb9a20 (patch) | |
tree | cb0627b028f9753a7b4c94ec2373c07a4f3fd9f8 /src/core/lombok | |
parent | 74b38cd7d6806723145f6183273468996ea7dd57 (diff) | |
download | lombok-58a7914027e3373faa942aa4ce7df2d3ebfb9a20.tar.gz lombok-58a7914027e3373faa942aa4ce7df2d3ebfb9a20.tar.bz2 lombok-58a7914027e3373faa942aa4ce7df2d3ebfb9a20.zip |
@UtilityClass handlers now more intelligent about inner types of implicitly static contexts (enums, interfaces, and annotation declarations). Also added tests to test for these.
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleUtilityClass.java | 16 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleUtilityClass.java | 22 |
2 files changed, 28 insertions, 10 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleUtilityClass.java b/src/core/lombok/eclipse/handlers/HandleUtilityClass.java index 36a7dc9c..176ff2d8 100644 --- a/src/core/lombok/eclipse/handlers/HandleUtilityClass.java +++ b/src/core/lombok/eclipse/handlers/HandleUtilityClass.java @@ -83,8 +83,10 @@ public class HandleUtilityClass extends EclipseAnnotationHandler<UtilityClass> { typeWalk = typeWalk.up(); switch (typeWalk.getKind()) { case TYPE: - if ((((TypeDeclaration) typeWalk.get()).modifiers & ClassFileConstants.AccStatic) != 0) continue; + if ((((TypeDeclaration) typeWalk.get()).modifiers & (ClassFileConstants.AccStatic | ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0) continue; if (typeWalk.up().getKind() == Kind.COMPILATION_UNIT) return true; + errorNode.addError("@UtilityClass automatically makes the class static, however, this class cannot be made static."); + return false; case COMPILATION_UNIT: return true; default: @@ -101,7 +103,15 @@ public class HandleUtilityClass extends EclipseAnnotationHandler<UtilityClass> { classDecl.modifiers |= ClassFileConstants.AccFinal; - if (typeNode.up().getKind() != Kind.COMPILATION_UNIT) classDecl.modifiers |= ClassFileConstants.AccStatic; + boolean markStatic = true; + + if (typeNode.up().getKind() == Kind.COMPILATION_UNIT) markStatic = false; + if (markStatic && typeNode.up().getKind() == Kind.TYPE) { + TypeDeclaration typeDecl = (TypeDeclaration) typeNode.up().get(); + if ((typeDecl.modifiers & ClassFileConstants.AccInterface) != 0) markStatic = false; + } + + if (markStatic) classDecl.modifiers |= ClassFileConstants.AccStatic; for (EclipseNode element : typeNode.down()) { if (element.getKind() == Kind.FIELD) { @@ -155,7 +165,7 @@ public class HandleUtilityClass extends EclipseAnnotationHandler<UtilityClass> { AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); - long[] ps = new long[3]; + long[] ps = new long[JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION.length]; Arrays.fill(ps, p); exception.type = new QualifiedTypeReference(JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION, ps); setGeneratedBy(exception.type, source); diff --git a/src/core/lombok/javac/handlers/HandleUtilityClass.java b/src/core/lombok/javac/handlers/HandleUtilityClass.java index 9a37653e..a4f8cb45 100644 --- a/src/core/lombok/javac/handlers/HandleUtilityClass.java +++ b/src/core/lombok/javac/handlers/HandleUtilityClass.java @@ -44,7 +44,6 @@ import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCTypeParameter; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.List; -import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; /** @@ -79,8 +78,11 @@ public class HandleUtilityClass extends JavacAnnotationHandler<UtilityClass> { typeWalk = typeWalk.up(); switch (typeWalk.getKind()) { case TYPE: - if ((((JCClassDecl) typeWalk.get()).mods.flags & Flags.STATIC) != 0) continue; + JCClassDecl typeDef = (JCClassDecl) typeWalk.get(); + if ((typeDef.mods.flags & (Flags.STATIC | Flags.ANNOTATION | Flags.ENUM | Flags.INTERFACE)) != 0) continue; if (typeWalk.up().getKind() == Kind.COMPILATION_UNIT) return true; + errorNode.addError("@UtilityClass automatically makes the class static, however, this class cannot be made static."); + return false; case COMPILATION_UNIT: return true; default: @@ -97,7 +99,15 @@ public class HandleUtilityClass extends JavacAnnotationHandler<UtilityClass> { classDecl.mods.flags |= Flags.FINAL; - if (typeNode.up().getKind() != Kind.COMPILATION_UNIT) classDecl.mods.flags |= Flags.STATIC; + boolean markStatic = true; + + if (typeNode.up().getKind() == Kind.COMPILATION_UNIT) markStatic = false; + if (markStatic && typeNode.up().getKind() == Kind.TYPE) { + JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get(); + if ((typeDecl.mods.flags & Flags.INTERFACE) != 0) markStatic = false; + } + + if (markStatic) classDecl.mods.flags |= Flags.STATIC; for (JavacNode element : typeNode.down()) { if (element.getKind() == Kind.FIELD) { @@ -135,13 +145,11 @@ public class HandleUtilityClass extends JavacAnnotationHandler<UtilityClass> { } private List<JCStatement> createThrowStatement(JavacNode typeNode, JavacTreeMaker maker) { - ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); - JCExpression exceptionType = genTypeRef(typeNode, "java.lang.UnsupportedOperationException"); + JCExpression exceptionType = genJavaLangTypeRef(typeNode, "UnsupportedOperationException"); List<JCExpression> jceBlank = List.nil(); JCExpression message = maker.Literal("This is a utility class and cannot be instantiated"); JCExpression exceptionInstance = maker.NewClass(null, jceBlank, exceptionType, List.of(message), null); JCStatement throwStatement = maker.Throw(exceptionInstance); - statements.add(throwStatement); - return statements.toList(); + return List.of(throwStatement); } } |