From 5c6fe62a854ef6c98920bc90e71664238a7b2f84 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 19 Jul 2010 23:48:12 +0200 Subject: Added support for @ConstructorProperties generation for generated constructors. Also added fix: @Constructor with access level none are now no longer generated. Implements issue #122 --- .../lombok/javac/handlers/HandleConstructor.java | 50 ++++++++++++++++------ src/core/lombok/javac/handlers/HandleData.java | 2 +- 2 files changed, 38 insertions(+), 14 deletions(-) (limited to 'src/core/lombok/javac') diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index 2f4a8474..4331761f 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -59,11 +59,12 @@ public class HandleConstructor { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, NoArgsConstructor.class); JavacNode typeNode = annotationNode.up(); - List fields = List.nil(); NoArgsConstructor ann = annotation.getInstance(); AccessLevel level = ann.access(); String staticName = ann.staticName(); - new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false); + if (level == AccessLevel.NONE) return true; + List fields = List.nil(); + new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false, false); return true; } } @@ -73,6 +74,12 @@ public class HandleConstructor { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, RequiredArgsConstructor.class); JavacNode typeNode = annotationNode.up(); + RequiredArgsConstructor ann = annotation.getInstance(); + AccessLevel level = ann.access(); + String staticName = ann.staticName(); + @SuppressWarnings("deprecation") + boolean suppressConstructorProperties = ann.suppressConstructorProperties(); + if (level == AccessLevel.NONE) return true; List fields = List.nil(); for (JavacNode child : typeNode.down()) { if (child.getKind() != Kind.FIELD) continue; @@ -86,10 +93,7 @@ public class HandleConstructor { boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty(); if ((isFinal || isNonNull) && fieldDecl.init == null) fields = fields.append(child); } - RequiredArgsConstructor ann = annotation.getInstance(); - AccessLevel level = ann.access(); - String staticName = ann.staticName(); - new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false); + new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false, suppressConstructorProperties); return true; } } @@ -99,6 +103,12 @@ public class HandleConstructor { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, AllArgsConstructor.class); JavacNode typeNode = annotationNode.up(); + AllArgsConstructor ann = annotation.getInstance(); + AccessLevel level = ann.access(); + String staticName = ann.staticName(); + @SuppressWarnings("deprecation") + boolean suppressConstructorProperties = ann.suppressConstructorProperties(); + if (level == AccessLevel.NONE) return true; List fields = List.nil(); for (JavacNode child : typeNode.down()) { if (child.getKind() != Kind.FIELD) continue; @@ -110,15 +120,12 @@ public class HandleConstructor { if ((fieldFlags & Flags.STATIC) != 0) continue; fields = fields.append(child); } - AllArgsConstructor ann = annotation.getInstance(); - AccessLevel level = ann.access(); - String staticName = ann.staticName(); - new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false); + new HandleConstructor().generateConstructor(level, typeNode, fields, staticName, false, suppressConstructorProperties); return true; } } - public void generateConstructor(AccessLevel level, JavacNode typeNode, List fields, String staticName, boolean skipIfConstructorExists) { + public void generateConstructor(AccessLevel level, JavacNode typeNode, List fields, String staticName, boolean skipIfConstructorExists, boolean suppressConstructorProperties) { if (skipIfConstructorExists && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return; if (skipIfConstructorExists) { for (JavacNode child : typeNode.down()) { @@ -133,7 +140,7 @@ public class HandleConstructor { boolean staticConstrRequired = staticName != null && !staticName.equals(""); - JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields); + JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields, suppressConstructorProperties); injectMethod(typeNode, constr); if (staticConstrRequired) { JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, fields); @@ -141,7 +148,20 @@ public class HandleConstructor { } } - private JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, List fields) { + private static void addConstructorProperties(JCModifiers mods, JavacNode node, List fields) { + if (fields.isEmpty()) return; + TreeMaker maker = node.getTreeMaker(); + JCExpression constructorPropertiesType = chainDots(maker, node, "java", "beans", "ConstructorProperties"); + List fieldNames = List.nil(); + for (JavacNode field : fields) { + fieldNames = fieldNames.append(maker.Literal(field.getName())); + } + JCExpression fieldNamesArray = maker.NewArray(null, List.nil(), fieldNames); + JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray)); + mods.annotations = mods.annotations.append(annotation); + } + + private JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, List fields, boolean suppressConstructorProperties) { TreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -166,6 +186,10 @@ public class HandleConstructor { } JCModifiers mods = maker.Modifiers(toJavacModifier(level)); + if (!suppressConstructorProperties && level != AccessLevel.PRIVATE) { + addConstructorProperties(mods, typeNode, fields); + } + return maker.MethodDef(mods, typeNode.toName(""), null, type.typarams, params, List.nil(), maker.Block(0L, nullChecks.appendList(assigns)), null); } diff --git a/src/core/lombok/javac/handlers/HandleData.java b/src/core/lombok/javac/handlers/HandleData.java index 3bdd2dce..e20b230e 100644 --- a/src/core/lombok/javac/handlers/HandleData.java +++ b/src/core/lombok/javac/handlers/HandleData.java @@ -78,7 +78,7 @@ public class HandleData implements JavacAnnotationHandler { String staticConstructorName = annotation.getInstance().staticConstructor(); - new HandleConstructor().generateConstructor(AccessLevel.PUBLIC, typeNode, nodesForConstructor, staticConstructorName, true); + new HandleConstructor().generateConstructor(AccessLevel.PUBLIC, typeNode, nodesForConstructor, staticConstructorName, true, false); for (Map.Entry field : gettersAndSetters.entrySet()) { new HandleGetter().generateGetterForField(field.getKey(), annotationNode.get(), AccessLevel.PUBLIC, true); -- cgit