From 70317c73841d3e83b4b8008b68bea95753a5275f Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 6 Aug 2012 22:47:59 +0200 Subject: Added @Value and @FieldDefaults implementations for javac and ecj, the annotations including @NonFinal and @PackagePrivate, and some refactors. No tests yet. --- .../eclipse/handlers/EclipseHandlerUtil.java | 24 ++++- .../lombok/eclipse/handlers/HandleConstructor.java | 32 +++--- .../eclipse/handlers/HandleEqualsAndHashCode.java | 10 +- .../eclipse/handlers/HandleFieldDefaults.java | 107 ++++++++++++++++++++ src/core/lombok/eclipse/handlers/HandleGetter.java | 20 ++-- src/core/lombok/eclipse/handlers/HandleSetter.java | 20 ++-- .../lombok/eclipse/handlers/HandleToString.java | 12 +-- src/core/lombok/eclipse/handlers/HandleValue.java | 68 +++++++++++++ src/core/lombok/eclipse/handlers/HandleWither.java | 10 +- src/core/lombok/experimental/FieldDefaults.java | 45 +++++++++ src/core/lombok/experimental/NonFinal.java | 35 +++++++ src/core/lombok/experimental/PackagePrivate.java | 35 +++++++ src/core/lombok/experimental/Value.java | 69 +++++++++++++ .../lombok/javac/handlers/HandleConstructor.java | 36 ++++--- .../javac/handlers/HandleEqualsAndHashCode.java | 10 +- .../lombok/javac/handlers/HandleFieldDefaults.java | 111 +++++++++++++++++++++ src/core/lombok/javac/handlers/HandleGetter.java | 18 ++-- src/core/lombok/javac/handlers/HandleSetter.java | 20 ++-- src/core/lombok/javac/handlers/HandleToString.java | 13 +-- src/core/lombok/javac/handlers/HandleValue.java | 60 +++++++++++ src/core/lombok/javac/handlers/HandleWither.java | 10 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 29 ++++++ src/utils/lombok/eclipse/Eclipse.java | 2 +- 23 files changed, 670 insertions(+), 126 deletions(-) create mode 100644 src/core/lombok/eclipse/handlers/HandleFieldDefaults.java create mode 100644 src/core/lombok/eclipse/handlers/HandleValue.java create mode 100644 src/core/lombok/experimental/FieldDefaults.java create mode 100644 src/core/lombok/experimental/NonFinal.java create mode 100644 src/core/lombok/experimental/PackagePrivate.java create mode 100644 src/core/lombok/experimental/Value.java create mode 100644 src/core/lombok/javac/handlers/HandleFieldDefaults.java create mode 100644 src/core/lombok/javac/handlers/HandleValue.java 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 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 findAllFields(EclipseNode typeNode) { + List fields = new ArrayList(); + 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 { @Override public void handle(AnnotationValues annotation, Annotation ast, EclipseNode annotationNode) { @@ -122,18 +137,7 @@ public class HandleConstructor { @SuppressWarnings("deprecation") boolean suppressConstructorProperties = ann.suppressConstructorProperties(); if (level == AccessLevel.NONE) return; - List fields = new ArrayList(); - 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 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 { + 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 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 { 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 { * 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 { 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 { * 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 { } 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 { + public void handle(AnnotationValues 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 { 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. + *

+ * 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. + *

+ * 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 not 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 package private 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:

    + *
  • Getters for all fields + *
  • toString method + *
  • hashCode and equals implementations that check all non-transient fields. + *
  • Generates withers for all fields (except final fields that are initialized in the field declaration itself) + *
  • Generates a constructor for each argument + *
  • Adds {@code private} and {@code final} to each field. + *
+ * + * In other words, {@code @Value} is a shorthand for {@code @Getter @Wither @FieldDefaults(makeFinal=true,level=AccessLevel.PRIVATE) @EqualsAndHashCode @ToString @AllArgsConstructor}. + *

+ * 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. + *

+ * 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.
+ * Static fields are skipped (no getter or setter, and they are not included in toString, equals, hashCode, or the constructor). + *

+ * {@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: + * + *

+	 *     public @Data(staticConstructor = "of") class Point { final int x, y; }
+	 * 
+ * + * 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 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 findAllFields(JavacNode typeNode) { + ListBuffer 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 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 { + 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 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 { 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 { * @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 { 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 { * @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 { } 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 { + @Override public void handle(AnnotationValues 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 { 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 type, JavacNode node) { + return hasAnnotation(type, node, false); + } + + public static boolean hasAnnotationAndDeleteIfNeccessary(Class type, JavacNode node) { + return hasAnnotation(type, node, true); + } + + private static boolean hasAnnotation(Class 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. * diff --git a/src/utils/lombok/eclipse/Eclipse.java b/src/utils/lombok/eclipse/Eclipse.java index c1bd95c3..301925d1 100644 --- a/src/utils/lombok/eclipse/Eclipse.java +++ b/src/utils/lombok/eclipse/Eclipse.java @@ -122,7 +122,7 @@ public class Eclipse { if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; for (Annotation annotation : field.annotations) { TypeReference typeRef = annotation.type; - if (typeRef != null && typeRef.getTypeName()!= null) { + if (typeRef != null && typeRef.getTypeName() != null) { char[][] typeName = typeRef.getTypeName(); String suspect = new String(typeName[typeName.length - 1]); if (namePattern.matcher(suspect).matches()) { -- cgit