aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2010-12-28 16:50:19 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2010-12-28 16:51:02 +0100
commit52582dc10ea1b5ea4a1dbb71831a442056354990 (patch)
treeafd0794b7360e5c174a79a54c54a50aacfe31efc /src
parentd80139fa8f21b1e6c7fad0a0607986d7b246a06f (diff)
downloadlombok-52582dc10ea1b5ea4a1dbb71831a442056354990.tar.gz
lombok-52582dc10ea1b5ea4a1dbb71831a442056354990.tar.bz2
lombok-52582dc10ea1b5ea4a1dbb71831a442056354990.zip
@ToString, @Getter, and @XArgsConstructor now work on, and are legal on, enums. Docs have been updated.
Behaviour of @XArgsConstructor when its placement makes no sense (i.e. when annotating an interface with them) is no longer 'throw weird errors', but has been brought in line with the others: A nice error message is generated. Fixes issue #175
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java18
-rw-r--r--src/core/lombok/eclipse/handlers/HandleConstructor.java35
-rw-r--r--src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java6
-rw-r--r--src/core/lombok/eclipse/handlers/HandleGetter.java10
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSetter.java6
-rw-r--r--src/core/lombok/eclipse/handlers/HandleToString.java11
-rw-r--r--src/core/lombok/javac/handlers/HandleConstructor.java17
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java4
-rw-r--r--src/core/lombok/javac/handlers/HandleToString.java4
9 files changed, 77 insertions, 34 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 4097362b..293359dc 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -294,6 +294,24 @@ public class EclipseHandlerUtil {
}
/**
+ * Checks if the field should be included in operations that work on 'all' fields:
+ * If the field is static, or starts with a '$', or is actually an enum constant, 'false' is returned, indicating you should skip it.
+ */
+ public static boolean filterField(FieldDeclaration declaration) {
+ // Skip the fake fields that represent enum constants.
+ if (declaration.initialization instanceof AllocationExpression &&
+ ((AllocationExpression)declaration.initialization).enumConstant != null) return false;
+
+ // Skip fields that start with $
+ if (declaration.name.length > 0 && declaration.name[0] == '$') return false;
+
+ // Skip static fields.
+ if ((declaration.modifiers & ClassFileConstants.AccStatic) != 0) return false;
+
+ return true;
+ }
+
+ /**
* Checks if there is a field with the provided name.
*
* @param fieldName the field name to check for.
diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java
index f5025b19..db071329 100644
--- a/src/core/lombok/eclipse/handlers/HandleConstructor.java
+++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java
@@ -75,6 +75,7 @@ public class HandleConstructor {
public static class HandleNoArgsConstructor implements EclipseAnnotationHandler<NoArgsConstructor> {
@Override public boolean handle(AnnotationValues<NoArgsConstructor> annotation, Annotation ast, EclipseNode annotationNode) {
EclipseNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, NoArgsConstructor.class.getSimpleName())) return true;
NoArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -89,6 +90,7 @@ public class HandleConstructor {
public static class HandleRequiredArgsConstructor implements EclipseAnnotationHandler<RequiredArgsConstructor> {
@Override public boolean handle(AnnotationValues<RequiredArgsConstructor> annotation, Annotation ast, EclipseNode annotationNode) {
EclipseNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, RequiredArgsConstructor.class.getSimpleName())) return true;
RequiredArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -105,10 +107,7 @@ public class HandleConstructor {
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
- //Skip fields that start with $
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') continue;
- //Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) continue;
+ if (!EclipseHandlerUtil.filterField(fieldDecl)) continue;
boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0;
boolean isNonNull = findAnnotations(fieldDecl, TransformationsUtil.NON_NULL_PATTERN).length != 0;
if ((isFinal || isNonNull) && fieldDecl.initialization == null) fields.add(child);
@@ -120,6 +119,7 @@ public class HandleConstructor {
public static class HandleAllArgsConstructor implements EclipseAnnotationHandler<AllArgsConstructor> {
@Override public boolean handle(AnnotationValues<AllArgsConstructor> annotation, Annotation ast, EclipseNode annotationNode) {
EclipseNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, AllArgsConstructor.class.getSimpleName())) return true;
AllArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -130,12 +130,11 @@ public class HandleConstructor {
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
- // Skip fields that start with $
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') continue;
- // Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) continue;
+ if (!EclipseHandlerUtil.filterField(fieldDecl)) continue;
+
// Skip initialized final fields.
- if (((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0) && fieldDecl.initialization != null) continue;
+ if (((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0) && fieldDecl.initialization != null) return false;
+
fields.add(child);
}
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, suppressConstructorProperties, ast);
@@ -143,6 +142,20 @@ public class HandleConstructor {
}
}
+ static boolean checkLegality(EclipseNode typeNode, EclipseNode errorNode, String name) {
+ TypeDeclaration typeDecl = null;
+ if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get();
+ int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
+ boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
+
+ if (typeDecl == null || notAClass) {
+ errorNode.addError(name + " is only supported on a class or an enum.");
+ return false;
+ }
+
+ return true;
+ }
+
public void generateRequiredArgsConstructor(EclipseNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists, ASTNode source) {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false, source);
}
@@ -208,6 +221,10 @@ public class HandleConstructor {
EclipseNode type, Collection<EclipseNode> fields, boolean suppressConstructorProperties, ASTNode source) {
long p = (long)source.sourceStart << 32 | source.sourceEnd;
+ boolean isEnum = (((TypeDeclaration)type.get()).modifiers & ClassFileConstants.AccEnum) != 0;
+
+ if (isEnum) level = AccessLevel.PRIVATE;
+
ConstructorDeclaration constructor = new ConstructorDeclaration(
((CompilationUnitDeclaration) type.top().get()).compilationResult);
Eclipse.setGeneratedBy(constructor, source);
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index 895c076e..d906d85d 100644
--- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -193,14 +193,12 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
- //Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) continue;
+ if (!EclipseHandlerUtil.filterField(fieldDecl)) continue;
+
//Skip transient fields.
if ((fieldDecl.modifiers & ClassFileConstants.AccTransient) != 0) continue;
//Skip excluded fields.
if (excludes != null && excludes.contains(new String(fieldDecl.name))) continue;
- //Skip fields that start with $.
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') continue;
nodesForEquality.add(child);
}
}
diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java
index 56d0ba6c..f39e55b5 100644
--- a/src/core/lombok/eclipse/handlers/HandleGetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleGetter.java
@@ -87,10 +87,10 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get();
int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
boolean notAClass = (modifiers &
- (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
+ (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
if (typeDecl == null || notAClass) {
- pos.addError("@Getter is only supported on a class or a field.");
+ pos.addError("@Getter is only supported on a class, an enum, or a field.");
return false;
}
@@ -103,11 +103,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
public boolean fieldQualifiesForGetterGeneration(EclipseNode field) {
if (field.getKind() != Kind.FIELD) return false;
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
- //Skip fields that start with $
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') return false;
- //Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) return false;
- return true;
+ return EclipseHandlerUtil.filterField(fieldDecl);
}
/**
diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java
index 2c3ca6ed..14ff0e10 100644
--- a/src/core/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleSetter.java
@@ -85,10 +85,8 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
for (EclipseNode field : typeNode.down()) {
if (field.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
- //Skip fields that start with $
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') continue;
- //Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) continue;
+ if (!EclipseHandlerUtil.filterField(fieldDecl)) continue;
+
//Skip final fields.
if ((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0) continue;
diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java
index f39cb129..8d722247 100644
--- a/src/core/lombok/eclipse/handlers/HandleToString.java
+++ b/src/core/lombok/eclipse/handlers/HandleToString.java
@@ -128,10 +128,10 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> {
if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get();
int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
boolean notAClass = (modifiers &
- (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
+ (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
if (typeDecl == null || notAClass) {
- errorNode.addError("@ToString is only supported on a class.");
+ errorNode.addError("@ToString is only supported on a class or enum.");
return false;
}
@@ -152,12 +152,11 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> {
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
- //Skip static fields.
- if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) continue;
+ if (!EclipseHandlerUtil.filterField(fieldDecl)) continue;
+
//Skip excluded fields.
if (excludes != null && excludes.contains(new String(fieldDecl.name))) continue;
- //Skip fields that start with $
- if (fieldDecl.name.length > 0 && fieldDecl.name[0] == '$') continue;
+
nodesForToString.add(child);
}
}
diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java
index 7f0c2ba6..3cb36c30 100644
--- a/src/core/lombok/javac/handlers/HandleConstructor.java
+++ b/src/core/lombok/javac/handlers/HandleConstructor.java
@@ -62,6 +62,7 @@ public class HandleConstructor {
markAnnotationAsProcessed(annotationNode, NoArgsConstructor.class);
deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
JavacNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, NoArgsConstructor.class.getSimpleName())) return true;
NoArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -82,6 +83,7 @@ public class HandleConstructor {
markAnnotationAsProcessed(annotationNode, RequiredArgsConstructor.class);
deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
JavacNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, RequiredArgsConstructor.class.getSimpleName())) return true;
RequiredArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -120,6 +122,7 @@ public class HandleConstructor {
markAnnotationAsProcessed(annotationNode, AllArgsConstructor.class);
deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
JavacNode typeNode = annotationNode.up();
+ if (!checkLegality(typeNode, annotationNode, AllArgsConstructor.class.getSimpleName())) return true;
AllArgsConstructor ann = annotation.getInstance();
AccessLevel level = ann.access();
String staticName = ann.staticName();
@@ -148,6 +151,20 @@ public class HandleConstructor {
}
}
+ static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) {
+ JCClassDecl typeDecl = null;
+ if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
+ long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
+ boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
+
+ if (typeDecl == null || notAClass) {
+ errorNode.addError(name + " is only supported on a class or an enum.");
+ return false;
+ }
+
+ return true;
+ }
+
public void generateRequiredArgsConstructor(JavacNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists) {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false);
}
diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index cfe5c98a..8a1f7eed 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -85,10 +85,10 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
JCClassDecl typeDecl = null;
if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
- boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
+ boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
if (typeDecl == null || notAClass) {
- errorNode.addError("@Getter is only supported on a class or a field.");
+ errorNode.addError("@Getter is only supported on a class, an enum, or a field.");
return false;
}
diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java
index 6f7465f3..8bc8036f 100644
--- a/src/core/lombok/javac/handlers/HandleToString.java
+++ b/src/core/lombok/javac/handlers/HandleToString.java
@@ -117,7 +117,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
boolean notAClass = true;
if (typeNode.get() instanceof JCClassDecl) {
long flags = ((JCClassDecl)typeNode.get()).mods.flags;
- notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
+ notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
}
if (callSuper == null) {
@@ -127,7 +127,7 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
}
if (notAClass) {
- errorNode.addError("@ToString is only supported on a class.");
+ errorNode.addError("@ToString is only supported on a class or enum.");
return false;
}