aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/eclipse
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java2
-rw-r--r--src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java107
2 files changed, 44 insertions, 65 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 6617d21a..c49107e5 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -1097,7 +1097,7 @@ public class EclipseHandlerUtil {
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;
+ ((AllocationExpression) declaration.initialization).enumConstant != null) return false;
if (declaration.type == null) return false;
diff --git a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
index 597b0937..754ddf47 100644
--- a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
+++ b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012-2014 The Project Lombok Authors.
+ * Copyright (C) 2014-2018 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
@@ -21,15 +21,17 @@
*/
package lombok.eclipse.handlers;
-import static java.lang.Character.*;
+import static lombok.core.handlers.HandlerUtil.handleExperimentalFlagUsage;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
import java.util.Collection;
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;
@@ -45,16 +47,9 @@ 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;
- }
- }
-
+@ProviderFor(EclipseAnnotationHandler.class)
+public class HandleFieldNameConstants extends EclipseAnnotationHandler<FieldNameConstants> {
+ public void generateFieldNameConstantsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level) {
TypeDeclaration typeDecl = null;
if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get();
@@ -62,45 +57,20 @@ import org.mangosdk.spi.ProviderFor;
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;
+ errorNode.addError("@FieldNameConstants is only supported on a class, an enum, or a field.");
+ return;
}
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;
- }
+ 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();
@@ -108,43 +78,52 @@ import org.mangosdk.spi.ProviderFor;
}
public void handle(AnnotationValues<FieldNameConstants> annotation, Annotation ast, EclipseNode annotationNode) {
+ handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_NAME_CONSTANTS_FLAG_USAGE, "@FieldNameConstants");
+
EclipseNode node = annotationNode.up();
FieldNameConstants annotatationInstance = annotation.getInstance();
AccessLevel level = annotatationInstance.level();
if (node == null) return;
- switch (node.getKind()){
+
+ switch (node.getKind()) {
case FIELD:
- createFieldNameConstantsForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true);
+ if (level != AccessLevel.NONE) createFieldNameConstantsForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true);
break;
case TYPE:
- generateFieldDefaultsForType(node, annotationNode, level, false);
+ if (level == AccessLevel.NONE) {
+ annotationNode.addWarning("type-level '@FieldNameConstants' does not work with AccessLevel.NONE.");
+ return;
+ }
+ generateFieldNameConstantsForType(node, annotationNode, level);
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);
- }
+ 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);
+
+ 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, an enum, or a field");
+ return;
}
- return b.toString();
+
+ FieldDeclaration field = (FieldDeclaration) fieldNode.get();
+ String fieldName = new String(field.name);
+ String constantName = HandlerUtil.camelCaseToConstant(fieldName);
+ 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);
}
-
}