From f1124aad02569c983cb8979445245141bf029a88 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 3 Sep 2009 01:44:59 +0200 Subject: Addressed issue #32: The @EqualsAndHashCode and @ToString annotations now support explicitly listing the fields to use, via the new 'of' parameter. We've also added any fields that start with $ to the default excludes list. Lombok itself can generate these fields ($lock of @Synchronized, for example), and in general they probably should count as effectively not part of the class. --- .../javac/handlers/HandleEqualsAndHashCode.java | 83 ++++++++++++---------- src/lombok/javac/handlers/HandleToString.java | 77 +++++++++++--------- src/lombok/javac/handlers/PKG.java | 26 +++++++ 3 files changed, 117 insertions(+), 69 deletions(-) (limited to 'src/lombok/javac') diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java index 8c3124c9..16a04da7 100644 --- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -22,7 +22,6 @@ package lombok.javac.handlers; import static lombok.javac.handlers.PKG.*; - import lombok.EqualsAndHashCode; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; @@ -59,35 +58,33 @@ import com.sun.tools.javac.util.Name; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleEqualsAndHashCode implements JavacAnnotationHandler { - private void checkForBogusExcludes(Node type, AnnotationValues annotation) { - List list = List.from(annotation.getInstance().exclude()); - boolean[] matched = new boolean[list.size()]; - - for ( Node child : type.down() ) { - if ( list.isEmpty() ) break; - if ( child.getKind() != Kind.FIELD ) continue; - if ( (((JCVariableDecl)child.get()).mods.flags & Flags.STATIC) != 0 ) continue; - if ( (((JCVariableDecl)child.get()).mods.flags & Flags.TRANSIENT) != 0 ) continue; - int idx = list.indexOf(child.getName()); - if ( idx > -1 ) matched[idx] = true; - } - - for ( int i = 0 ; i < list.size() ; i++ ) { - if ( !matched[i] ) { + private void checkForBogusFieldNames(Node type, AnnotationValues 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) ) { + annotation.setWarning("of", "This field does not exist.", i); + } + } } @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, Node annotationNode) { EqualsAndHashCode ann = annotation.getInstance(); List excludes = List.from(ann.exclude()); + List includes = List.from(ann.of()); Node typeNode = annotationNode.up(); - checkForBogusExcludes(typeNode, annotation); + checkForBogusFieldNames(typeNode, annotation); - return generateMethods(typeNode, annotationNode, excludes, - ann.callSuper(), annotation.getRawExpression("callSuper") == null, true); + Boolean callSuper = ann.callSuper(); + if ( !annotation.isExplicit("callSuper") ) callSuper = null; + if ( !annotation.isExplicit("exclude") ) excludes = null; + if ( !annotation.isExplicit("of") ) includes = null; + + return generateMethods(typeNode, annotationNode, excludes, includes, callSuper, true); } public void generateEqualsAndHashCodeForType(Node typeNode, Node errorNode) { @@ -100,15 +97,11 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandlernil(), callSuper, true, false); + generateMethods(typeNode, errorNode, null, null, null, false); } - private boolean generateMethods(Node typeNode, Node errorNode, List excludes, - boolean callSuper, boolean implicit, boolean whineIfExists) { + private boolean generateMethods(Node typeNode, Node errorNode, List excludes, List includes, + Boolean callSuper, boolean whineIfExists) { boolean notAClass = true; if ( typeNode.get() instanceof JCClassDecl ) { long flags = ((JCClassDecl)typeNode.get()).mods.flags; @@ -121,6 +114,12 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler nodesForEquality = List.nil(); - for ( Node 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; - //Skip transient fields. - if ( (fieldDecl.mods.flags & Flags.TRANSIENT) != 0 ) continue; - //Skip excluded fields. - if ( excludes.contains(fieldDecl.name.toString()) ) continue; - nodesForEquality = nodesForEquality.append(child); + if ( includes != null ) { + for ( Node child : typeNode.down() ) { + if ( child.getKind() != Kind.FIELD ) continue; + JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); + if ( includes.contains(fieldDecl.name.toString()) ) nodesForEquality = nodesForEquality.append(child); + } + } else { + for ( Node 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; + //Skip transient fields. + if ( (fieldDecl.mods.flags & Flags.TRANSIENT) != 0 ) continue; + //Skip excluded fields. + if ( excludes != null && excludes.contains(fieldDecl.name.toString()) ) continue; + //Skip fields that start with $ + if ( fieldDecl.name.toString().startsWith("$") ) continue; + nodesForEquality = nodesForEquality.append(child); + } } switch ( methodExists("hashCode", typeNode) ) { diff --git a/src/lombok/javac/handlers/HandleToString.java b/src/lombok/javac/handlers/HandleToString.java index 49f0d15c..7cdbb6e1 100644 --- a/src/lombok/javac/handlers/HandleToString.java +++ b/src/lombok/javac/handlers/HandleToString.java @@ -54,33 +54,34 @@ import com.sun.tools.javac.util.List; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleToString implements JavacAnnotationHandler { - private void checkForBogusExcludes(Node type, AnnotationValues annotation) { - List list = List.from(annotation.getInstance().exclude()); - boolean[] matched = new boolean[list.size()]; - - for ( Node child : type.down() ) { - if ( list.isEmpty() ) break; - if ( child.getKind() != Kind.FIELD ) continue; - if ( (((JCVariableDecl)child.get()).mods.flags & Flags.STATIC) != 0 ) continue; - int idx = list.indexOf(child.getName()); - if ( idx > -1 ) matched[idx] = true; - } - - for ( int i = 0 ; i < list.size() ; i++ ) { - if ( !matched[i] ) { + private void checkForBogusFieldNames(Node type, AnnotationValues 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) ) { + annotation.setWarning("of", "This field does not exist.", i); + } + } } @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, Node annotationNode) { ToString ann = annotation.getInstance(); List excludes = List.from(ann.exclude()); + List includes = List.from(ann.of()); Node typeNode = annotationNode.up(); - checkForBogusExcludes(typeNode, annotation); + checkForBogusFieldNames(typeNode, annotation); - return generateToString(typeNode, annotationNode, excludes, ann.includeFieldNames(), ann.callSuper(), true); + Boolean callSuper = ann.callSuper(); + + if ( !annotation.isExplicit("callSuper") ) callSuper = null; + if ( !annotation.isExplicit("exclude") ) excludes = null; + if ( !annotation.isExplicit("of") ) includes = null; + + return generateToString(typeNode, annotationNode, excludes, includes, ann.includeFieldNames(), callSuper, true); } public void generateToStringForType(Node typeNode, Node errorNode) { @@ -93,39 +94,51 @@ public class HandleToString implements JavacAnnotationHandler { } } - boolean includeFieldNames = false; - boolean callSuper = false; + boolean includeFieldNames = true; try { includeFieldNames = ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue(); } catch ( Exception ignore ) {} - try { - callSuper = ((Boolean)ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue(); - } catch ( Exception ignore ) {} - generateToString(typeNode, errorNode, List.nil(), includeFieldNames, callSuper, false); + generateToString(typeNode, errorNode, null, null, includeFieldNames, null, false); } - private boolean generateToString(Node typeNode, Node errorNode, List excludes, - boolean includeFieldNames, boolean callSuper, boolean whineIfExists) { + private boolean generateToString(Node typeNode, Node errorNode, List excludes, List includes, + boolean includeFieldNames, Boolean callSuper, boolean whineIfExists) { boolean notAClass = true; if ( typeNode.get() instanceof JCClassDecl ) { long flags = ((JCClassDecl)typeNode.get()).mods.flags; notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; } + if ( callSuper == null ) { + try { + callSuper = ((Boolean)ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue(); + } catch ( Exception ignore ) {} + } + if ( notAClass ) { errorNode.addError("@ToString is only supported on a class."); return false; } List nodesForToString = List.nil(); - for ( Node 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; - //Skip excluded fields. - if ( excludes.contains(fieldDecl.name.toString()) ) continue; - nodesForToString = nodesForToString.append(child); + if ( includes != null ) { + for ( Node child : typeNode.down() ) { + if ( child.getKind() != Kind.FIELD ) continue; + JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); + if ( includes.contains(fieldDecl.name.toString()) ) nodesForToString = nodesForToString.append(child); + } + } else { + for ( Node 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; + //Skip excluded fields. + if ( excludes != null && excludes.contains(fieldDecl.name.toString()) ) continue; + //Skip fields that start with $. + if ( fieldDecl.name.toString().startsWith("$") ) continue; + nodesForToString = nodesForToString.append(child); + } } switch ( methodExists("toString", typeNode) ) { diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 4b05b9ae..0563f33c 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -288,4 +288,30 @@ class PKG { JCStatement throwStatement = treeMaker.Throw(exception); return treeMaker.If(treeMaker.Binary(JCTree.EQ, treeMaker.Ident(fieldName), treeMaker.Literal(TypeTags.BOT, null)), throwStatement, null); } + + static List createListOfNonExistentFields(List list, Node 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; + JCVariableDecl field = (JCVariableDecl)child.get(); + 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; + + int idx = list.indexOf(child.getName()); + if ( idx > -1 ) matched[idx] = true; + } + + List problematic = List.nil(); + for ( int i = 0 ; i < list.size() ; i++ ) { + if ( !matched[i] ) problematic = problematic.append(i); + } + + return problematic; + } + } -- cgit