From 3d32cd85f174bbeb54ea0c102e029c42daf51931 Mon Sep 17 00:00:00 2001 From: Christian Sterzl Date: Thu, 3 Apr 2014 08:11:16 +0200 Subject: Adding onParam to annotation @EqualsAndHashCode to add individual annotations to the parameters of equals and canEqual. --- src/core/lombok/EqualsAndHashCode.java | 15 +++++++++++++++ .../javac/handlers/HandleEqualsAndHashCode.java | 20 ++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index 60ed9e7a..de9c43ba 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -61,4 +61,19 @@ public @interface EqualsAndHashCode { * default: false */ boolean doNotUseGetters() default false; + + /** + * Any annotations listed here are put on the generated parameter of {@code equals} and {@code canEqual}. The syntax for this feature is: {@code @EqualsAndHashCode(onParam=@__({@AnnotationsGoHere}))} + * This is useful to add for example a {@code Nullable} annotation. + */ + AnyAnnotation[] onParam() default @AnyAnnotation; + + /** + * Placeholder annotation to enable the placement of annotations on the generated code. + * @deprecated Don't use this annotation, ever - Read the documentation. + */ + @Deprecated + @Retention(RetentionPolicy.SOURCE) + @Target({}) + @interface AnyAnnotation {} } diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 05244f3b..e62b0555 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -94,7 +94,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler excludes = List.from(ann.exclude()); List includes = List.from(ann.of()); JavacNode typeNode = annotationNode.up(); - + List onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@EqualsAndHashCode(onParam=", annotationNode); checkForBogusFieldNames(typeNode, annotation); Boolean callSuper = ann.callSuper(); @@ -109,7 +109,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler excludes, List includes, - Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) { + Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, List onParam) { boolean notAClass = true; if (typeNode.get() instanceof JCClassDecl) { long flags = ((JCClassDecl)typeNode.get()).mods.flags; @@ -210,11 +210,11 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler fields, boolean callSuper, FieldAccess fieldAccess, boolean needsCanEqual, JCTree source) { + public JCMethodDecl createEquals(JavacNode typeNode, List fields, boolean callSuper, FieldAccess fieldAccess, boolean needsCanEqual, JCTree source, List onParam) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -379,7 +379,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler statements = new ListBuffer(); - final List params = List.of(maker.VarDef(maker.Modifiers(finalFlag | Flags.PARAMETER), oName, objectType, null)); + final List params = List.of(maker.VarDef(maker.Modifiers(finalFlag | Flags.PARAMETER, onParam), oName, objectType, null)); /* if (o == this) return true; */ { statements.append(maker.If(maker.Binary(CTC_EQUAL, maker.Ident(oName), @@ -497,7 +497,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandlernil(), params, List.nil(), body, null), source, typeNode.getContext()); } - public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) { + public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List onParam) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } @@ -510,7 +510,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler params = List.of(maker.VarDef(maker.Modifiers(flags), otherName, objectType, null)); + List params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null)); JCBlock body = maker.Block(0, List.of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); -- cgit