Added annotation for handleconstructor: issue 172

This commit is contained in:
grootjans committed 2011-02-14 22:51:53 +01:00
1 parent b15bcb09e9
commit 3a6615234d
5 files changed
+70 -22

No files matched your search

+15 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright © 2010 Reinier Zwitserloot and Roel Spilker.
* Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -59,4 +59,18 @@ public @interface AllArgsConstructor {
*/
@Deprecated
boolean suppressConstructorProperties() default false;
/**
* If you want your constructor to have additional annotations, you can specify them here.
*/
AnyAnnotation[] onConstructor() default {};
/**
* Placeholder annotation to enable the placement of annotations on the constructor.
* @deprecated Don't use this annotation, since we might remove it.
*/
@Deprecated
@Retention(RetentionPolicy.SOURCE)
@Target({})
@interface AnyAnnotation {}
}
+15 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright © 2010 Reinier Zwitserloot and Roel Spilker.
* Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -51,4 +51,18 @@ public @interface NoArgsConstructor {
* Sets the access level of the constructor. By default, generated constructors are {@code public}.
*/
AccessLevel access() default lombok.AccessLevel.PUBLIC;
/**
* If you want your constructor to have additional annotations, you can specify them here.
*/
AnyAnnotation[] onConstructor() default {};
/**
* Placeholder annotation to enable the placement of annotations on the constructor.
* @deprecated Don't use this annotation, since we might remove it.
*/
@Deprecated
@Retention(RetentionPolicy.SOURCE)
@Target({})
@interface AnyAnnotation {}
}
+15 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright © 2010 Reinier Zwitserloot and Roel Spilker.
* Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -59,4 +59,18 @@ public @interface RequiredArgsConstructor {
*/
@Deprecated
boolean suppressConstructorProperties() default false;
/**
* If you want your constructor to have additional annotations, you can specify them here.
*/
AnyAnnotation[] onConstructor() default {};
/**
* Placeholder annotation to enable the placement of annotations on the constructor.
* @deprecated Don't use this annotation, since we might remove it.
*/
@Deprecated
@Retention(RetentionPolicy.SOURCE)
@Target({})
@interface AnyAnnotation {}
}
@@ -1,5 +1,5 @@
/*
* Copyright © 2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
* Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -81,7 +81,8 @@ public class HandleConstructor {
String staticName = ann.staticName();
if (level == AccessLevel.NONE) return true;
List<EclipseNode> fields = new ArrayList<EclipseNode>();
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, false, ast);
Annotation[] onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, onConstructor, false, false, ast);
return true;
}
}
@@ -97,7 +98,8 @@ public class HandleConstructor {
@SuppressWarnings("deprecation")
boolean suppressConstructorProperties = ann.suppressConstructorProperties();
if (level == AccessLevel.NONE) return true;
new HandleConstructor().generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, false, suppressConstructorProperties, ast);
Annotation[] onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, onConstructor, false, suppressConstructorProperties, ast);
return true;
}
}
@@ -137,7 +139,8 @@ public class HandleConstructor {
fields.add(child);
}
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, suppressConstructorProperties, ast);
Annotation[] onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, onConstructor, false, suppressConstructorProperties, ast);
return true;
}
}
@@ -157,10 +160,10 @@ public class HandleConstructor {
}
public void generateRequiredArgsConstructor(EclipseNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists, ASTNode source) {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false, source);
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, null, skipIfConstructorExists, false, source);
}
public void generateConstructor(EclipseNode typeNode, AccessLevel level, List<EclipseNode> fields, String staticName, boolean skipIfConstructorExists, boolean suppressConstructorProperties, ASTNode source) {
public void generateConstructor(EclipseNode typeNode, AccessLevel level, List<EclipseNode> fields, String staticName, Annotation[] onConstructor, boolean skipIfConstructorExists, boolean suppressConstructorProperties, ASTNode source) {
if (skipIfConstructorExists && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
if (skipIfConstructorExists) {
for (EclipseNode child : typeNode.down()) {
@@ -175,7 +178,7 @@ public class HandleConstructor {
boolean staticConstrRequired = staticName != null && !staticName.equals("");
ConstructorDeclaration constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields, suppressConstructorProperties, source);
ConstructorDeclaration constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields, suppressConstructorProperties, onConstructor, source);
injectMethod(typeNode, constr);
if (staticConstrRequired) {
MethodDeclaration staticConstr = createStaticConstructor(level, staticName, typeNode, fields, source);
@@ -218,7 +221,7 @@ public class HandleConstructor {
}
private ConstructorDeclaration createConstructor(AccessLevel level,
EclipseNode type, Collection<EclipseNode> fields, boolean suppressConstructorProperties, ASTNode source) {
EclipseNode type, Collection<EclipseNode> fields, boolean suppressConstructorProperties, Annotation[] onConstructor, ASTNode source) {
long p = (long)source.sourceStart << 32 | source.sourceEnd;
boolean isEnum = (((TypeDeclaration)type.get()).modifiers & ClassFileConstants.AccEnum) != 0;
@@ -230,7 +233,7 @@ public class HandleConstructor {
Eclipse.setGeneratedBy(constructor, source);
constructor.modifiers = EclipseHandlerUtil.toEclipseModifier(level);
constructor.annotations = null;
constructor.annotations = onConstructor;
constructor.selector = ((TypeDeclaration)type.get()).name;
constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
Eclipse.setGeneratedBy(constructor.constructorCall, source);
@@ -1,5 +1,5 @@
/*
* Copyright © 2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
* Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -68,7 +68,8 @@ public class HandleConstructor {
String staticName = ann.staticName();
if (level == AccessLevel.NONE) return true;
List<JavacNode> fields = List.nil();
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, false);
List<JCExpression> onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, onConstructor, false, false);
return true;
}
@@ -90,7 +91,8 @@ public class HandleConstructor {
@SuppressWarnings("deprecation")
boolean suppressConstructorProperties = ann.suppressConstructorProperties();
if (level == AccessLevel.NONE) return true;
new HandleConstructor().generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, false, suppressConstructorProperties);
List<JCExpression> onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, onConstructor, false, suppressConstructorProperties);
return true;
}
@@ -142,7 +144,8 @@ public class HandleConstructor {
if (((fieldFlags & Flags.FINAL) != 0) && fieldDecl.init != null) continue;
fields.append(child);
}
new HandleConstructor().generateConstructor(typeNode, level, fields.toList(), staticName, false, suppressConstructorProperties);
List<JCExpression> onConstructor = getAndRemoveAnnotationParameter(ast, "onConstructor");
new HandleConstructor().generateConstructor(typeNode, level, fields.toList(), staticName, onConstructor, false, suppressConstructorProperties);
return true;
}
@@ -166,10 +169,10 @@ public class HandleConstructor {
}
public void generateRequiredArgsConstructor(JavacNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists) {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false);
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, List.<JCExpression>nil(), skipIfConstructorExists, false);
}
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JavacNode> fields, String staticName, boolean skipIfConstructorExists, boolean suppressConstructorProperties) {
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JavacNode> fields, String staticName, List<JCExpression> onConstructor, boolean skipIfConstructorExists, boolean suppressConstructorProperties) {
if (skipIfConstructorExists && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS) return;
if (skipIfConstructorExists) {
for (JavacNode child : typeNode.down()) {
@@ -184,7 +187,7 @@ public class HandleConstructor {
boolean staticConstrRequired = staticName != null && !staticName.equals("");
JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields, suppressConstructorProperties);
JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, typeNode, fields, onConstructor, suppressConstructorProperties);
injectMethod(typeNode, constr);
if (staticConstrRequired) {
JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, fields);
@@ -205,7 +208,7 @@ public class HandleConstructor {
mods.annotations = mods.annotations.append(annotation);
}
private JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, List<JavacNode> fields, boolean suppressConstructorProperties) {
private JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, List<JavacNode> fields, List<JCExpression> onConstructor, boolean suppressConstructorProperties) {
TreeMaker maker = typeNode.getTreeMaker();
boolean isEnum = (((JCClassDecl) typeNode.get()).mods.flags & Flags.ENUM) != 0;
@@ -231,11 +234,11 @@ public class HandleConstructor {
}
}
JCModifiers mods = maker.Modifiers(toJavacModifier(level));
List<JCAnnotation> annsOnConstructor = copyAnnotations(onConstructor);
JCModifiers mods = maker.Modifiers(toJavacModifier(level), annsOnConstructor);
if (!suppressConstructorProperties && level != AccessLevel.PRIVATE && !isLocalType(typeNode)) {
addConstructorProperties(mods, typeNode, fields);
}
return maker.MethodDef(mods, typeNode.toName("<init>"),
null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null);
}