aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/eclipse
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2012-08-06 22:47:59 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2012-08-06 22:47:59 +0200
commit70317c73841d3e83b4b8008b68bea95753a5275f (patch)
tree1c66ab010930425aa673b3988aa4a5ad3ae1dd13 /src/core/lombok/eclipse
parentd1b0242dc5e38cddd0e1ecc2a089c13e744d75d4 (diff)
downloadlombok-70317c73841d3e83b4b8008b68bea95753a5275f.tar.gz
lombok-70317c73841d3e83b4b8008b68bea95753a5275f.tar.bz2
lombok-70317c73841d3e83b4b8008b68bea95753a5275f.zip
Added @Value and @FieldDefaults implementations for javac and ecj, the annotations including @NonFinal and @PackagePrivate, and some refactors. No tests yet.
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java24
-rw-r--r--src/core/lombok/eclipse/handlers/HandleConstructor.java32
-rw-r--r--src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java10
-rw-r--r--src/core/lombok/eclipse/handlers/HandleFieldDefaults.java107
-rw-r--r--src/core/lombok/eclipse/handlers/HandleGetter.java20
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSetter.java20
-rw-r--r--src/core/lombok/eclipse/handlers/HandleToString.java12
-rw-r--r--src/core/lombok/eclipse/handlers/HandleValue.java68
-rw-r--r--src/core/lombok/eclipse/handlers/HandleWither.java10
9 files changed, 240 insertions, 63 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index ed18dd45..79a14d5a 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -466,6 +466,24 @@ public class EclipseHandlerUtil {
return result.toArray(EMPTY_ANNOTATION_ARRAY);
}
+ public static boolean hasAnnotation(Class<? extends java.lang.annotation.Annotation> type, EclipseNode node) {
+ if (node == null) return false;
+ if (type == null) return false;
+ switch (node.getKind()) {
+ case ARGUMENT:
+ case FIELD:
+ case LOCAL:
+ case TYPE:
+ case METHOD:
+ for (EclipseNode child : node.down()) {
+ if (annotationTypeMatches(type, child)) return true;
+ }
+ // intentional fallthrough
+ default:
+ return false;
+ }
+ }
+
/**
* Checks if the provided annotation type is likely to be the intended type for the given annotation node.
*
@@ -1034,6 +1052,10 @@ public class EclipseHandlerUtil {
* 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) {
+ return filterField(declaration, true);
+ }
+
+ public static boolean filterField(FieldDeclaration declaration, boolean skipStatic) {
// Skip the fake fields that represent enum constants.
if (declaration.initialization instanceof AllocationExpression &&
((AllocationExpression)declaration.initialization).enumConstant != null) return false;
@@ -1044,7 +1066,7 @@ public class EclipseHandlerUtil {
if (declaration.name.length > 0 && declaration.name[0] == '$') return false;
// Skip static fields.
- if ((declaration.modifiers & ClassFileConstants.AccStatic) != 0) return false;
+ if (skipStatic && (declaration.modifiers & ClassFileConstants.AccStatic) != 0) return false;
return true;
}
diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java
index 25d47870..5d4656b6 100644
--- a/src/core/lombok/eclipse/handlers/HandleConstructor.java
+++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java
@@ -111,6 +111,21 @@ public class HandleConstructor {
return fields;
}
+ private static List<EclipseNode> findAllFields(EclipseNode typeNode) {
+ List<EclipseNode> fields = new ArrayList<EclipseNode>();
+ for (EclipseNode child : typeNode.down()) {
+ if (child.getKind() != Kind.FIELD) continue;
+ FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
+ if (!filterField(fieldDecl)) continue;
+
+ // Skip initialized final fields.
+ if (((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0) && fieldDecl.initialization != null) continue;
+
+ fields.add(child);
+ }
+ return fields;
+ }
+
@ProviderFor(EclipseAnnotationHandler.class)
public static class HandleAllArgsConstructor extends EclipseAnnotationHandler<AllArgsConstructor> {
@Override public void handle(AnnotationValues<AllArgsConstructor> annotation, Annotation ast, EclipseNode annotationNode) {
@@ -122,18 +137,7 @@ public class HandleConstructor {
@SuppressWarnings("deprecation")
boolean suppressConstructorProperties = ann.suppressConstructorProperties();
if (level == AccessLevel.NONE) return;
- List<EclipseNode> fields = new ArrayList<EclipseNode>();
- for (EclipseNode child : typeNode.down()) {
- if (child.getKind() != Kind.FIELD) continue;
- FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
- if (!filterField(fieldDecl)) continue;
-
- // Skip initialized final fields.
- if (((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0) && fieldDecl.initialization != null) continue;
-
- fields.add(child);
- }
- new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, suppressConstructorProperties, ast);
+ new HandleConstructor().generateConstructor(typeNode, level, findAllFields(typeNode), staticName, false, suppressConstructorProperties, ast);
}
}
@@ -155,6 +159,10 @@ public class HandleConstructor {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false, source);
}
+ public void generateAllArgsConstructor(EclipseNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists, ASTNode source) {
+ generateConstructor(typeNode, level, findAllFields(typeNode), staticName, skipIfConstructorExists, false, source);
+ }
+
public void generateConstructor(EclipseNode typeNode, AccessLevel level, List<EclipseNode> fields, String staticName, boolean skipIfConstructorExists, boolean suppressConstructorProperties, ASTNode source) {
boolean staticConstrRequired = staticName != null && !staticName.equals("");
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index 50991e7b..0c82b74c 100644
--- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -108,13 +108,9 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
}
public void generateEqualsAndHashCodeForType(EclipseNode typeNode, EclipseNode errorNode) {
- for (EclipseNode child : typeNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(EqualsAndHashCode.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return;
- }
- }
+ if (hasAnnotation(EqualsAndHashCode.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
generateMethods(typeNode, errorNode, null, null, null, false, FieldAccess.GETTER);
diff --git a/src/core/lombok/eclipse/handlers/HandleFieldDefaults.java b/src/core/lombok/eclipse/handlers/HandleFieldDefaults.java
new file mode 100644
index 00000000..e1dd8460
--- /dev/null
+++ b/src/core/lombok/eclipse/handlers/HandleFieldDefaults.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2012 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.eclipse.handlers;
+
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
+import lombok.AccessLevel;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.eclipse.EclipseAnnotationHandler;
+import lombok.eclipse.EclipseNode;
+import lombok.experimental.FieldDefaults;
+import lombok.experimental.NonFinal;
+import lombok.experimental.PackagePrivate;
+
+import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
+import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
+import org.mangosdk.spi.ProviderFor;
+
+/**
+ * Handles the {@code lombok.FieldDefaults} annotation for eclipse.
+ */
+@ProviderFor(EclipseAnnotationHandler.class)
+public class HandleFieldDefaults extends EclipseAnnotationHandler<FieldDefaults> {
+ public boolean generateFieldDefaultsForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean makeFinal, boolean checkForTypeLevelFieldDefaults) {
+ if (checkForTypeLevelFieldDefaults) {
+ if (hasAnnotation(FieldDefaults.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return true;
+ }
+ }
+
+ 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) {
+ pos.addError("@FieldDefaults is only supported on a class or an enum.");
+ return false;
+ }
+
+ for (EclipseNode field : typeNode.down()) {
+ if (field.getKind() != Kind.FIELD) continue;
+ FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
+ if (!filterField(fieldDecl, false)) continue;
+
+ setFieldDefaultsForField(field, pos.get(), level, makeFinal);
+ }
+ return true;
+ }
+
+ public void setFieldDefaultsForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, boolean makeFinal) {
+ FieldDeclaration field = (FieldDeclaration) fieldNode.get();
+ if (level != null && level != AccessLevel.NONE) {
+ if ((field.modifiers & (ClassFileConstants.AccPublic | ClassFileConstants.AccPrivate | ClassFileConstants.AccProtected)) == 0) {
+ if (!hasAnnotation(PackagePrivate.class, fieldNode)) {
+ field.modifiers |= EclipseHandlerUtil.toEclipseModifier(level);
+ }
+ }
+ }
+
+ if (makeFinal && (field.modifiers & ClassFileConstants.AccFinal) == 0) {
+ if (!hasAnnotation(NonFinal.class, fieldNode)) {
+ field.modifiers |= ClassFileConstants.AccFinal;
+ }
+ }
+ }
+
+ public void handle(AnnotationValues<FieldDefaults> annotation, Annotation ast, EclipseNode annotationNode) {
+ EclipseNode node = annotationNode.up();
+ FieldDefaults instance = annotation.getInstance();
+ AccessLevel level = instance.level();
+ boolean makeFinal = instance.makeFinal();
+
+ if (level == AccessLevel.NONE && !makeFinal) {
+ annotationNode.addError("This does nothing; provide either level or makeFinal or both.");
+ return;
+ }
+
+ if (node == null) return;
+
+ generateFieldDefaultsForType(node, annotationNode, level, makeFinal, false);
+ }
+}
diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java
index 31376749..df05fc7e 100644
--- a/src/core/lombok/eclipse/handlers/HandleGetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleGetter.java
@@ -78,13 +78,9 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> {
public boolean generateGetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelGetter) {
if (checkForTypeLevelGetter) {
- if (typeNode != null) for (EclipseNode child : typeNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(Getter.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return true;
- }
- }
+ if (hasAnnotation(Getter.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return true;
}
}
@@ -124,13 +120,9 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> {
* be a warning if its already there. The default access level is used.
*/
public void generateGetterForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, boolean lazy) {
- for (EclipseNode child : fieldNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(Getter.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return;
- }
- }
+ if (hasAnnotation(Getter.class, fieldNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
createGetterForField(level, fieldNode, fieldNode, pos, false, lazy);
diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java
index 0bce69d6..8037ed23 100644
--- a/src/core/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleSetter.java
@@ -63,13 +63,9 @@ import org.mangosdk.spi.ProviderFor;
public class HandleSetter extends EclipseAnnotationHandler<Setter> {
public boolean generateSetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelSetter) {
if (checkForTypeLevelSetter) {
- if (typeNode != null) for (EclipseNode child : typeNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(Setter.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return true;
- }
- }
+ if (hasAnnotation(Setter.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return true;
}
}
@@ -110,13 +106,9 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
* be a warning if its already there. The default access level is used.
*/
public void generateSetterForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level) {
- for (EclipseNode child : fieldNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(Setter.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return;
- }
- }
+ if (hasAnnotation(Setter.class, fieldNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
createSetterForField(level, fieldNode, fieldNode, pos, false);
diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java
index 26f0e9be..75d4acef 100644
--- a/src/core/lombok/eclipse/handlers/HandleToString.java
+++ b/src/core/lombok/eclipse/handlers/HandleToString.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2012 The Project Lombok Authors.
*
* 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,13 +81,9 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> {
}
public void generateToStringForType(EclipseNode typeNode, EclipseNode errorNode) {
- for (EclipseNode child : typeNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(ToString.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return;
- }
- }
+ if (hasAnnotation(ToString.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
boolean includeFieldNames = true;
diff --git a/src/core/lombok/eclipse/handlers/HandleValue.java b/src/core/lombok/eclipse/handlers/HandleValue.java
new file mode 100644
index 00000000..9b3edabf
--- /dev/null
+++ b/src/core/lombok/eclipse/handlers/HandleValue.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2012 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.eclipse.handlers;
+
+import lombok.AccessLevel;
+import lombok.experimental.Value;
+import lombok.core.AnnotationValues;
+import lombok.eclipse.EclipseAnnotationHandler;
+import lombok.eclipse.EclipseNode;
+
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
+import org.mangosdk.spi.ProviderFor;
+
+/**
+ * Handles the {@code lombok.Value} annotation for eclipse.
+ */
+@ProviderFor(EclipseAnnotationHandler.class)
+public class HandleValue extends EclipseAnnotationHandler<Value> {
+ public void handle(AnnotationValues<Value> annotation, Annotation ast, EclipseNode annotationNode) {
+ Value ann = annotation.getInstance();
+ EclipseNode typeNode = annotationNode.up();
+
+ 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 | ClassFileConstants.AccEnum)) != 0;
+
+ if (typeDecl == null || notAClass) {
+ annotationNode.addError("@Value is only supported on a class.");
+ return;
+ }
+
+ //Careful: Generate the public static constructor (if there is one) LAST, so that any attempt to
+ //'find callers' on the annotation node will find callers of the constructor, which is by far the
+ //most useful of the many methods built by @Value. This trick won't work for the non-static constructor,
+ //for whatever reason, though you can find callers of that one by focusing on the class name itself
+ //and hitting 'find callers'.
+
+ new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
+ new HandleWither().generateWitherForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
+ new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
+ new HandleToString().generateToStringForType(typeNode, annotationNode);
+ new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true);
+ new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, ann.staticConstructor(), true, ast);
+ }
+}
diff --git a/src/core/lombok/eclipse/handlers/HandleWither.java b/src/core/lombok/eclipse/handlers/HandleWither.java
index 7dd93d84..1441a5f2 100644
--- a/src/core/lombok/eclipse/handlers/HandleWither.java
+++ b/src/core/lombok/eclipse/handlers/HandleWither.java
@@ -61,13 +61,9 @@ import org.mangosdk.spi.ProviderFor;
public class HandleWither extends EclipseAnnotationHandler<Wither> {
public boolean generateWitherForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelWither) {
if (checkForTypeLevelWither) {
- if (typeNode != null) for (EclipseNode child : typeNode.down()) {
- if (child.getKind() == Kind.ANNOTATION) {
- if (annotationTypeMatches(Wither.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return true;
- }
- }
+ if (hasAnnotation(Wither.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return true;
}
}