aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2018-07-29 16:37:21 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2018-07-29 16:37:21 +0200
commit3d432c38d0cfeb36b8e7402eca56faaafcf7bac7 (patch)
treee07e107d5ac54c2d91298ca7338621ffeec69a03 /src
parent61fbd4a7f82180871e635a0ca80248e0cec02cf3 (diff)
downloadlombok-3d432c38d0cfeb36b8e7402eca56faaafcf7bac7.tar.gz
lombok-3d432c38d0cfeb36b8e7402eca56faaafcf7bac7.tar.bz2
lombok-3d432c38d0cfeb36b8e7402eca56faaafcf7bac7.zip
code impl of FieldNameConstants rewrite + test updates for it
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/ConfigurationKeys.java11
-rw-r--r--src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java144
-rw-r--r--src/core/lombok/experimental/FieldNameConstants.java28
-rw-r--r--src/core/lombok/javac/handlers/HandleFieldNameConstants.java129
4 files changed, 188 insertions, 124 deletions
diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java
index 1a28c0fa..184ded27 100644
--- a/src/core/lombok/ConfigurationKeys.java
+++ b/src/core/lombok/ConfigurationKeys.java
@@ -529,18 +529,11 @@ public class ConfigurationKeys {
public static final ConfigurationKey<FlagUsageType> FIELD_NAME_CONSTANTS_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.fieldNameConstants.flagUsage", "Emit a warning or error if @FieldNameConstants is used.") {};
/**
- * lombok configuration: {@code lombok.fieldNameConstants.prefix} = &lt;String: aJavaIdentifierPrefix&gt; (Default: {@code PREFIX_}).
+ * lombok configuration: {@code lombok.fieldNameConstants.innerTypeName} = &lt;String: AValidJavaTypeName&gt; (Default: {@code Fields}).
*
* The names of the constants generated by {@code @FieldNameConstants} will be prefixed with this value.
*/
- public static final ConfigurationKey<String> FIELD_NAME_CONSTANTS_PREFIX = new ConfigurationKey<String>("lombok.fieldNameConstants.prefix", "names of constants generated by @FieldNameConstants will be prefixed with this value. (default: 'PREFIX_').") {};
-
- /**
- * lombok configuration: {@code lombok.fieldNameConstants.suffix} = &lt;String: aJavaIdentifierPrefix&gt; (Default: nothing).
- *
- * The names of the constants generated by {@code @FieldNameConstants} will be suffixed with this value.
- */
- public static final ConfigurationKey<String> FIELD_NAME_CONSTANTS_SUFFIX = new ConfigurationKey<String>("lombok.fieldNameConstants.suffix", "names of constants generated by @FieldNameConstants will be suffixed with this value. (default: nothing).") {};
+ public static final ConfigurationKey<String> FIELD_NAME_CONSTANTS_INNER_TYPE_NAME = new ConfigurationKey<String>("lombok.fieldNameConstants.innerTypeName", "The default name of the inner type generated by @FieldNameConstants. (default: 'Fields').") {};
// ----- Wither -----
diff --git a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
index c3a28f7f..15650490 100644
--- a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
+++ b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
@@ -24,32 +24,37 @@ package lombok.eclipse.handlers;
import static lombok.core.handlers.HandlerUtil.handleExperimentalFlagUsage;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
-import java.lang.reflect.Modifier;
-import java.util.Collection;
+import java.util.ArrayList;
+import java.util.List;
import lombok.AccessLevel;
import lombok.ConfigurationKeys;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
-import lombok.core.handlers.HandlerUtil;
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.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
+import org.eclipse.jdt.internal.compiler.ast.Clinit;
+import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.Statement;
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.ClassScope;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.mangosdk.spi.ProviderFor;
@ProviderFor(EclipseAnnotationHandler.class)
public class HandleFieldNameConstants extends EclipseAnnotationHandler<FieldNameConstants> {
- public void generateFieldNameConstantsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level, String prefix, String suffix) {
+ public void generateFieldNameConstantsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level, boolean asEnum, String innerTypeName, boolean onlyExplicit) {
TypeDeclaration typeDecl = null;
if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get();
@@ -57,22 +62,29 @@ public class HandleFieldNameConstants extends EclipseAnnotationHandler<FieldName
boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
if (typeDecl == null || notAClass) {
- errorNode.addError("@FieldNameConstants is only supported on a class, an enum, or a field.");
+ errorNode.addError("@FieldNameConstants is only supported on a class or an enum.");
return;
}
+ List<EclipseNode> qualified = new ArrayList<EclipseNode>();
+
for (EclipseNode field : typeNode.down()) {
- if (fieldQualifiesForFieldNameConstantsGeneration(field)) generateFieldNameConstantsForField(field, errorNode.get(), level, prefix, suffix);
+ if (fieldQualifiesForFieldNameConstantsGeneration(field, onlyExplicit)) qualified.add(field);
+ }
+
+ if (qualified.isEmpty()) {
+ errorNode.addWarning("No fields qualify for @FieldNameConstants, therefore this annotation does nothing");
+ } else {
+ createInnerTypeFieldNameConstants(typeNode, errorNode.get(), level, qualified, asEnum, innerTypeName);
}
}
- private void generateFieldNameConstantsForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, String prefix, String suffix) {
- if (hasAnnotation(FieldNameConstants.class, fieldNode)) return;
- createFieldNameConstantsForField(level, prefix, suffix, fieldNode, fieldNode, pos, false);
- }
-
- private boolean fieldQualifiesForFieldNameConstantsGeneration(EclipseNode field) {
+ private boolean fieldQualifiesForFieldNameConstantsGeneration(EclipseNode field, boolean onlyExplicit) {
if (field.getKind() != Kind.FIELD) return false;
+ if (hasAnnotation(FieldNameConstants.Exclude.class, field)) return false;
+ if (hasAnnotation(FieldNameConstants.Include.class, field)) return true;
+ if (onlyExplicit) return false;
+
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
return filterField(fieldDecl);
}
@@ -81,55 +93,75 @@ public class HandleFieldNameConstants extends EclipseAnnotationHandler<FieldName
handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_NAME_CONSTANTS_FLAG_USAGE, "@FieldNameConstants");
EclipseNode node = annotationNode.up();
- FieldNameConstants annotatationInstance = annotation.getInstance();
- AccessLevel level = annotatationInstance.level();
- String prefix = annotatationInstance.prefix();
- String suffix = annotatationInstance.suffix();
- if (prefix.equals(" CONFIG DEFAULT ")) prefix = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_PREFIX);
- if (suffix.equals(" CONFIG DEFAULT ")) suffix = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_SUFFIX);
- if (prefix == null) prefix = "FIELD_";
- if (suffix == null) suffix = "";
- if (node == null) return;
-
- switch (node.getKind()) {
- case FIELD:
- if (level != AccessLevel.NONE) createFieldNameConstantsForFields(level, prefix, suffix, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true);
- break;
- case TYPE:
- if (level == AccessLevel.NONE) {
- annotationNode.addWarning("type-level '@FieldNameConstants' does not work with AccessLevel.NONE.");
- return;
- }
- generateFieldNameConstantsForType(node, annotationNode, level, prefix, suffix);
- break;
- }
- }
-
- private void createFieldNameConstantsForFields(AccessLevel level, String prefix, String suffix, Collection<EclipseNode> fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists) {
- for (EclipseNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, prefix, suffix, fieldNode, errorNode, source, whineIfExists);
- }
-
- private void createFieldNameConstantsForField(AccessLevel level, String prefix, String suffix, EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists) {
- if (fieldNode.getKind() != Kind.FIELD) {
- errorNode.addError("@FieldNameConstants is only supported on a class, an enum, or a field");
+ FieldNameConstants annotationInstance = annotation.getInstance();
+ AccessLevel level = annotationInstance.level();
+ boolean asEnum = annotationInstance.asEnum();
+ boolean usingLombokv1_18_2 = annotation.isExplicit("prefix") || annotation.isExplicit("suffix") || node.getKind() == Kind.FIELD;
+
+ if (usingLombokv1_18_2) {
+ annotationNode.addError("@FieldNameConstants has been redesigned in lombok v1.18.4; please upgrade your project dependency on lombok. See https://projectlombok.org/features/experimental/FieldNameConstants for more information.");
return;
}
- FieldDeclaration field = (FieldDeclaration) fieldNode.get();
- String fieldName = new String(field.name);
- String constantName = prefix + HandlerUtil.camelCaseToConstant(fieldName) + suffix;
- if (constantName.equals(fieldName)) {
- fieldNode.addWarning("Not generating constant for this field: The name of the constant would be equal to the name of this field.");
+ if (level == AccessLevel.NONE) {
+ annotationNode.addWarning("AccessLevel.NONE is not compatible with @FieldNameConstants. If you don't want the inner type, simply remove FieldNameConstants.");
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);
+ String innerTypeName = annotationInstance.innerTypeName();
+ if (innerTypeName.isEmpty()) innerTypeName = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_INNER_TYPE_NAME);
+ if (innerTypeName == null || innerTypeName.isEmpty()) innerTypeName = "Fields";
+
+ generateFieldNameConstantsForType(node, annotationNode, level, asEnum, innerTypeName, annotationInstance.onlyExplicitlyIncluded());
+ }
+
+ private void createInnerTypeFieldNameConstants(EclipseNode typeNode, ASTNode source, AccessLevel level, List<EclipseNode> fields, boolean asEnum, String innerTypeName) {
+ if (fields.isEmpty()) return;
+
+ TypeDeclaration parent = (TypeDeclaration) typeNode.get();
+ TypeDeclaration innerType = new TypeDeclaration(parent.compilationResult);
+ innerType.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ innerType.modifiers = toEclipseModifier(level) | (asEnum ? ClassFileConstants.AccEnum : ClassFileConstants.AccStatic | ClassFileConstants.AccFinal);
+ char[] name = innerTypeName.toCharArray();
+ innerType.name = name;
+ innerType.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
+ EclipseNode innerNode = injectType(typeNode, innerType);
+
+ ConstructorDeclaration constructor = new ConstructorDeclaration(parent.compilationResult);
+ constructor.selector = name;
+ constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
+ constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
+ constructor.modifiers = ClassFileConstants.AccPrivate;
+ ExplicitConstructorCall superCall = new ExplicitConstructorCall(0);
+ superCall.sourceStart = source.sourceStart;
+ superCall.sourceEnd = source.sourceEnd;
+ superCall.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ constructor.constructorCall = superCall;
+ if (!asEnum) constructor.statements = new Statement[0];
+
+ injectMethod(innerNode, constructor);
+
+ if (asEnum) injectMethod(innerNode, new Clinit(parent.compilationResult));
+
+ for (EclipseNode fieldNode : fields) {
+ FieldDeclaration field = (FieldDeclaration) fieldNode.get();
+ char[] fName = field.name;
+ int pS = source.sourceStart, pE = source.sourceEnd;
+ long p = (long) pS << 32 | pE;
+ FieldDeclaration fieldConstant = new FieldDeclaration(fName, pS, pE);
+ fieldConstant.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ fieldConstant.modifiers = asEnum ? 0 : ClassFileConstants.AccPublic | ClassFileConstants.AccStatic | ClassFileConstants.AccFinal;
+ fieldConstant.type = asEnum ? null : new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[] {p, p, p});
+ if (asEnum) {
+ AllocationExpression ac = new AllocationExpression();
+ ac.enumConstant = fieldConstant;
+ ac.sourceStart = source.sourceStart;
+ ac.sourceEnd = source.sourceEnd;
+ fieldConstant.initialization = ac;
+ } else {
+ fieldConstant.initialization = new StringLiteral(field.name, pS, pE, 0);
+ }
+ injectField(innerNode, fieldConstant);
+ }
}
}
diff --git a/src/core/lombok/experimental/FieldNameConstants.java b/src/core/lombok/experimental/FieldNameConstants.java
index 31c2970c..d6886890 100644
--- a/src/core/lombok/experimental/FieldNameConstants.java
+++ b/src/core/lombok/experimental/FieldNameConstants.java
@@ -29,12 +29,32 @@ import java.lang.annotation.Target;
import lombok.AccessLevel;
/**
- * Generates String constants containing the field name for each field.
+ * Generates an inner type, containing String constants containing the field name for each field. Alternatively, generates an inner enum with enum values matching each field name.
*/
-@Target({ElementType.TYPE, ElementType.FIELD})
+@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface FieldNameConstants {
lombok.AccessLevel level() default AccessLevel.PUBLIC;
- String prefix() default " CONFIG DEFAULT ";
- String suffix() default " CONFIG DEFAULT ";
+ boolean asEnum() default false;
+ String innerTypeName() default "";
+
+ /**
+ * Only include fields and methods explicitly marked with {@code @FieldNameConstants.Include}.
+ * Normally, all (non-static) fields are included by default.
+ */
+ boolean onlyExplicitlyIncluded() default false;
+
+ /**
+ * If present, do not include this field in the generated fieldnames inner type.
+ */
+ @Target(ElementType.FIELD)
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface Exclude {}
+
+ /**
+ * If present, include this field in the generated fieldnames inner type (default).
+ */
+ @Target(ElementType.FIELD)
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface Include {}
}
diff --git a/src/core/lombok/javac/handlers/HandleFieldNameConstants.java b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java
index 8ff136fc..5b120948 100644
--- a/src/core/lombok/javac/handlers/HandleFieldNameConstants.java
+++ b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java
@@ -24,13 +24,11 @@ package lombok.javac.handlers;
import static lombok.core.handlers.HandlerUtil.handleExperimentalFlagUsage;
import static lombok.javac.handlers.JavacHandlerUtil.*;
-import java.lang.reflect.Modifier;
-import java.util.Collection;
+import java.util.ArrayList;
import lombok.AccessLevel;
import lombok.ConfigurationKeys;
import lombok.core.AST.Kind;
-import lombok.core.handlers.HandlerUtil;
import lombok.core.AnnotationValues;
import lombok.experimental.FieldNameConstants;
import lombok.javac.JavacAnnotationHandler;
@@ -40,16 +38,23 @@ import lombok.javac.JavacTreeMaker;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
+import com.sun.tools.javac.tree.JCTree.JCStatement;
+import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Name;
@ProviderFor(JavacAnnotationHandler.class)
public class HandleFieldNameConstants extends JavacAnnotationHandler<FieldNameConstants> {
- public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, String prefix, String suffix) {
+ public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean asEnum, String innerTypeName, boolean onlyExplicit) {
JCClassDecl typeDecl = null;
if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
@@ -57,22 +62,31 @@ public class HandleFieldNameConstants extends JavacAnnotationHandler<FieldNameCo
boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
if (typeDecl == null || notAClass) {
- errorNode.addError("@FieldNameConstants is only supported on a class, an enum, or a field.");
+ errorNode.addError("@FieldNameConstants is only supported on a class or an enum.");
return;
}
+ java.util.List<JavacNode> qualified = new ArrayList<JavacNode>();
+
for (JavacNode field : typeNode.down()) {
- if (fieldQualifiesForFieldNameConstantsGeneration(field)) generateFieldNameConstantsForField(field, errorNode.get(), level, prefix, suffix);
+ if (fieldQualifiesForFieldNameConstantsGeneration(field, onlyExplicit)) qualified.add(field);
+ }
+
+ if (qualified.isEmpty()) {
+ errorNode.addWarning("No fields qualify for @FieldNameConstants, therefore this annotation does nothing");
+ } else {
+ createInnerTypeFieldNameConstants(typeNode, errorNode.get(), level, qualified, asEnum, innerTypeName);
}
}
- private void generateFieldNameConstantsForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, String prefix, String suffix) {
- if (hasAnnotation(FieldNameConstants.class, fieldNode)) return;
- createFieldNameConstantsForField(level, prefix, suffix, fieldNode, fieldNode, false);
- }
-
- private boolean fieldQualifiesForFieldNameConstantsGeneration(JavacNode field) {
+ private boolean fieldQualifiesForFieldNameConstantsGeneration(JavacNode field, boolean onlyExplicit) {
if (field.getKind() != Kind.FIELD) return false;
+ boolean exclAnn = JavacHandlerUtil.hasAnnotationAndDeleteIfNeccessary(FieldNameConstants.Exclude.class, field);
+ boolean inclAnn = JavacHandlerUtil.hasAnnotationAndDeleteIfNeccessary(FieldNameConstants.Include.class, field);
+ if (exclAnn) return false;
+ if (inclAnn) return true;
+ if (onlyExplicit) return false;
+
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
if (fieldDecl.name.toString().startsWith("$")) return false;
if ((fieldDecl.mods.flags & Flags.STATIC) != 0) return false;
@@ -82,56 +96,61 @@ public class HandleFieldNameConstants extends JavacAnnotationHandler<FieldNameCo
public void handle(AnnotationValues<FieldNameConstants> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_NAME_CONSTANTS_FLAG_USAGE, "@FieldNameConstants");
- 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();
- String prefix = annotatationInstance.prefix();
- String suffix = annotatationInstance.suffix();
- if (prefix.equals(" CONFIG DEFAULT ")) prefix = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_PREFIX);
- if (suffix.equals(" CONFIG DEFAULT ")) suffix = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_SUFFIX);
- if (prefix == null) prefix = "FIELD_";
- if (suffix == null) suffix = "";
- if (node == null) return;
- switch (node.getKind()) {
- case FIELD:
- if (level != AccessLevel.NONE) createFieldNameConstantsForFields(level, prefix, suffix, fields, annotationNode, annotationNode, true);
- break;
- case TYPE:
- if (level == AccessLevel.NONE) {
- annotationNode.addWarning("type-level '@FieldNameConstants' does not work with AccessLevel.NONE.");
- return;
- }
- generateFieldNameConstantsForType(node, annotationNode, level, prefix, suffix);
- break;
- }
- }
-
- private void createFieldNameConstantsForFields(AccessLevel level, String prefix, String suffix, Collection<JavacNode> fieldNodes, JavacNode annotationNode, JavacNode errorNode, boolean whineIfExists) {
- for (JavacNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, prefix, suffix, fieldNode, errorNode, whineIfExists);
- }
-
- private void createFieldNameConstantsForField(AccessLevel level, String prefix, String suffix, JavacNode fieldNode, JavacNode source, boolean whineIfExists) {
- if (fieldNode.getKind() != Kind.FIELD) {
- source.addError("@FieldNameConstants is only supported on a class, an enum, or a field");
+ FieldNameConstants annotationInstance = annotation.getInstance();
+ AccessLevel level = annotationInstance.level();
+ boolean asEnum = annotationInstance.asEnum();
+ boolean usingLombokv1_18_2 = annotation.isExplicit("prefix") || annotation.isExplicit("suffix") || node.getKind() == Kind.FIELD;
+
+ if (usingLombokv1_18_2) {
+ annotationNode.addError("@FieldNameConstants has been redesigned in lombok v1.18.4; please upgrade your project dependency on lombok. See https://projectlombok.org/features/experimental/FieldNameConstants for more information.");
return;
}
- JCVariableDecl field = (JCVariableDecl) fieldNode.get();
- String fieldName = field.name.toString();
- String constantName = prefix + HandlerUtil.camelCaseToConstant(fieldName) + suffix;
- if (constantName.equals(fieldName)) {
- fieldNode.addWarning("Not generating constant for this field: The name of the constant would be equal to the name of this field.");
+
+ if (level == AccessLevel.NONE) {
+ annotationNode.addWarning("AccessLevel.NONE is not compatible with @FieldNameConstants. If you don't want the inner type, simply remove @FieldNameConstants.");
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);
+ String innerTypeName = annotationInstance.innerTypeName();
+ if (innerTypeName.isEmpty()) innerTypeName = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_INNER_TYPE_NAME);
+ if (innerTypeName == null || innerTypeName.isEmpty()) innerTypeName = "Fields";
+
+ generateFieldNameConstantsForType(node, annotationNode, level, asEnum, innerTypeName, annotationInstance.onlyExplicitlyIncluded());
+ }
+
+ private void createInnerTypeFieldNameConstants(JavacNode typeNode, DiagnosticPosition pos, AccessLevel level, java.util.List<JavacNode> fields, boolean asEnum, String innerTypeName) {
+ if (fields.isEmpty()) return;
+
+ JavacTreeMaker maker = typeNode.getTreeMaker();
+ JCModifiers mods = maker.Modifiers(toJavacModifier(level) | (asEnum ? Flags.ENUM : Flags.STATIC | Flags.FINAL));
+
+ Name fieldsName = typeNode.toName(innerTypeName);
+ JCClassDecl innerType = maker.ClassDef(mods, fieldsName, List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), List.<JCTree>nil());
+ JavacNode innerNode = injectType(typeNode, innerType);
+
+ JCModifiers genConstrMods = maker.Modifiers(Flags.GENERATEDCONSTR | (asEnum ? 0L : Flags.PRIVATE));
+ JCBlock genConstrBody = maker.Block(0L, List.<JCStatement>of(maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("super")), List.<JCExpression>nil()))));
+ JCMethodDecl genConstr = maker.MethodDef(genConstrMods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), genConstrBody, null);
+
+ injectMethod(innerNode, genConstr);
+
+ for (JavacNode field : fields) {
+ JCModifiers enumValueMods = maker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL | (asEnum ? Flags.ENUM : 0L));
+ JCExpression returnType;
+ JCExpression init;
+ if (asEnum) {
+ returnType = maker.Ident(fieldsName);
+ init = maker.NewClass(null, List.<JCExpression>nil(), maker.Ident(fieldsName), List.<JCExpression>nil(), null);
+ } else {
+ returnType = chainDots(field, "java", "lang", "String");
+ init = maker.Literal(field.getName());
+ }
+ JCVariableDecl enumField = maker.VarDef(enumValueMods, ((JCVariableDecl) field.get()).name, returnType, init);
+ injectField(innerNode, enumField);
+ }
}
-} \ No newline at end of file
+}