aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AUTHORS1
-rw-r--r--src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java150
-rw-r--r--src/core/lombok/experimental/FieldNameConstants.java43
-rw-r--r--src/core/lombok/javac/handlers/HandleFieldNameConstants.java159
4 files changed, 353 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index 15394c60..f2c9e6d7 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -12,6 +12,7 @@ Liu DongMiao <liudongmiao@gmail.com>
Luan Nico <luannico27@gmail.com>
Maarten Mulders <mthmulders@users.noreply.github.com>
Mart Hagenaars <marthagenaars@gmail.com>
+Michiel Verheul <cheelio@gmail.com>
Peter Grant <petercgrant@users.noreply.github.com>
Philipp Eichhorn <peichhor@web.de>
Rabea Gransberger <rgra@users.noreply.github.com>
diff --git a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
new file mode 100644
index 00000000..597b0937
--- /dev/null
+++ b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2012-2014 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 java.lang.Character.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
+
+import java.lang.reflect.Modifier;
+import java.util.Collection;
+
+import lombok.AccessLevel;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.eclipse.Eclipse;
+import lombok.eclipse.EclipseAnnotationHandler;
+import lombok.eclipse.EclipseNode;
+import lombok.experimental.FieldNameConstants;
+
+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.QualifiedTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
+import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
+import org.mangosdk.spi.ProviderFor;
+
+@ProviderFor(EclipseAnnotationHandler.class) public class HandleFieldNameConstants extends EclipseAnnotationHandler<FieldNameConstants> {
+
+ public boolean generateFieldDefaultsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level, boolean checkForTypeLevelFieldNameConstants) {
+
+ if (checkForTypeLevelFieldNameConstants) {
+ if (hasAnnotation(FieldNameConstants.class, typeNode)) {
+ 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) {
+ errorNode.addError("@FieldNameConstants is only supported on a class or an enum or a field.");
+ return false;
+ }
+
+ for (EclipseNode field : typeNode.down()) {
+ if (fieldQualifiesForFieldNameConstantsGeneration(field)) generateFieldNameConstantsForField(field, errorNode.get(), level);
+ if (field.getKind() != Kind.FIELD) return false;
+ }
+ return true;
+ }
+
+ private void generateFieldNameConstantsForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level) {
+ if (hasAnnotation(FieldNameConstants.class, fieldNode)) {
+ return;
+ }
+ createFieldNameConstantsForField(level, fieldNode, fieldNode, pos, false);
+ }
+
+ private void createFieldNameConstantsForField(AccessLevel level, EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists) {
+ if (fieldNode.getKind() != Kind.FIELD) {
+ errorNode.addError("@FieldNameConstants is only supported on a class or a field");
+ return;
+ }
+ FieldDeclaration field = (FieldDeclaration) fieldNode.get();
+ String constantName = camelCaseToConstant(new String(field.name));
+ if (constantName == null) {
+ errorNode.addWarning("Not generating constant for this field: It does not fit in your @Accessors prefix list");
+ return;
+ }
+ int pS = source.sourceStart, pE = source.sourceEnd;
+ long p = (long) pS << 32 | pE;
+ FieldDeclaration fieldConstant = new FieldDeclaration(constantName.toCharArray(), pS,pE);
+ fieldConstant.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ fieldConstant.modifiers = toEclipseModifier(level) | Modifier.STATIC | Modifier.FINAL;
+ fieldConstant.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[]{p,p,p});
+ fieldConstant.initialization = new StringLiteral(field.name, pS,pE,0);
+ injectField(fieldNode.up(), fieldConstant);
+ }
+
+ private boolean fieldQualifiesForFieldNameConstantsGeneration(EclipseNode field) {
+ if (field.getKind() != Kind.FIELD) return false;
+ FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
+ return filterField(fieldDecl);
+ }
+
+ public void handle(AnnotationValues<FieldNameConstants> annotation, Annotation ast, EclipseNode annotationNode) {
+ EclipseNode node = annotationNode.up();
+ FieldNameConstants annotatationInstance = annotation.getInstance();
+ AccessLevel level = annotatationInstance.level();
+ if (node == null) return;
+ switch (node.getKind()){
+ case FIELD:
+ createFieldNameConstantsForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true);
+ break;
+ case TYPE:
+ generateFieldDefaultsForType(node, annotationNode, level, false);
+ break;
+
+ }
+
+ }
+
+ private void createFieldNameConstantsForFields(AccessLevel level, Collection<EclipseNode> fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists) {
+ for (EclipseNode fieldNode : fieldNodes){
+ createFieldNameConstantsForField(level, fieldNode, errorNode, source, whineIfExists);
+ }
+ }
+
+ public static String camelCaseToConstant(final String fieldName) {
+ if (fieldName == null || fieldName.isEmpty()) return "";
+ char[] chars = fieldName.toCharArray();
+ StringBuilder b = new StringBuilder();
+ b.append(toUpperCase(chars[0]));
+ for (int i = 1, iend = chars.length; i < iend; i++) {
+ char c = chars[i];
+ if (isUpperCase(c)) {
+ b.append('_');
+ } else {
+ c = toUpperCase(c);
+ }
+ b.append(c);
+ }
+ return b.toString();
+ }
+
+}
diff --git a/src/core/lombok/experimental/FieldNameConstants.java b/src/core/lombok/experimental/FieldNameConstants.java
new file mode 100644
index 00000000..0f582f18
--- /dev/null
+++ b/src/core/lombok/experimental/FieldNameConstants.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2009-2013 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;
+
+
+
+
+/**
+ * Generates String constants containing the field name for each field.
+ *
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.SOURCE)
+public @interface FieldNameConstants {
+
+ lombok.AccessLevel level()default AccessLevel.PUBLIC;
+}
diff --git a/src/core/lombok/javac/handlers/HandleFieldNameConstants.java b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java
new file mode 100644
index 00000000..cf0fad0c
--- /dev/null
+++ b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2012-2014 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 java.lang.reflect.Modifier;
+import java.util.Collection;
+
+import lombok.AccessLevel;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.experimental.FieldNameConstants;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacNode;
+import lombok.javac.JavacTreeMaker;
+
+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.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCModifiers;
+import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
+import com.sun.tools.javac.util.List;
+
+@ProviderFor(JavacAnnotationHandler.class) @SuppressWarnings("restriction") public class HandleFieldNameConstants extends JavacAnnotationHandler<FieldNameConstants> {
+
+ public void generateFieldDefaultsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelFieldNameConstants) {
+
+ if (checkForTypeLevelFieldNameConstants) {
+ if (hasAnnotation(FieldNameConstants.class, typeNode)) {
+ return;
+ }
+ }
+
+ 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("@FieldNameConstants is only supported on a class or an enum or a field.");
+ return;
+ }
+
+ for (JavacNode field : typeNode.down()) {
+ if (fieldQualifiesForFieldNameConstantsGeneration(field)) generateFieldNameConstantsForField(field, errorNode.get(), level);
+
+ }
+ }
+
+ private void generateFieldNameConstantsForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
+ if (hasAnnotation(FieldNameConstants.class, fieldNode)) {
+ return;
+ }
+ createFieldNameConstantsForField(level, fieldNode, fieldNode, false, List.<JCAnnotation>nil());
+ }
+
+ private boolean fieldQualifiesForFieldNameConstantsGeneration(JavacNode field) {
+ if (field.getKind() != Kind.FIELD) return false;
+ JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
+ if (fieldDecl.name.toString().startsWith("$")) return false;
+ if ((fieldDecl.mods.flags & Flags.STATIC) != 0) return false;
+ return true;
+ }
+
+ public void handle(AnnotationValues<FieldNameConstants> annotation, JCAnnotation ast, JavacNode annotationNode) {
+
+ Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
+ deleteAnnotationIfNeccessary(annotationNode, FieldNameConstants.class);
+ deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
+ JavacNode node = annotationNode.up();
+ FieldNameConstants annotatationInstance = annotation.getInstance();
+ AccessLevel level = annotatationInstance.level();
+ if (level == AccessLevel.NONE) {
+ annotationNode.addWarning("'lazy' does not work with AccessLevel.NONE.");
+ return;
+ }
+ if (node == null) return;
+ List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@FieldNameConstants(onMethod=", annotationNode);
+ switch (node.getKind()) {
+ case FIELD:
+ createFieldNameConstantsForFields(level, fields, annotationNode, annotationNode, true, onMethod);
+ break;
+ case TYPE:
+ if (!onMethod.isEmpty()) {
+ annotationNode.addError("'onMethod' is not supported for @FieldNameConstants on a type.");
+ }
+ generateFieldDefaultsForType(node, annotationNode, level, false);
+ break;
+ }
+ }
+
+ private void createFieldNameConstantsForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode annotationNode, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod) {
+ for (JavacNode fieldNode : fieldNodes) {
+ createFieldNameConstantsForField(level, fieldNode, errorNode, whineIfExists, onMethod);
+ }
+ }
+
+ private void createFieldNameConstantsForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod) {
+ if (fieldNode.getKind() != Kind.FIELD) {
+ source.addError("@FieldNameConstants is only supported on a class or a field");
+ return;
+ }
+ JCVariableDecl field = (JCVariableDecl) fieldNode.get();
+ String constantName = camelCaseToConstant(field.name.toString());
+ if (constantName == null) {
+ source.addWarning("Not generating constant for this field: It does not fit in your @Accessors prefix list");
+ return;
+ }
+
+ JavacTreeMaker treeMaker = fieldNode.getTreeMaker();
+ JCModifiers modifiers = treeMaker.Modifiers(toJavacModifier(level) | Modifier.STATIC | Modifier.FINAL);
+ JCExpression returnType = chainDots(fieldNode, "java", "lang", "String");
+ JCExpression init = treeMaker.Literal(fieldNode.getName());
+ JCVariableDecl fieldConstant = treeMaker.VarDef(modifiers, fieldNode.toName(constantName), returnType, init);
+ injectField(fieldNode.up(), fieldConstant);
+ }
+
+ public static String camelCaseToConstant(final String fieldName) {
+ if (fieldName == null || fieldName.isEmpty()) return "";
+ char[] chars = fieldName.toCharArray();
+ StringBuilder b = new StringBuilder();
+ b.append(Character.toUpperCase(chars[0]));
+ for (int i = 1, iend = chars.length; i < iend; i++) {
+ char c = chars[i];
+ if (Character.isUpperCase(c)) {
+ b.append('_');
+ } else {
+ c = Character.toUpperCase(c);
+ }
+ b.append(c);
+ }
+ return b.toString();
+ }
+} \ No newline at end of file