aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lombok')
-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
-rw-r--r--src/core/lombok/experimental/FieldDefaults.java45
-rw-r--r--src/core/lombok/experimental/NonFinal.java35
-rw-r--r--src/core/lombok/experimental/PackagePrivate.java35
-rw-r--r--src/core/lombok/experimental/Value.java69
-rw-r--r--src/core/lombok/javac/handlers/HandleConstructor.java36
-rw-r--r--src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java10
-rw-r--r--src/core/lombok/javac/handlers/HandleFieldDefaults.java111
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java18
-rw-r--r--src/core/lombok/javac/handlers/HandleSetter.java20
-rw-r--r--src/core/lombok/javac/handlers/HandleToString.java13
-rw-r--r--src/core/lombok/javac/handlers/HandleValue.java60
-rw-r--r--src/core/lombok/javac/handlers/HandleWither.java10
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java29
22 files changed, 669 insertions, 125 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;
}
}
diff --git a/src/core/lombok/experimental/FieldDefaults.java b/src/core/lombok/experimental/FieldDefaults.java
new file mode 100644
index 00000000..5bbf92dc
--- /dev/null
+++ b/src/core/lombok/experimental/FieldDefaults.java
@@ -0,0 +1,45 @@
+/*
+ * 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.experimental;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import lombok.AccessLevel;
+
+/**
+ * Adds modifiers to each field in the type with this annotation.
+ * If {@code makeFinal} is {@code true}, then each field that is not annotated with {@code @NonFinal} will have the {@code final} modifier added.
+ * <p>
+ * If {@code level} is set, then each field that is package private (i.e. no access modifier) and does not have the {@code @PackagePrivate} annotation will
+ * have the appropriate access level modifier added.
+ * <p>
+ * The only fields that are skipped are those that start with a '$'; everything else, including static, volatile, and transient fields, are modified.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.SOURCE)
+public @interface FieldDefaults {
+ AccessLevel level() default AccessLevel.NONE;
+ boolean makeFinal() default false;
+}
diff --git a/src/core/lombok/experimental/NonFinal.java b/src/core/lombok/experimental/NonFinal.java
new file mode 100644
index 00000000..0c31dd2a
--- /dev/null
+++ b/src/core/lombok/experimental/NonFinal.java
@@ -0,0 +1,35 @@
+/*
+ * 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.experimental;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to indicate the explicit intention for the annotated entity to <em>not</em> be {@code final}.
+ * Currently used by {@code FieldDefaults} to avoid having it make a field final.
+ */
+@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.SOURCE)
+public @interface NonFinal {}
diff --git a/src/core/lombok/experimental/PackagePrivate.java b/src/core/lombok/experimental/PackagePrivate.java
new file mode 100644
index 00000000..bfe5638b
--- /dev/null
+++ b/src/core/lombok/experimental/PackagePrivate.java
@@ -0,0 +1,35 @@
+/*
+ * 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.experimental;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to indicate the explicit intention for the annotated entity to have the <em>package private</em> access level.
+ * Currently used by {@code FieldDefaults} to avoid having it make a field one of {@code public}, {@code protected}, or {@code private}.
+ */
+@Target({ElementType.TYPE, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
+@Retention(RetentionPolicy.SOURCE)
+public @interface PackagePrivate {}
diff --git a/src/core/lombok/experimental/Value.java b/src/core/lombok/experimental/Value.java
new file mode 100644
index 00000000..f8bd06b2
--- /dev/null
+++ b/src/core/lombok/experimental/Value.java
@@ -0,0 +1,69 @@
+/*
+ * 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.experimental;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Generates a lot of code which fits with a class that is a representation of an immutable entity.
+ * Specifically, it generates:<ul>
+ * <li>Getters for all fields
+ * <li>toString method
+ * <li>hashCode and equals implementations that check all non-transient fields.
+ * <li>Generates withers for all fields (except final fields that are initialized in the field declaration itself)
+ * <li>Generates a constructor for each argument
+ * <li>Adds {@code private} and {@code final} to each field.
+ * </ul>
+ *
+ * In other words, {@code @Value} is a shorthand for {@code @Getter @Wither @FieldDefaults(makeFinal=true,level=AccessLevel.PRIVATE) @EqualsAndHashCode @ToString @AllArgsConstructor}.
+ * <p>
+ * If any method to be generated already exists (in name - the return type or parameters are not relevant), then
+ * that method will not be generated by the Data annotation.
+ * <p>
+ * The generated constructor will have 1 parameter for each field. The generated toString will print all fields,
+ * while the generated hashCode and equals take into account all non-transient fields.<br>
+ * Static fields are skipped (no getter or setter, and they are not included in toString, equals, hashCode, or the constructor).
+ * <p>
+ * {@code toString}, {@code equals}, and {@code hashCode} use the deepX variants in the
+ * {@code java.util.Arrays} utility class. Therefore, if your class has arrays that contain themselves,
+ * these methods will just loop endlessly until the inevitable {@code StackOverflowError}. This behaviour
+ * is no different from {@code java.util.ArrayList}, though.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.SOURCE)
+public @interface Value {
+ /**
+ * If you specify a static constructor name, then the generated constructor will be private, and
+ * instead a static factory method is created that other classes can use to create instances.
+ * We suggest the name: "of", like so:
+ *
+ * <pre>
+ * public @Data(staticConstructor = "of") class Point { final int x, y; }
+ * </pre>
+ *
+ * Default: No static constructor, instead the normal constructor is public.
+ */
+ String staticConstructor() default "";
+}
diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java
index d701b41e..b6c31f83 100644
--- a/src/core/lombok/javac/handlers/HandleConstructor.java
+++ b/src/core/lombok/javac/handlers/HandleConstructor.java
@@ -116,21 +116,25 @@ public class HandleConstructor {
@SuppressWarnings("deprecation")
boolean suppressConstructorProperties = ann.suppressConstructorProperties();
if (level == AccessLevel.NONE) return;
- ListBuffer<JavacNode> fields = ListBuffer.lb();
- for (JavacNode child : typeNode.down()) {
- if (child.getKind() != Kind.FIELD) continue;
- JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
- // Skip fields that start with $
- if (fieldDecl.name.toString().startsWith("$")) continue;
- long fieldFlags = fieldDecl.mods.flags;
- // Skip static fields.
- if ((fieldFlags & Flags.STATIC) != 0) continue;
- // Skip initialized final fields.
- if (((fieldFlags & Flags.FINAL) != 0) && fieldDecl.init != null) continue;
- fields.append(child);
- }
- new HandleConstructor().generateConstructor(typeNode, level, fields.toList(), staticName, false, suppressConstructorProperties, annotationNode);
+ new HandleConstructor().generateConstructor(typeNode, level, findAllFields(typeNode), staticName, false, suppressConstructorProperties, annotationNode);
+ }
+ }
+
+ private static List<JavacNode> findAllFields(JavacNode typeNode) {
+ ListBuffer<JavacNode> fields = ListBuffer.lb();
+ for (JavacNode child : typeNode.down()) {
+ if (child.getKind() != Kind.FIELD) continue;
+ JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
+ //Skip fields that start with $
+ if (fieldDecl.name.toString().startsWith("$")) continue;
+ long fieldFlags = fieldDecl.mods.flags;
+ //Skip static fields.
+ if ((fieldFlags & Flags.STATIC) != 0) continue;
+ //Skip initialized final fields
+ boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
+ if (!isFinal || fieldDecl.init == null) fields.append(child);
}
+ return fields.toList();
}
static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) {
@@ -151,6 +155,10 @@ public class HandleConstructor {
generateConstructor(typeNode, level, findRequiredFields(typeNode), staticName, skipIfConstructorExists, false, source);
}
+ public void generateAllArgsConstructor(JavacNode typeNode, AccessLevel level, String staticName, boolean skipIfConstructorExists, JavacNode source) {
+ generateConstructor(typeNode, level, findAllFields(typeNode), staticName, skipIfConstructorExists, false, source);
+ }
+
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JavacNode> fields, String staticName, boolean skipIfConstructorExists, boolean suppressConstructorProperties, JavacNode source) {
boolean staticConstrRequired = staticName != null && !staticName.equals("");
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
index 3b8c7af3..2b9b546d 100644
--- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -107,13 +107,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
}
public void generateEqualsAndHashCodeForType(JavacNode typeNode, JavacNode source) {
- for (JavacNode 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, source, null, null, null, false, FieldAccess.GETTER);
diff --git a/src/core/lombok/javac/handlers/HandleFieldDefaults.java b/src/core/lombok/javac/handlers/HandleFieldDefaults.java
new file mode 100644
index 00000000..a2dfb7ce
--- /dev/null
+++ b/src/core/lombok/javac/handlers/HandleFieldDefaults.java
@@ -0,0 +1,111 @@
+/*
+ * 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.javac.handlers;
+
+import static lombok.javac.handlers.JavacHandlerUtil.*;
+import lombok.AccessLevel;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.experimental.FieldDefaults;
+import lombok.experimental.NonFinal;
+import lombok.experimental.PackagePrivate;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacNode;
+
+import org.mangosdk.spi.ProviderFor;
+
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+import com.sun.tools.javac.tree.JCTree.JCClassDecl;
+import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
+
+/**
+ * Handles the {@code lombok.FieldDefaults} annotation for eclipse.
+ */
+@ProviderFor(JavacAnnotationHandler.class)
+public class HandleFieldDefaults extends JavacAnnotationHandler<FieldDefaults> {
+ public boolean generateFieldDefaultsForType(JavacNode typeNode, JavacNode errorNode, 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;
+ }
+ }
+
+ 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("@FieldDefaults is only supported on a class or an enum.");
+ return false;
+ }
+
+ for (JavacNode field : typeNode.down()) {
+ if (field.getKind() != Kind.FIELD) continue;
+ JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
+ //Skip fields that start with $
+ if (fieldDecl.name.toString().startsWith("$")) continue;
+
+ setFieldDefaultsForField(field, errorNode.get(), level, makeFinal);
+ }
+
+ return true;
+ }
+
+ public void setFieldDefaultsForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, boolean makeFinal) {
+ JCVariableDecl field = (JCVariableDecl) fieldNode.get();
+ if (level != null && level != AccessLevel.NONE) {
+ if ((field.mods.flags & (Flags.PUBLIC | Flags.PRIVATE | Flags.PROTECTED)) == 0) {
+ if (!hasAnnotationAndDeleteIfNeccessary(PackagePrivate.class, fieldNode)) {
+ field.mods.flags |= toJavacModifier(level);
+ }
+ }
+ }
+
+ if (makeFinal && (field.mods.flags & Flags.FINAL) == 0) {
+ if (!hasAnnotationAndDeleteIfNeccessary(NonFinal.class, fieldNode)) {
+ field.mods.flags |= Flags.FINAL;
+ }
+ }
+ }
+
+ @Override public void handle(AnnotationValues<FieldDefaults> annotation, JCAnnotation ast, JavacNode annotationNode) {
+ deleteAnnotationIfNeccessary(annotationNode, FieldDefaults.class);
+ deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
+ JavacNode 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/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index fa60504f..8a1fb4e1 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -73,11 +73,9 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
public class HandleGetter extends JavacAnnotationHandler<Getter> {
public void generateGetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelGetter) {
if (checkForTypeLevelGetter) {
- if (typeNode != null) for (JavacNode child : typeNode.down()) {
- if (annotationTypeMatches(Getter.class, child)) {
- //The annotation will make it happen, so we can skip it.
- return;
- }
+ if (hasAnnotation(Getter.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
}
@@ -122,13 +120,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
* @param pos The node responsible for generating the getter (the {@code @Data} or {@code @Getter} annotation).
*/
public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, boolean lazy) {
- for (JavacNode 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, false, lazy);
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java
index a782e605..093b1947 100644
--- a/src/core/lombok/javac/handlers/HandleSetter.java
+++ b/src/core/lombok/javac/handlers/HandleSetter.java
@@ -68,13 +68,9 @@ import com.sun.tools.javac.util.Name;
public class HandleSetter extends JavacAnnotationHandler<Setter> {
public void generateSetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelSetter) {
if (checkForTypeLevelSetter) {
- if (typeNode != null) for (JavacNode 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;
- }
- }
+ if (hasAnnotation(Setter.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
}
@@ -118,13 +114,9 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
* @param pos The node responsible for generating the setter (the {@code @Data} or {@code @Setter} annotation).
*/
public void generateSetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
- for (JavacNode 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, false);
diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java
index 27641cf2..aad06b83 100644
--- a/src/core/lombok/javac/handlers/HandleToString.java
+++ b/src/core/lombok/javac/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
@@ -95,15 +95,12 @@ public class HandleToString extends JavacAnnotationHandler<ToString> {
}
public void generateToStringForType(JavacNode typeNode, JavacNode errorNode) {
- for (JavacNode 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;
try {
includeFieldNames = ((Boolean)ToString.class.getMethod("includeFieldNames").getDefaultValue()).booleanValue();
diff --git a/src/core/lombok/javac/handlers/HandleValue.java b/src/core/lombok/javac/handlers/HandleValue.java
new file mode 100644
index 00000000..fac017a8
--- /dev/null
+++ b/src/core/lombok/javac/handlers/HandleValue.java
@@ -0,0 +1,60 @@
+/*
+ * 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.javac.handlers;
+
+import static lombok.javac.handlers.JavacHandlerUtil.*;
+import lombok.AccessLevel;
+import lombok.core.AnnotationValues;
+import lombok.experimental.Value;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacNode;
+
+import org.mangosdk.spi.ProviderFor;
+
+import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+
+/**
+ * Handles the {@code lombok.Value} annotation for javac.
+ */
+@ProviderFor(JavacAnnotationHandler.class)
+public class HandleValue extends JavacAnnotationHandler<Value> {
+ @Override public void handle(AnnotationValues<Value> annotation, JCAnnotation ast, JavacNode annotationNode) {
+ deleteAnnotationIfNeccessary(annotationNode, Value.class);
+ JavacNode typeNode = annotationNode.up();
+ boolean notAClass = !isClass(typeNode);
+
+ if (notAClass) {
+ annotationNode.addError("@Value is only supported on a class.");
+ return;
+ }
+
+ String staticConstructorName = annotation.getInstance().staticConstructor();
+
+ // TODO move this to the end OR move it to the top in eclipse.
+ new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, true, annotationNode);
+ 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);
+ }
+}
diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java
index 61bf7bd0..6b4067b1 100644
--- a/src/core/lombok/javac/handlers/HandleWither.java
+++ b/src/core/lombok/javac/handlers/HandleWither.java
@@ -63,13 +63,9 @@ import com.sun.tools.javac.util.Name;
public class HandleWither extends JavacAnnotationHandler<Wither> {
public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
if (checkForTypeLevelWither) {
- if (typeNode != null) for (JavacNode 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;
- }
- }
+ if (hasAnnotation(Wither.class, typeNode)) {
+ //The annotation will make it happen, so we can skip it.
+ return;
}
}
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index b0f2a890..08542664 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -137,6 +137,35 @@ public class JavacHandlerUtil {
return node;
}
+ public static boolean hasAnnotation(Class<? extends Annotation> type, JavacNode node) {
+ return hasAnnotation(type, node, false);
+ }
+
+ public static boolean hasAnnotationAndDeleteIfNeccessary(Class<? extends Annotation> type, JavacNode node) {
+ return hasAnnotation(type, node, true);
+ }
+
+ private static boolean hasAnnotation(Class<? extends Annotation> type, JavacNode node, boolean delete) {
+ if (node == null) return false;
+ if (type == null) return false;
+ switch (node.getKind()) {
+ case ARGUMENT:
+ case FIELD:
+ case LOCAL:
+ case TYPE:
+ case METHOD:
+ for (JavacNode child : node.down()) {
+ if (annotationTypeMatches(type, child)) {
+ if (delete) deleteAnnotationIfNeccessary(child, type);
+ return true;
+ }
+ }
+ // intentional fallthrough
+ default:
+ return false;
+ }
+ }
+
/**
* Checks if the Annotation AST Node provided is likely to be an instance of the provided annotation type.
*