mirror of
https://github.com/tiennm99/lombok.git
synced 2026-09-20 04:23:33 +00:00
code impl of FieldNameConstants rewrite + test updates for it
This commit is contained in:
1 parent
61fbd4a7f8
commit
3d432c38d0
18 files changed
+277
-181
No files matched your search
@@ -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} = <String: aJavaIdentifierPrefix> (Default: {@code PREFIX_}).
|
||||
* lombok configuration: {@code lombok.fieldNameConstants.innerTypeName} = <String: AValidJavaTypeName> (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} = <String: aJavaIdentifierPrefix> (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 -----
|
||||
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
generateFieldNameConstantsForType(node, annotationNode, level, prefix, suffix);
|
||||
break;
|
||||
injectField(innerNode, fieldConstant);
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
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.");
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
}
|
||||
@@ -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;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
generateFieldNameConstantsForType(node, annotationNode, level, prefix, suffix);
|
||||
break;
|
||||
JCVariableDecl enumField = maker.VarDef(enumValueMods, ((JCVariableDecl) field.get()).name, returnType, init);
|
||||
injectField(innerNode, enumField);
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
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.");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
public class FieldNameConstantsBasic {
|
||||
protected static final java.lang.String FIELD_I_AM_A_DVD_PLAYER = "iAmADvdPlayer";
|
||||
public static final java.lang.String FIELD_BUT_PRINT_ME_PLEASE = "butPrintMePlease";
|
||||
String iAmADvdPlayer;
|
||||
int $skipMe;
|
||||
static double skipMeToo;
|
||||
int andMe;
|
||||
String butPrintMePlease;
|
||||
@java.lang.SuppressWarnings("all")
|
||||
static final class Fields {
|
||||
public static final java.lang.String butPrintMePlease = "butPrintMePlease";
|
||||
public static final java.lang.String iAmADvdPlayer = "iAmADvdPlayer";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
public class FieldNameConstantsConfigKeys {
|
||||
public static final java.lang.String I_AM_A_DVD_PLAYER_SFX = "iAmADvdPlayer";
|
||||
String iAmADvdPlayer;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public static final class Foobar {
|
||||
public static final java.lang.String iAmADvdPlayer = "iAmADvdPlayer";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
public class FieldNameConstantsEnum {
|
||||
String iAmADvdPlayer;
|
||||
int $dontSkipMe;
|
||||
static double alsoDontSkipMe;
|
||||
int butSkipMe;
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public enum TypeTest {
|
||||
iAmADvdPlayer, $dontSkipMe, alsoDontSkipMe;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
public class FieldNameConstantsWeird {
|
||||
public static final java.lang.String FIELD_AZ = "A";
|
||||
String iAmADvdPlayer;
|
||||
String X;
|
||||
String A;
|
||||
}
|
||||
@@ -1,15 +1,23 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
public @FieldNameConstants class FieldNameConstantsBasic {
|
||||
public static final java.lang.String FIELD_BUT_PRINT_ME_PLEASE = "butPrintMePlease";
|
||||
protected static final java.lang.String FIELD_I_AM_A_DVD_PLAYER = "iAmADvdPlayer";
|
||||
@FieldNameConstants(level = AccessLevel.PROTECTED) String iAmADvdPlayer;
|
||||
public @FieldNameConstants(level = AccessLevel.PACKAGE) class FieldNameConstantsBasic {
|
||||
static final @java.lang.SuppressWarnings("all") class Fields {
|
||||
public static final java.lang.String butPrintMePlease = "butPrintMePlease";
|
||||
public static final java.lang.String iAmADvdPlayer = "iAmADvdPlayer";
|
||||
<clinit>() {
|
||||
}
|
||||
private @java.lang.SuppressWarnings("all") Fields() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
String iAmADvdPlayer;
|
||||
int $skipMe;
|
||||
static double skipMeToo;
|
||||
@FieldNameConstants.Exclude int andMe;
|
||||
String butPrintMePlease;
|
||||
<clinit>() {
|
||||
}
|
||||
public FieldNameConstantsBasic() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
public @lombok.experimental.FieldNameConstants class FieldNameConstantsConfigKeys {
|
||||
public static final java.lang.String I_AM_A_DVD_PLAYER_SFX = "iAmADvdPlayer";
|
||||
String iAmADvdPlayer;
|
||||
<clinit>() {
|
||||
public static final @java.lang.SuppressWarnings("all") class Foobar {
|
||||
public static final java.lang.String iAmADvdPlayer = "iAmADvdPlayer";
|
||||
<clinit>() {
|
||||
}
|
||||
private @java.lang.SuppressWarnings("all") Foobar() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
String iAmADvdPlayer;
|
||||
public FieldNameConstantsConfigKeys() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
public @FieldNameConstants(onlyExplicitlyIncluded = true,asEnum = true,innerTypeName = "TypeTest") class FieldNameConstantsEnum {
|
||||
public @java.lang.SuppressWarnings("all") enum TypeTest {
|
||||
iAmADvdPlayer(),
|
||||
$dontSkipMe(),
|
||||
alsoDontSkipMe(),
|
||||
private @java.lang.SuppressWarnings("all") TypeTest() {
|
||||
super();
|
||||
}
|
||||
<clinit>() {
|
||||
}
|
||||
}
|
||||
@FieldNameConstants.Include String iAmADvdPlayer;
|
||||
@FieldNameConstants.Include int $dontSkipMe;
|
||||
static @FieldNameConstants.Include double alsoDontSkipMe;
|
||||
int butSkipMe;
|
||||
<clinit>() {
|
||||
}
|
||||
public FieldNameConstantsEnum() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
public @FieldNameConstants class FieldNameConstantsWeird {
|
||||
public static final java.lang.String FIELD_AZ = "A";
|
||||
@FieldNameConstants(level = AccessLevel.NONE) String iAmADvdPlayer;
|
||||
@FieldNameConstants(prefix = "") String X;
|
||||
@FieldNameConstants(suffix = "Z") String A;
|
||||
<clinit>() {
|
||||
}
|
||||
public FieldNameConstantsWeird() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
|
||||
@FieldNameConstants
|
||||
@FieldNameConstants(level = AccessLevel.PACKAGE)
|
||||
public class FieldNameConstantsBasic {
|
||||
@FieldNameConstants(level = AccessLevel.PROTECTED)
|
||||
String iAmADvdPlayer;
|
||||
int $skipMe;
|
||||
static double skipMeToo;
|
||||
@FieldNameConstants.Exclude int andMe;
|
||||
String butPrintMePlease;
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
//CONF: lombok.fieldNameConstants.prefix =
|
||||
//CONF: lombok.fieldNameConstants.suffix = _SFX
|
||||
//CONF: lombok.fieldNameConstants.innerTypeName = Foobar
|
||||
|
||||
@lombok.experimental.FieldNameConstants
|
||||
public class FieldNameConstantsConfigKeys {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
|
||||
@FieldNameConstants(onlyExplicitlyIncluded = true, asEnum = true, innerTypeName = "TypeTest")
|
||||
public class FieldNameConstantsEnum {
|
||||
@FieldNameConstants.Include
|
||||
String iAmADvdPlayer;
|
||||
@FieldNameConstants.Include
|
||||
int $dontSkipMe;
|
||||
@FieldNameConstants.Include
|
||||
static double alsoDontSkipMe;
|
||||
int butSkipMe;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import lombok.AccessLevel;
|
||||
|
||||
@FieldNameConstants
|
||||
public class FieldNameConstantsWeird {
|
||||
@FieldNameConstants(level = AccessLevel.NONE)
|
||||
String iAmADvdPlayer;
|
||||
@FieldNameConstants(prefix = "")
|
||||
String X;
|
||||
@FieldNameConstants(suffix = "Z")
|
||||
String A;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
9 Not generating constant for this field: The name of the constant would be equal to the name of this field.
|
||||
@@ -1 +0,0 @@
|
||||
9 Not generating constant for this field: The name of the constant would be equal to the name of this field.
|
||||
Reference in new issue
Block a user