diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-10-16 09:32:36 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-10-16 09:32:36 +0200 |
commit | b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb (patch) | |
tree | 571d13cd7028a6b7d1ebfe84180a4328a20c42d7 /src/lombok/javac/handlers | |
parent | 8629a651a66aa5fba9e0ada7df00803528b0e34f (diff) | |
download | lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.tar.gz lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.tar.bz2 lombok-b5c8b725655d2ad8a715cfb1fbbdf25dbdcd4ceb.zip |
Fixed issue #24 by refactoring the AST.Node class - taken it out, and in the process fixed a lot of type annoyance by adding more generics.
Also changed coding style from for/while/if/switch/catch/do ( expr ) {} to for (expr) {}, hence the changes _everywhere_.
Diffstat (limited to 'src/lombok/javac/handlers')
-rw-r--r-- | src/lombok/javac/handlers/HandleCleanup.java | 58 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 52 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleEqualsAndHashCode.java | 145 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 31 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandlePrintAST.java | 8 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 28 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSneakyThrows.java | 30 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSynchronized.java | 18 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleToString.java | 88 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 112 |
10 files changed, 287 insertions, 283 deletions
diff --git a/src/lombok/javac/handlers/HandleCleanup.java b/src/lombok/javac/handlers/HandleCleanup.java index 7a2f232d..acee24b1 100644 --- a/src/lombok/javac/handlers/HandleCleanup.java +++ b/src/lombok/javac/handlers/HandleCleanup.java @@ -25,7 +25,7 @@ import lombok.Cleanup; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -52,34 +52,34 @@ import com.sun.tools.javac.util.Name; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { - @Override public boolean handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, Node annotationNode) { + @Override public boolean handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) { String cleanupName = annotation.getInstance().value(); - if ( cleanupName.length() == 0 ) { + if (cleanupName.length() == 0) { annotationNode.addError("cleanupName cannot be the empty string."); return true; } - if ( annotationNode.up().getKind() != Kind.LOCAL ) { + if (annotationNode.up().getKind() != Kind.LOCAL) { annotationNode.addError("@Cleanup is legal only on local variable declarations."); return true; } JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get(); - if ( decl.init == null ) { + if (decl.init == null) { annotationNode.addError("@Cleanup variable declarations need to be initialized."); return true; } - Node ancestor = annotationNode.up().directUp(); + JavacNode ancestor = annotationNode.up().directUp(); JCTree blockNode = ancestor.get(); final List<JCStatement> statements; - if ( blockNode instanceof JCBlock ) { + if (blockNode instanceof JCBlock) { statements = ((JCBlock)blockNode).stats; - } else if ( blockNode instanceof JCCase ) { + } else if (blockNode instanceof JCCase) { statements = ((JCCase)blockNode).stats; - } else if ( blockNode instanceof JCMethodDecl ) { + } else if (blockNode instanceof JCMethodDecl) { statements = ((JCMethodDecl)blockNode).body.stats; } else { annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block."); @@ -89,14 +89,16 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { boolean seenDeclaration = false; List<JCStatement> tryBlock = List.nil(); List<JCStatement> newStatements = List.nil(); - for ( JCStatement statement : statements ) { - if ( !seenDeclaration ) { - if ( statement == decl ) seenDeclaration = true; + for (JCStatement statement : statements) { + if (!seenDeclaration) { + if (statement == decl) seenDeclaration = true; newStatements = newStatements.append(statement); - } else tryBlock = tryBlock.append(statement); + } else { + tryBlock = tryBlock.append(statement); + } } - if ( !seenDeclaration ) { + if (!seenDeclaration) { annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent."); return true; } @@ -111,11 +113,11 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { JCBlock finalizer = maker.Block(0, finalizerBlock); newStatements = newStatements.append(maker.Try(maker.Block(0, tryBlock), List.<JCCatch>nil(), finalizer)); - if ( blockNode instanceof JCBlock ) { + if (blockNode instanceof JCBlock) { ((JCBlock)blockNode).stats = newStatements; - } else if ( blockNode instanceof JCCase ) { + } else if (blockNode instanceof JCCase) { ((JCCase)blockNode).stats = newStatements; - } else if ( blockNode instanceof JCMethodDecl ) { + } else if (blockNode instanceof JCMethodDecl) { ((JCMethodDecl)blockNode).body.stats = newStatements; } else throw new AssertionError("Should not get here"); @@ -124,20 +126,20 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { return true; } - private void doAssignmentCheck(Node node, List<JCStatement> statements, Name name) { - for ( JCStatement statement : statements ) doAssignmentCheck0(node, statement, name); + private void doAssignmentCheck(JavacNode node, List<JCStatement> statements, Name name) { + for (JCStatement statement : statements) doAssignmentCheck0(node, statement, name); } - private void doAssignmentCheck0(Node node, JCTree statement, Name name) { - if ( statement instanceof JCAssign ) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name); - if ( statement instanceof JCExpressionStatement ) doAssignmentCheck0(node, + private void doAssignmentCheck0(JavacNode node, JCTree statement, Name name) { + if (statement instanceof JCAssign) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name); + if (statement instanceof JCExpressionStatement) doAssignmentCheck0(node, ((JCExpressionStatement)statement).expr, name); - if ( statement instanceof JCVariableDecl ) doAssignmentCheck0(node, ((JCVariableDecl)statement).init, name); - if ( statement instanceof JCTypeCast ) doAssignmentCheck0(node, ((JCTypeCast)statement).expr, name); - if ( statement instanceof JCIdent ) { - if ( ((JCIdent)statement).name.contentEquals(name) ) { - Node problemNode = node.getNodeFor(statement); - if ( problemNode != null ) problemNode.addWarning( + if (statement instanceof JCVariableDecl) doAssignmentCheck0(node, ((JCVariableDecl)statement).init, name); + if (statement instanceof JCTypeCast) doAssignmentCheck0(node, ((JCTypeCast)statement).expr, name); + if (statement instanceof JCIdent) { + if (((JCIdent)statement).name.contentEquals(name)) { + JavacNode problemNode = node.getNodeFor(statement); + if (problemNode != null) problemNode.addWarning( "You're assigning an auto-cleanup variable to something else. This is a bad idea."); } } diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index 2e1d01b1..f18af241 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -30,7 +30,7 @@ import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import lombok.javac.handlers.PKG.MemberExistsResult; import org.mangosdk.spi.ProviderFor; @@ -58,32 +58,32 @@ import com.sun.tools.javac.util.List; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleData implements JavacAnnotationHandler<Data> { - @Override public boolean handle(AnnotationValues<Data> annotation, JCAnnotation ast, Node annotationNode) { - Node typeNode = annotationNode.up(); + @Override public boolean handle(AnnotationValues<Data> annotation, JCAnnotation ast, JavacNode annotationNode) { + JavacNode typeNode = annotationNode.up(); JCClassDecl typeDecl = null; - if ( typeNode.get() instanceof JCClassDecl ) typeDecl = (JCClassDecl)typeNode.get(); + if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl)typeNode.get(); long flags = typeDecl == null ? 0 : typeDecl.mods.flags; boolean notAClass = (flags & (Flags.INTERFACE | Flags.ENUM | Flags.ANNOTATION)) != 0; - if ( typeDecl == null || notAClass ) { + if (typeDecl == null || notAClass) { annotationNode.addError("@Data is only supported on a class."); return false; } - List<Node> nodesForEquality = List.nil(); - List<Node> nodesForConstructor = List.nil(); - for ( Node child : typeNode.down() ) { - if ( child.getKind() != Kind.FIELD ) continue; + List<JavacNode> nodesForEquality = List.nil(); + List<JavacNode> nodesForConstructor = List.nil(); + for (JavacNode child : typeNode.down()) { + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); long fieldFlags = fieldDecl.mods.flags; //Skip static fields. - if ( (fieldFlags & Flags.STATIC) != 0 ) continue; - if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child); + if ((fieldFlags & Flags.STATIC) != 0) continue; + if ((fieldFlags & Flags.TRANSIENT) == 0) nodesForEquality = nodesForEquality.append(child); boolean isFinal = (fieldFlags & Flags.FINAL) != 0; boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty(); - if ( (isFinal || isNonNull) && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child); + if ((isFinal || isNonNull) && fieldDecl.init == null) nodesForConstructor = nodesForConstructor.append(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); - if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); + if (!isFinal) new HandleSetter().generateSetterForField(child, annotationNode.get()); } new HandleToString().generateToStringForType(typeNode, annotationNode); @@ -91,12 +91,12 @@ public class HandleData implements JavacAnnotationHandler<Data> { String staticConstructorName = annotation.getInstance().staticConstructor(); - if ( constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS ) { + if (constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS) { JCMethodDecl constructor = createConstructor(staticConstructorName.equals(""), typeNode, nodesForConstructor); injectMethod(typeNode, constructor); } - if ( !staticConstructorName.isEmpty() && methodExists("of", typeNode) == MemberExistsResult.NOT_EXISTS ) { + if (!staticConstructorName.isEmpty() && methodExists("of", typeNode) == MemberExistsResult.NOT_EXISTS) { JCMethodDecl staticConstructor = createStaticConstructor(staticConstructorName, typeNode, nodesForConstructor); injectMethod(typeNode, staticConstructor); } @@ -104,7 +104,7 @@ public class HandleData implements JavacAnnotationHandler<Data> { return true; } - private JCMethodDecl createConstructor(boolean isPublic, Node typeNode, List<Node> fields) { + private JCMethodDecl createConstructor(boolean isPublic, JavacNode typeNode, List<JavacNode> fields) { TreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -112,7 +112,7 @@ public class HandleData implements JavacAnnotationHandler<Data> { List<JCStatement> assigns = List.nil(); List<JCVariableDecl> params = List.nil(); - for ( Node fieldNode : fields ) { + for (JavacNode fieldNode : fields) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); @@ -133,7 +133,7 @@ public class HandleData implements JavacAnnotationHandler<Data> { null, type.typarams, params, List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns)), null); } - private JCMethodDecl createStaticConstructor(String name, Node typeNode, List<Node> fields) { + private JCMethodDecl createStaticConstructor(String name, JavacNode typeNode, List<JavacNode> fields) { TreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -147,8 +147,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { List<JCExpression> typeArgs2 = List.nil(); List<JCExpression> args = List.nil(); - if ( !type.typarams.isEmpty() ) { - for ( JCTypeParameter param : type.typarams ) { + if (!type.typarams.isEmpty()) { + for (JCTypeParameter param : type.typarams) { typeArgs1 = typeArgs1.append(maker.Ident(param.name)); typeArgs2 = typeArgs2.append(maker.Ident(param.name)); typeParams = typeParams.append(maker.TypeParameter(param.name, param.bounds)); @@ -160,16 +160,18 @@ public class HandleData implements JavacAnnotationHandler<Data> { constructorType = maker.Ident(type.name); } - for ( Node fieldNode : fields ) { + for (JavacNode fieldNode : fields) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); JCExpression pType; - if ( field.vartype instanceof JCIdent ) pType = maker.Ident(((JCIdent)field.vartype).name); - else if ( field.vartype instanceof JCTypeApply ) { + if (field.vartype instanceof JCIdent) pType = maker.Ident(((JCIdent)field.vartype).name); + else if (field.vartype instanceof JCTypeApply) { JCTypeApply typeApply = (JCTypeApply) field.vartype; List<JCExpression> tArgs = List.nil(); - for ( JCExpression arg : typeApply.arguments ) tArgs = tArgs.append(arg); + for (JCExpression arg : typeApply.arguments) tArgs = tArgs.append(arg); pType = maker.TypeApply(typeApply.clazz, tArgs); - } else pType = field.vartype; + } else { + pType = field.vartype; + } List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL, nonNulls.appendList(nullables)), field.name, pType, null); diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java index 21966be2..fa1f4d0d 100644 --- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -27,7 +27,7 @@ import lombok.core.AnnotationValues; import lombok.core.AST.Kind; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -58,33 +58,33 @@ import com.sun.tools.javac.util.Name; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAndHashCode> { - private void checkForBogusFieldNames(Node type, AnnotationValues<EqualsAndHashCode> annotation) { - if ( annotation.isExplicit("exclude") ) { - for ( int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, true) ) { + private void checkForBogusFieldNames(JavacNode type, AnnotationValues<EqualsAndHashCode> annotation) { + if (annotation.isExplicit("exclude")) { + for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, true)) { annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i); } } - if ( annotation.isExplicit("of") ) { - for ( int i : createListOfNonExistentFields(List.from(annotation.getInstance().of()), type, false, false) ) { + if (annotation.isExplicit("of")) { + for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().of()), type, false, false)) { annotation.setWarning("of", "This field does not exist.", i); } } } - @Override public boolean handle(AnnotationValues<EqualsAndHashCode> annotation, JCAnnotation ast, Node annotationNode) { + @Override public boolean handle(AnnotationValues<EqualsAndHashCode> annotation, JCAnnotation ast, JavacNode annotationNode) { EqualsAndHashCode ann = annotation.getInstance(); List<String> excludes = List.from(ann.exclude()); List<String> includes = List.from(ann.of()); - Node typeNode = annotationNode.up(); + JavacNode typeNode = annotationNode.up(); checkForBogusFieldNames(typeNode, annotation); Boolean callSuper = ann.callSuper(); - if ( !annotation.isExplicit("callSuper") ) callSuper = null; - if ( !annotation.isExplicit("exclude") ) excludes = null; - if ( !annotation.isExplicit("of") ) includes = null; + if (!annotation.isExplicit("callSuper")) callSuper = null; + if (!annotation.isExplicit("exclude")) excludes = null; + if (!annotation.isExplicit("of")) includes = null; - if ( excludes != null && includes != null ) { + if (excludes != null && includes != null) { excludes = null; annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored."); } @@ -92,10 +92,10 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return generateMethods(typeNode, annotationNode, excludes, includes, callSuper, true); } - public void generateEqualsAndHashCodeForType(Node typeNode, Node errorNode) { - for ( Node child : typeNode.down() ) { - if ( child.getKind() == Kind.ANNOTATION ) { - if ( Javac.annotationTypeMatches(EqualsAndHashCode.class, child) ) { + public void generateEqualsAndHashCodeForType(JavacNode typeNode, JavacNode errorNode) { + for (JavacNode child : typeNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(EqualsAndHashCode.class, child)) { //The annotation will make it happen, so we can skip it. return; } @@ -105,66 +105,66 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd generateMethods(typeNode, errorNode, null, null, null, false); } - private boolean generateMethods(Node typeNode, Node errorNode, List<String> excludes, List<String> includes, + private boolean generateMethods(JavacNode typeNode, JavacNode errorNode, List<String> excludes, List<String> includes, Boolean callSuper, boolean whineIfExists) { boolean notAClass = true; - if ( typeNode.get() instanceof JCClassDecl ) { + if (typeNode.get() instanceof JCClassDecl) { long flags = ((JCClassDecl)typeNode.get()).mods.flags; notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; } - if ( notAClass ) { + if (notAClass) { errorNode.addError("@EqualsAndHashCode is only supported on a class."); return false; } boolean isDirectDescendantOfObject = true; boolean implicitCallSuper = callSuper == null; - if ( callSuper == null ) { + if (callSuper == null) { try { callSuper = ((Boolean)EqualsAndHashCode.class.getMethod("callSuper").getDefaultValue()).booleanValue(); - } catch ( Exception ignore ) {} + } catch (Exception ignore) {} } JCTree extending = ((JCClassDecl)typeNode.get()).extending; - if ( extending != null ) { + if (extending != null) { String p = extending.toString(); isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object"); } - if ( isDirectDescendantOfObject && callSuper ) { + if (isDirectDescendantOfObject && callSuper) { errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless."); return true; } - if ( !isDirectDescendantOfObject && !callSuper && implicitCallSuper ) { + if (!isDirectDescendantOfObject && !callSuper && implicitCallSuper) { errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type."); } - List<Node> nodesForEquality = List.nil(); - if ( includes != null ) { - for ( Node child : typeNode.down() ) { - if ( child.getKind() != Kind.FIELD ) continue; + List<JavacNode> nodesForEquality = List.nil(); + if (includes != null) { + for (JavacNode child : typeNode.down()) { + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); - if ( includes.contains(fieldDecl.name.toString()) ) nodesForEquality = nodesForEquality.append(child); + if (includes.contains(fieldDecl.name.toString())) nodesForEquality = nodesForEquality.append(child); } } else { - for ( Node child : typeNode.down() ) { - if ( child.getKind() != Kind.FIELD ) continue; + for (JavacNode child : typeNode.down()) { + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); //Skip static fields. - if ( (fieldDecl.mods.flags & Flags.STATIC) != 0 ) continue; + if ((fieldDecl.mods.flags & Flags.STATIC) != 0) continue; //Skip transient fields. - if ( (fieldDecl.mods.flags & Flags.TRANSIENT) != 0 ) continue; + if ((fieldDecl.mods.flags & Flags.TRANSIENT) != 0) continue; //Skip excluded fields. - if ( excludes != null && excludes.contains(fieldDecl.name.toString()) ) continue; + if (excludes != null && excludes.contains(fieldDecl.name.toString())) continue; //Skip fields that start with $ - if ( fieldDecl.name.toString().startsWith("$") ) continue; + if (fieldDecl.name.toString().startsWith("$")) continue; nodesForEquality = nodesForEquality.append(child); } } - switch ( methodExists("hashCode", typeNode) ) { + switch (methodExists("hashCode", typeNode)) { case NOT_EXISTS: JCMethodDecl method = createHashCode(typeNode, nodesForEquality, callSuper); injectMethod(typeNode, method); @@ -173,13 +173,13 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd break; default: case EXISTS_BY_USER: - if ( whineIfExists ) { + if (whineIfExists) { errorNode.addWarning("Not generating hashCode(): A method with that name already exists"); } break; } - switch ( methodExists("equals", typeNode) ) { + switch (methodExists("equals", typeNode)) { case NOT_EXISTS: JCMethodDecl method = createEquals(typeNode, nodesForEquality, callSuper); injectMethod(typeNode, method); @@ -188,7 +188,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd break; default: case EXISTS_BY_USER: - if ( whineIfExists ) { + if (whineIfExists) { errorNode.addWarning("Not generating equals(Object other): A method with that name already exists"); } break; @@ -197,7 +197,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return true; } - private JCMethodDecl createHashCode(Node typeNode, List<Node> fields, boolean callSuper) { + private JCMethodDecl createHashCode(JavacNode typeNode, List<JavacNode> fields, boolean callSuper) { TreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); @@ -208,9 +208,9 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd Name primeName = typeNode.toName("PRIME"); Name resultName = typeNode.toName("result"); /* final int PRIME = 31; */ { - if ( !fields.isEmpty() || callSuper ) { - statements = statements.append( - maker.VarDef(maker.Modifiers(Flags.FINAL), primeName, maker.TypeIdent(TypeTags.INT), maker.Literal(31))); + if (!fields.isEmpty() || callSuper) { + statements = statements.append(maker.VarDef(maker.Modifiers(Flags.FINAL), + primeName, maker.TypeIdent(TypeTags.INT), maker.Literal(31))); } } @@ -220,7 +220,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd List<JCExpression> intoResult = List.nil(); - if ( callSuper ) { + if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")), List.<JCExpression>nil()); @@ -228,13 +228,13 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd } int tempCounter = 0; - for ( Node fieldNode : fields ) { + for (JavacNode fieldNode : fields) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); JCExpression fType = field.vartype; JCExpression thisDotField = maker.Select(maker.Ident(typeNode.toName("this")), field.name); JCExpression thisDotFieldClone = maker.Select(maker.Ident(typeNode.toName("this")), field.name); - if ( fType instanceof JCPrimitiveTypeTree ) { - switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) { + if (fType instanceof JCPrimitiveTypeTree) { + switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) { case BOOLEAN: /* this.fieldName ? 1231 : 1237 */ intoResult = intoResult.append(maker.Conditional(thisDotField, maker.Literal(1231), maker.Literal(1237))); @@ -269,7 +269,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd intoResult = intoResult.append(thisDotField); break; } - } else if ( fType instanceof JCArrayTypeTree ) { + } else if (fType instanceof JCArrayTypeTree) { /* java.util.Arrays.deepHashCode(this.fieldName) //use just hashCode() for primitive arrays. */ boolean multiDim = ((JCArrayTypeTree)fType).elemtype instanceof JCArrayTypeTree; boolean primitiveArray = ((JCArrayTypeTree)fType).elemtype instanceof JCPrimitiveTypeTree; @@ -290,7 +290,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd /* fold each intoResult entry into: result = result * PRIME + (item); */ - for ( JCExpression expr : intoResult ) { + for (JCExpression expr : intoResult) { JCExpression mult = maker.Binary(JCTree.MUL, maker.Ident(resultName), maker.Ident(primeName)); JCExpression add = maker.Binary(JCTree.PLUS, mult, expr); statements = statements.append(maker.Exec(maker.Assign(maker.Ident(resultName), add))); @@ -313,7 +313,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return maker.TypeCast(maker.TypeIdent(TypeTags.INT), xorBits); } - private JCMethodDecl createEquals(Node typeNode, List<Node> fields, boolean callSuper) { + private JCMethodDecl createEquals(JavacNode typeNode, List<JavacNode> fields, boolean callSuper) { TreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -329,17 +329,17 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd List<JCStatement> statements = List.nil(); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), oName, objectType, null)); - /* if ( o == this ) return true; */ { - statements = statements.append( - maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Ident(thisName)), returnBool(maker, true), null)); + /* if (o == this) return true; */ { + statements = statements.append(maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), + maker.Ident(thisName)), returnBool(maker, true), null)); } - /* if ( o == null ) return false; */ { - statements = statements.append( - maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Literal(TypeTags.BOT, null)), returnBool(maker, false), null)); + /* if (o == null) return false; */ { + statements = statements.append(maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), + maker.Literal(TypeTags.BOT, null)), returnBool(maker, false), null)); } - /* if ( o.getClass() != this.getClass() ) return false; */ { + /* if (o.getClass() != this.getClass()) return false; */ { Name getClass = typeNode.toName("getClass"); List<JCExpression> exprNil = List.nil(); JCExpression oGetClass = maker.Apply(exprNil, maker.Select(maker.Ident(oName), getClass), exprNil); @@ -348,8 +348,8 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd maker.If(maker.Binary(JCTree.NE, oGetClass, thisGetClass), returnBool(maker, false), null)); } - /* if ( !super.equals(o) ) return false; */ - if ( callSuper ) { + /* if (!super.equals(o)) return false; */ + if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("equals")), List.<JCExpression>of(maker.Ident(oName))); @@ -361,12 +361,12 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd final JCExpression selfType1, selfType2; List<JCExpression> wildcards1 = List.nil(); List<JCExpression> wildcards2 = List.nil(); - for ( int i = 0 ; i < type.typarams.length() ; i++ ) { + for (int i = 0 ; i < type.typarams.length() ; i++) { wildcards1 = wildcards1.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null)); wildcards2 = wildcards2.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null)); } - if ( type.typarams.isEmpty() ) { + if (type.typarams.isEmpty()) { selfType1 = maker.Ident(type.name); selfType2 = maker.Ident(type.name); } else { @@ -378,29 +378,29 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, selfType1, maker.TypeCast(selfType2, maker.Ident(oName)))); } - for ( Node fieldNode : fields ) { + for (JavacNode fieldNode : fields) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); JCExpression fType = field.vartype; JCExpression thisDotField = maker.Select(maker.Ident(thisName), field.name); JCExpression otherDotField = maker.Select(maker.Ident(otherName), field.name); - if ( fType instanceof JCPrimitiveTypeTree ) { - switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) { + if (fType instanceof JCPrimitiveTypeTree) { + switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) { case FLOAT: - /* if ( Float.compare(this.fieldName, other.fieldName) != 0 ) return false; */ + /* if (Float.compare(this.fieldName, other.fieldName) != 0) return false; */ statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, false)); break; case DOUBLE: - /* if ( Double(this.fieldName, other.fieldName) != 0 ) return false; */ + /* if (Double(this.fieldName, other.fieldName) != 0) return false; */ statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, true)); break; default: - /* if ( this.fieldName != other.fieldName ) return false; */ + /* if (this.fieldName != other.fieldName) return false; */ statements = statements.append( maker.If(maker.Binary(JCTree.NE, thisDotField, otherDotField), returnBool(maker, false), null)); break; } - } else if ( fType instanceof JCArrayTypeTree ) { - /* if ( !java.util.Arrays.deepEquals(this.fieldName, other.fieldName) ) return false; //use equals for primitive arrays. */ + } else if (fType instanceof JCArrayTypeTree) { + /* if (!java.util.Arrays.deepEquals(this.fieldName, other.fieldName)) return false; //use equals for primitive arrays. */ boolean multiDim = ((JCArrayTypeTree)fType).elemtype instanceof JCArrayTypeTree; boolean primitiveArray = ((JCArrayTypeTree)fType).elemtype instanceof JCPrimitiveTypeTree; boolean useDeepEquals = multiDim || !primitiveArray; @@ -410,7 +410,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd statements = statements.append(maker.If(maker.Unary(JCTree.NOT, maker.Apply(List.<JCExpression>nil(), eqMethod, args)), returnBool(maker, false), null)); } else /* objects */ { - /* if ( this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName) ) return false; */ + /* if (this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName)) return false; */ JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, thisDotField, maker.Literal(TypeTags.BOT, null)); JCExpression otherNotEqualsNull = maker.Binary(JCTree.NE, otherDotField, maker.Literal(TypeTags.BOT, null)); JCExpression thisEqualsThat = maker.Apply( @@ -428,8 +428,9 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return maker.MethodDef(mods, typeNode.toName("equals"), returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null); } - private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, TreeMaker maker, Node node, boolean isDouble) { - /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false; */ + private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, + TreeMaker maker, JavacNode node, boolean isDouble) { + /* if (Float.compare(fieldName, other.fieldName) != 0) return false; */ JCExpression clazz = chainDots(maker, node, "java", "lang", isDouble ? "Double" : "Float"); List<JCExpression> args = List.of(thisDotField, otherDotField); JCBinary compareCallEquals0 = maker.Binary(JCTree.NE, maker.Apply( diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 47cd3095..fd39bda2 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -29,7 +29,7 @@ import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -63,10 +63,10 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { * If not, the getter is still generated if it isn't already there, though there will not * be a warning if its already there. The default access level is used. */ - public void generateGetterForField(Node fieldNode, DiagnosticPosition pos) { - for ( Node child : fieldNode.down() ) { - if ( child.getKind() == Kind.ANNOTATION ) { - if ( Javac.annotationTypeMatches(Getter.class, child) ) { + public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos) { + for (JavacNode child : fieldNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(Getter.class, child)) { //The annotation will make it happen, so we can skip it. return; } @@ -76,16 +76,17 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { createGetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, pos, false); } - @Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, Node annotationNode) { - Node fieldNode = annotationNode.up(); + @Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) { + JavacNode fieldNode = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); - if ( level == AccessLevel.NONE ) return true; + if (level == AccessLevel.NONE) return true; return createGetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); } - private boolean createGetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos, boolean whineIfExists) { - if ( fieldNode.getKind() != Kind.FIELD ) { + private boolean createGetterForField(AccessLevel level, + JavacNode fieldNode, JavacNode errorNode, DiagnosticPosition pos, boolean whineIfExists) { + if (fieldNode.getKind() != Kind.FIELD) { errorNode.addError("@Getter is only supported on a field."); return true; } @@ -93,14 +94,14 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); String methodName = toGetterName(fieldDecl); - for ( String altName : toAllGetterNames(fieldDecl) ) { - switch ( methodExists(altName, fieldNode) ) { + for (String altName : toAllGetterNames(fieldDecl)) { + switch (methodExists(altName, fieldNode)) { case EXISTS_BY_LOMBOK: return true; case EXISTS_BY_USER: - if ( whineIfExists ) { + if (whineIfExists) { String altNameExpl = ""; - if ( !altName.equals(methodName) ) altNameExpl = String.format(" (%s)", altName); + if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName); errorNode.addWarning( String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl)); } @@ -118,7 +119,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { return true; } - private JCMethodDecl createGetter(long access, Node field, TreeMaker treeMaker) { + private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker) { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); JCStatement returnStatement = treeMaker.Return(treeMaker.Ident(fieldNode.getName())); diff --git a/src/lombok/javac/handlers/HandlePrintAST.java b/src/lombok/javac/handlers/HandlePrintAST.java index aa7b0ab9..77ecd54f 100644 --- a/src/lombok/javac/handlers/HandlePrintAST.java +++ b/src/lombok/javac/handlers/HandlePrintAST.java @@ -34,19 +34,19 @@ import lombok.core.AnnotationValues; import lombok.core.PrintAST; import lombok.javac.JavacASTVisitor; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; /** * Handles the <code>lombok.core.PrintAST</code> annotation for javac. */ @ProviderFor(JavacAnnotationHandler.class) public class HandlePrintAST implements JavacAnnotationHandler<PrintAST> { - @Override public boolean handle(AnnotationValues<PrintAST> annotation, JCAnnotation ast, Node annotationNode) { + @Override public boolean handle(AnnotationValues<PrintAST> annotation, JCAnnotation ast, JavacNode annotationNode) { PrintStream stream = System.out; String fileName = annotation.getInstance().outfile(); - if ( fileName.length() > 0 ) try { + if (fileName.length() > 0) try { stream = new PrintStream(new File(fileName)); - } catch ( FileNotFoundException e ) { + } catch (FileNotFoundException e) { Lombok.sneakyThrow(e); } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index 44ad0dca..f7360988 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -28,9 +28,8 @@ import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.Javac; -import lombok.javac.JavacAST; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -66,10 +65,10 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { * If not, the setter is still generated if it isn't already there, though there will not * be a warning if its already there. The default access level is used. */ - public void generateSetterForField(Node fieldNode, DiagnosticPosition pos) { - for ( Node child : fieldNode.down() ) { - if ( child.getKind() == Kind.ANNOTATION ) { - if ( Javac.annotationTypeMatches(Setter.class, child) ) { + public void generateSetterForField(JavacNode fieldNode, DiagnosticPosition pos) { + for (JavacNode child : fieldNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(Setter.class, child)) { //The annotation will make it happen, so we can skip it. return; } @@ -79,16 +78,17 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { createSetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, pos, false); } - @Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) { - Node fieldNode = annotationNode.up(); + @Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, JavacNode annotationNode) { + JavacNode fieldNode = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); - if ( level == AccessLevel.NONE ) return true; + if (level == AccessLevel.NONE) return true; return createSetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); } - private boolean createSetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos, boolean whineIfExists) { - if ( fieldNode.getKind() != Kind.FIELD ) { + private boolean createSetterForField(AccessLevel level, + JavacNode fieldNode, JavacNode errorNode, DiagnosticPosition pos, boolean whineIfExists) { + if (fieldNode.getKind() != Kind.FIELD) { fieldNode.addError("@Setter is only supported on a field."); return true; } @@ -96,11 +96,11 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); String methodName = toSetterName(fieldDecl); - switch ( methodExists(methodName, fieldNode) ) { + switch (methodExists(methodName, fieldNode)) { case EXISTS_BY_LOMBOK: return true; case EXISTS_BY_USER: - if ( whineIfExists ) errorNode.addWarning( + if (whineIfExists) errorNode.addWarning( String.format("Not generating %s(%s %s): A method with that name already exists", methodName, fieldDecl.vartype, fieldDecl.name)); return true; @@ -116,7 +116,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { return true; } - private JCMethodDecl createSetter(long access, JavacAST.Node field, TreeMaker treeMaker) { + private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker) { JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name); diff --git a/src/lombok/javac/handlers/HandleSneakyThrows.java b/src/lombok/javac/handlers/HandleSneakyThrows.java index 4a4ab09b..3cbad7f6 100644 --- a/src/lombok/javac/handlers/HandleSneakyThrows.java +++ b/src/lombok/javac/handlers/HandleSneakyThrows.java @@ -29,7 +29,7 @@ import java.util.Collection; import lombok.SneakyThrows; import lombok.core.AnnotationValues; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -50,31 +50,31 @@ import com.sun.tools.javac.util.List; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows> { - @Override public boolean handle(AnnotationValues<SneakyThrows> annotation, JCAnnotation ast, Node annotationNode) { + @Override public boolean handle(AnnotationValues<SneakyThrows> annotation, JCAnnotation ast, JavacNode annotationNode) { Collection<String> exceptionNames = annotation.getRawExpressions("value"); List<JCExpression> memberValuePairs = ast.getArguments(); - if ( memberValuePairs == null || memberValuePairs.size() == 0 ) return false; + if (memberValuePairs == null || memberValuePairs.size() == 0) return false; JCExpression arrayOrSingle = ((JCAssign)memberValuePairs.get(0)).rhs; final List<JCExpression> exceptionNameNodes; - if ( arrayOrSingle instanceof JCNewArray ) { + if (arrayOrSingle instanceof JCNewArray) { exceptionNameNodes = ((JCNewArray)arrayOrSingle).elems; } else exceptionNameNodes = List.of(arrayOrSingle); - if ( exceptionNames.size() != exceptionNameNodes.size() ) { + if (exceptionNames.size() != exceptionNameNodes.size()) { annotationNode.addError( "LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing."); } java.util.List<String> exceptions = new ArrayList<String>(); - for ( String exception : exceptionNames ) { - if ( exception.endsWith(".class") ) exception = exception.substring(0, exception.length() - 6); + for (String exception : exceptionNames) { + if (exception.endsWith(".class")) exception = exception.substring(0, exception.length() - 6); exceptions.add(exception); } - Node owner = annotationNode.up(); - switch ( owner.getKind() ) { + JavacNode owner = annotationNode.up(); + switch (owner.getKind()) { case METHOD: return handleMethod(annotationNode, (JCMethodDecl)owner.get(), exceptions); default: @@ -83,19 +83,19 @@ public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows> } } - private boolean handleMethod(Node annotation, JCMethodDecl method, Collection<String> exceptions) { - Node methodNode = annotation.up(); + private boolean handleMethod(JavacNode annotation, JCMethodDecl method, Collection<String> exceptions) { + JavacNode methodNode = annotation.up(); - if ( (method.mods.flags & Flags.ABSTRACT) != 0 ) { + if ( (method.mods.flags & Flags.ABSTRACT) != 0) { annotation.addError("@SneakyThrows can only be used on concrete methods."); return true; } - if ( method.body == null ) return false; + if (method.body == null) return false; List<JCStatement> contents = method.body.stats; - for ( String exception : exceptions ) { + for (String exception : exceptions) { contents = List.of(buildTryCatchBlock(methodNode, contents, exception)); } @@ -105,7 +105,7 @@ public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows> return true; } - private JCStatement buildTryCatchBlock(Node node, List<JCStatement> contents, String exception) { + private JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception) { TreeMaker maker = node.getTreeMaker(); JCBlock tryBlock = maker.Block(0, contents); diff --git a/src/lombok/javac/handlers/HandleSynchronized.java b/src/lombok/javac/handlers/HandleSynchronized.java index efc6daf0..559c116a 100644 --- a/src/lombok/javac/handlers/HandleSynchronized.java +++ b/src/lombok/javac/handlers/HandleSynchronized.java @@ -40,7 +40,7 @@ import lombok.Synchronized; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; /** * Handles the <code>lombok.Synchronized</code> annotation for javac. @@ -50,32 +50,32 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized> private static final String INSTANCE_LOCK_NAME = "$lock"; private static final String STATIC_LOCK_NAME = "$LOCK"; - @Override public boolean handle(AnnotationValues<Synchronized> annotation, JCAnnotation ast, Node annotationNode) { - Node methodNode = annotationNode.up(); + @Override public boolean handle(AnnotationValues<Synchronized> annotation, JCAnnotation ast, JavacNode annotationNode) { + JavacNode methodNode = annotationNode.up(); - if ( methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof JCMethodDecl) ) { + if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof JCMethodDecl)) { annotationNode.addError("@Synchronized is legal only on methods."); return true; } JCMethodDecl method = (JCMethodDecl)methodNode.get(); - if ( (method.mods.flags & Flags.ABSTRACT) != 0 ) { + if ((method.mods.flags & Flags.ABSTRACT) != 0) { annotationNode.addError("@Synchronized is legal only on concrete methods."); return true; } boolean isStatic = (method.mods.flags & Flags.STATIC) != 0; String lockName = annotation.getInstance().value(); boolean autoMake = false; - if ( lockName.length() == 0 ) { + if (lockName.length() == 0) { autoMake = true; lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME; } TreeMaker maker = methodNode.getTreeMaker(); - if ( fieldExists(lockName, methodNode) == MemberExistsResult.NOT_EXISTS ) { - if ( !autoMake ) { + if (fieldExists(lockName, methodNode) == MemberExistsResult.NOT_EXISTS) { + if (!autoMake) { annotationNode.addError("The field " + new String(lockName) + " does not exist."); return true; } @@ -89,7 +89,7 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized> injectField(methodNode.up(), fieldDecl); } - if ( method.body == null ) return false; + if (method.body == null) return false; JCExpression lockNode = maker.Ident(methodNode.toName(lockName)); diff --git a/src/lombok/javac/handlers/HandleToString.java b/src/lombok/javac/handlers/HandleToString.java index eb2013c7..4c9b9c89 100644 --- a/src/lombok/javac/handlers/HandleToString.java +++ b/src/lombok/javac/handlers/HandleToString.java @@ -28,7 +28,7 @@ import lombok.core.AnnotationValues; import lombok.core.AST.Kind; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import org.mangosdk.spi.ProviderFor; @@ -54,34 +54,34 @@ import com.sun.tools.javac.util.List; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleToString implements JavacAnnotationHandler<ToString> { - private void checkForBogusFieldNames(Node type, AnnotationValues<ToString> annotation) { - if ( annotation.isExplicit("exclude") ) { - for ( int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, false) ) { + private void checkForBogusFieldNames(JavacNode type, AnnotationValues<ToString> annotation) { + if (annotation.isExplicit("exclude")) { + for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, false)) { annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i); } } - if ( annotation.isExplicit("of") ) { - for ( int i : createListOfNonExistentFields(List.from(annotation.getInstance().of()), type, false, false) ) { + if (annotation.isExplicit("of")) { + for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().of()), type, false, false)) { annotation.setWarning("of", "This field does not exist.", i); } } } - @Override public boolean handle(AnnotationValues<ToString> annotation, JCAnnotation ast, Node annotationNode) { + @Override public boolean handle(AnnotationValues<ToString> annotation, JCAnnotation ast, JavacNode annotationNode) { ToString ann = annotation.getInstance(); List<String> excludes = List.from(ann.exclude()); List<String> includes = List.from(ann.of()); - Node typeNode = annotationNode.up(); + JavacNode typeNode = annotationNode.up(); checkForBogusFieldNames(typeNode, annotation); Boolean callSuper = ann.callSuper(); - if ( !annotation.isExplicit("callSuper") ) callSuper = null; - if ( !annotation.isExplicit("exclude") ) excludes = null; - if ( !annotation.isExplicit("of") ) includes = null; + if (!annotation.isExplicit("callSuper")) callSuper = null; + if (!annotation.isExplicit("exclude")) excludes = null; + if (!annotation.isExplicit("of")) includes = null; - if ( excludes != null && includes != null ) { + if (excludes != null && includes != null) { excludes = null; annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored."); } @@ -89,10 +89,10 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { return generateToString(typeNode, annotationNode, excludes, includes, ann.includeFieldNames(), callSuper, true); } - public void generateToStringForType(Node typeNode, Node errorNode) { - for ( Node child : typeNode.down() ) { - if ( child.getKind() == Kind.ANNOTATION ) { - if ( Javac.annotationTypeMatches(ToString.class, child) ) { + public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) { + for (JavacNode child : typeNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(ToString.class, child)) { //The annotation will make it happen, so we can skip it. return; } @@ -102,51 +102,51 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { boolean includeFieldNames = true; try { includeFieldNames = ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue(); - } catch ( Exception ignore ) {} + } catch (Exception ignore) {} generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false); } - private boolean generateToString(Node typeNode, Node errorNode, List<String> excludes, List<String> includes, + private boolean generateToString(JavacNode typeNode, JavacNode errorNode, List<String> excludes, List<String> includes, boolean includeFieldNames, Boolean callSuper, boolean whineIfExists) { boolean notAClass = true; - if ( typeNode.get() instanceof JCClassDecl ) { + if (typeNode.get() instanceof JCClassDecl) { long flags = ((JCClassDecl)typeNode.get()).mods.flags; notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; } - if ( callSuper == null ) { + if (callSuper == null) { try { callSuper = ((Boolean)ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue(); - } catch ( Exception ignore ) {} + } catch (Exception ignore) {} } - if ( notAClass ) { + if (notAClass) { errorNode.addError("@ToString is only supported on a class."); return false; } - List<Node> nodesForToString = List.nil(); - if ( includes != null ) { - for ( Node child : typeNode.down() ) { - if ( child.getKind() != Kind.FIELD ) continue; + List<JavacNode> nodesForToString = List.nil(); + if (includes != null) { + for (JavacNode child : typeNode.down()) { + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); - if ( includes.contains(fieldDecl.name.toString()) ) nodesForToString = nodesForToString.append(child); + if (includes.contains(fieldDecl.name.toString())) nodesForToString = nodesForToString.append(child); } } else { - for ( Node child : typeNode.down() ) { - if ( child.getKind() != Kind.FIELD ) continue; + for (JavacNode child : typeNode.down()) { + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); //Skip static fields. - if ( (fieldDecl.mods.flags & Flags.STATIC) != 0 ) continue; + if ((fieldDecl.mods.flags & Flags.STATIC) != 0) continue; //Skip excluded fields. - if ( excludes != null && excludes.contains(fieldDecl.name.toString()) ) continue; + if (excludes != null && excludes.contains(fieldDecl.name.toString())) continue; //Skip fields that start with $. - if ( fieldDecl.name.toString().startsWith("$") ) continue; + if (fieldDecl.name.toString().startsWith("$")) continue; nodesForToString = nodesForToString.append(child); } } - switch ( methodExists("toString", typeNode) ) { + switch (methodExists("toString", typeNode)) { case NOT_EXISTS: JCMethodDecl method = createToString(typeNode, nodesForToString, includeFieldNames, callSuper); injectMethod(typeNode, method); @@ -155,7 +155,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { return true; default: case EXISTS_BY_USER: - if ( whineIfExists ) { + if (whineIfExists) { errorNode.addWarning("Not generating toString(): A method with that name already exists"); } return true; @@ -163,7 +163,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { } - private JCMethodDecl createToString(Node typeNode, List<Node> fields, boolean includeFieldNames, boolean callSuper) { + private JCMethodDecl createToString(JavacNode typeNode, List<JavacNode> fields, boolean includeFieldNames, boolean callSuper) { TreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); @@ -176,11 +176,11 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { String infix = ", "; String suffix = ")"; String prefix; - if ( callSuper ) { + if (callSuper) { prefix = typeName + "(super="; - } else if ( fields.isEmpty() ) { + } else if (fields.isEmpty()) { prefix = typeName + "()"; - } else if ( includeFieldNames ) { + } else if (includeFieldNames) { prefix = typeName + "(" + ((JCVariableDecl)fields.iterator().next().get()).name.toString() + "="; } else { prefix = typeName + "("; @@ -188,7 +188,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { JCExpression current = maker.Literal(prefix); - if ( callSuper ) { + if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil()); @@ -196,11 +196,11 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { first = false; } - for ( Node fieldNode : fields ) { + for (JavacNode fieldNode : fields) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); JCExpression expr; - if ( field.vartype instanceof JCArrayTypeTree ) { + if (field.vartype instanceof JCArrayTypeTree) { boolean multiDim = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCArrayTypeTree; boolean primitiveArray = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCPrimitiveTypeTree; boolean useDeepTS = multiDim || !primitiveArray; @@ -209,13 +209,13 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { expr = maker.Apply(List.<JCExpression>nil(), hcMethod, List.<JCExpression>of(maker.Ident(field.name))); } else expr = maker.Ident(field.name); - if ( first ) { + if (first) { current = maker.Binary(JCTree.PLUS, current, expr); first = false; continue; } - if ( includeFieldNames ) { + if (includeFieldNames) { current = maker.Binary(JCTree.PLUS, current, maker.Literal(infix + fieldNode.getName() + "=")); } else { current = maker.Binary(JCTree.PLUS, current, maker.Literal(infix)); @@ -224,7 +224,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> { current = maker.Binary(JCTree.PLUS, current, expr); } - if ( !first ) current = maker.Binary(JCTree.PLUS, current, maker.Literal(suffix)); + if (!first) current = maker.Binary(JCTree.PLUS, current, maker.Literal(suffix)); JCStatement returnStatement = maker.Return(current); diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 0563f33c..e2fceb08 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -27,8 +27,7 @@ import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; -import lombok.javac.JavacAST; -import lombok.javac.JavacAST.Node; +import lombok.javac.JavacNode; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.TypeTags; @@ -95,17 +94,17 @@ class PKG { * @param fieldName the field name to check for. * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof. */ - static MemberExistsResult fieldExists(String fieldName, JavacAST.Node node) { - while ( node != null && !(node.get() instanceof JCClassDecl) ) { + static MemberExistsResult fieldExists(String fieldName, JavacNode node) { + while (node != null && !(node.get() instanceof JCClassDecl)) { node = node.up(); } - if ( node != null && node.get() instanceof JCClassDecl ) { - for ( JCTree def : ((JCClassDecl)node.get()).defs ) { - if ( def instanceof JCVariableDecl ) { - if ( ((JCVariableDecl)def).name.contentEquals(fieldName) ) { - JavacAST.Node existing = node.getNodeFor(def); - if ( existing == null || !existing.isHandled() ) return MemberExistsResult.EXISTS_BY_USER; + if (node != null && node.get() instanceof JCClassDecl) { + for (JCTree def : ((JCClassDecl)node.get()).defs) { + if (def instanceof JCVariableDecl) { + if (((JCVariableDecl)def).name.contentEquals(fieldName)) { + JavacNode existing = node.getNodeFor(def); + if (existing == null || !existing.isHandled()) return MemberExistsResult.EXISTS_BY_USER; return MemberExistsResult.EXISTS_BY_LOMBOK; } } @@ -122,17 +121,17 @@ class PKG { * @param methodName the method name to check for. * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof. */ - static MemberExistsResult methodExists(String methodName, JavacAST.Node node) { - while ( node != null && !(node.get() instanceof JCClassDecl) ) { + static MemberExistsResult methodExists(String methodName, JavacNode node) { + while (node != null && !(node.get() instanceof JCClassDecl)) { node = node.up(); } - if ( node != null && node.get() instanceof JCClassDecl ) { - for ( JCTree def : ((JCClassDecl)node.get()).defs ) { - if ( def instanceof JCMethodDecl ) { - if ( ((JCMethodDecl)def).name.contentEquals(methodName) ) { - JavacAST.Node existing = node.getNodeFor(def); - if ( existing == null || !existing.isHandled() ) return MemberExistsResult.EXISTS_BY_USER; + if (node != null && node.get() instanceof JCClassDecl) { + for (JCTree def : ((JCClassDecl)node.get()).defs) { + if (def instanceof JCMethodDecl) { + if (((JCMethodDecl)def).name.contentEquals(methodName)) { + JavacNode existing = node.getNodeFor(def); + if (existing == null || !existing.isHandled()) return MemberExistsResult.EXISTS_BY_USER; return MemberExistsResult.EXISTS_BY_LOMBOK; } } @@ -148,18 +147,18 @@ class PKG { * * @param node Any node that represents the Type (JCClassDecl) to check for, or any child node thereof. */ - static MemberExistsResult constructorExists(JavacAST.Node node) { - while ( node != null && !(node.get() instanceof JCClassDecl) ) { + static MemberExistsResult constructorExists(JavacNode node) { + while (node != null && !(node.get() instanceof JCClassDecl)) { node = node.up(); } - if ( node != null && node.get() instanceof JCClassDecl ) { - for ( JCTree def : ((JCClassDecl)node.get()).defs ) { - if ( def instanceof JCMethodDecl ) { - if ( ((JCMethodDecl)def).name.contentEquals("<init>") ) { - if ( (((JCMethodDecl)def).mods.flags & Flags.GENERATEDCONSTR) != 0 ) continue; - JavacAST.Node existing = node.getNodeFor(def); - if ( existing == null || !existing.isHandled() ) return MemberExistsResult.EXISTS_BY_USER; + if (node != null && node.get() instanceof JCClassDecl) { + for (JCTree def : ((JCClassDecl)node.get()).defs) { + if (def instanceof JCMethodDecl) { + if (((JCMethodDecl)def).name.contentEquals("<init>")) { + if ((((JCMethodDecl)def).mods.flags & Flags.GENERATEDCONSTR) != 0) continue; + JavacNode existing = node.getNodeFor(def); + if (existing == null || !existing.isHandled()) return MemberExistsResult.EXISTS_BY_USER; return MemberExistsResult.EXISTS_BY_LOMBOK; } } @@ -175,7 +174,7 @@ class PKG { * @see java.lang.Modifier */ static int toJavacModifier(AccessLevel accessLevel) { - switch ( accessLevel ) { + switch (accessLevel) { case MODULE: case PACKAGE: return 0; @@ -194,7 +193,7 @@ class PKG { * * Also takes care of updating the JavacAST. */ - static void injectField(JavacAST.Node typeNode, JCVariableDecl field) { + static void injectField(JavacNode typeNode, JCVariableDecl field) { JCClassDecl type = (JCClassDecl) typeNode.get(); type.defs = type.defs.append(field); @@ -208,17 +207,17 @@ class PKG { * * Also takes care of updating the JavacAST. */ - static void injectMethod(JavacAST.Node typeNode, JCMethodDecl method) { + static void injectMethod(JavacNode typeNode, JCMethodDecl method) { JCClassDecl type = (JCClassDecl) typeNode.get(); - if ( method.getName().contentEquals("<init>") ) { + if (method.getName().contentEquals("<init>")) { //Scan for default constructor, and remove it. int idx = 0; - for ( JCTree def : type.defs ) { - if ( def instanceof JCMethodDecl ) { - if ( (((JCMethodDecl)def).mods.flags & Flags.GENERATEDCONSTR) != 0 ) { - JavacAST.Node tossMe = typeNode.getNodeFor(def); - if ( tossMe != null ) tossMe.up().removeChild(tossMe); + for (JCTree def : type.defs) { + if (def instanceof JCMethodDecl) { + if ((((JCMethodDecl)def).mods.flags & Flags.GENERATEDCONSTR) != 0) { + JavacNode tossMe = typeNode.getNodeFor(def); + if (tossMe != null) tossMe.up().removeChild(tossMe); type.defs = addAllButOne(type.defs, idx); break; } @@ -235,8 +234,8 @@ class PKG { private static List<JCTree> addAllButOne(List<JCTree> defs, int idx) { List<JCTree> out = List.nil(); int i = 0; - for ( JCTree def : defs ) { - if ( i++ != idx ) out = out.append(def); + for (JCTree def : defs) { + if (i++ != idx) out = out.append(def); } return out; } @@ -251,22 +250,22 @@ class PKG { * @see com.sun.tools.javac.tree.JCTree.JCIdent * @see com.sun.tools.javac.tree.JCTree.JCFieldAccess */ - static JCExpression chainDots(TreeMaker maker, JavacAST.Node node, String... elems) { + static JCExpression chainDots(TreeMaker maker, JavacNode node, String... elems) { assert elems != null; assert elems.length > 0; JCExpression e = maker.Ident(node.toName(elems[0])); - for ( int i = 1 ; i < elems.length ; i++ ) { + for (int i = 1 ; i < elems.length ; i++) { e = maker.Select(e, node.toName(elems[i])); } return e; } - static List<JCAnnotation> findAnnotations(Node fieldNode, Pattern namePattern) { + static List<JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern) { List<JCAnnotation> result = List.nil(); - for ( Node child : fieldNode.down() ) { - if ( child.getKind() == Kind.ANNOTATION ) { + for (JavacNode child : fieldNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { JCAnnotation annotation = (JCAnnotation) child.get(); String name = annotation.annotationType.toString(); int idx = name.lastIndexOf("."); @@ -279,7 +278,7 @@ class PKG { return result; } - static JCStatement generateNullCheck(TreeMaker treeMaker, JavacAST.Node variable) { + static JCStatement generateNullCheck(TreeMaker treeMaker, JavacNode variable) { JCVariableDecl varDecl = (JCVariableDecl) variable.get(); if (isPrimitive(varDecl.vartype)) return null; Name fieldName = varDecl.name; @@ -289,29 +288,28 @@ class PKG { return treeMaker.If(treeMaker.Binary(JCTree.EQ, treeMaker.Ident(fieldName), treeMaker.Literal(TypeTags.BOT, null)), throwStatement, null); } - static List<Integer> createListOfNonExistentFields(List<String> list, Node type, boolean excludeStandard, boolean excludeTransient) { + static List<Integer> createListOfNonExistentFields(List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient) { boolean[] matched = new boolean[list.size()]; - for ( Node child : type.down() ) { - if ( list.isEmpty() ) break; - if ( child.getKind() != Kind.FIELD ) continue; + for (JavacNode child : type.down()) { + if (list.isEmpty()) break; + if (child.getKind() != Kind.FIELD) continue; JCVariableDecl field = (JCVariableDecl)child.get(); - if ( excludeStandard ) { - if ( (field.mods.flags & Flags.STATIC) != 0 ) continue; - if ( field.name.toString().startsWith("$") ) continue; + if (excludeStandard) { + if ((field.mods.flags & Flags.STATIC) != 0) continue; + if (field.name.toString().startsWith("$")) continue; } - if ( excludeTransient && (field.mods.flags & Flags.TRANSIENT) != 0 ) continue; - + if (excludeTransient && (field.mods.flags & Flags.TRANSIENT) != 0) continue; + int idx = list.indexOf(child.getName()); - if ( idx > -1 ) matched[idx] = true; + if (idx > -1) matched[idx] = true; } List<Integer> problematic = List.nil(); - for ( int i = 0 ; i < list.size() ; i++ ) { - if ( !matched[i] ) problematic = problematic.append(i); + for (int i = 0 ; i < list.size() ; i++) { + if (!matched[i]) problematic = problematic.append(i); } return problematic; } - } |