diff options
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 74 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleGetter.java | 10 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSetter.java | 8 |
3 files changed, 84 insertions, 8 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 56f51573..ff743601 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -41,11 +41,12 @@ import lombok.Data; import lombok.Getter; import lombok.Lombok; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.core.AnnotationValues.AnnotationValue; import lombok.eclipse.EclipseAST; import lombok.eclipse.EclipseNode; -import lombok.core.TransformationsUtil; +import lombok.experimental.Accessors; import lombok.core.TypeResolver; import org.eclipse.core.runtime.ILog; @@ -777,7 +778,7 @@ public class EclipseHandlerUtil { TypeReference fieldType = ((FieldDeclaration)field.get()).type; boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; EclipseNode typeNode = field.up(); - for (String potentialGetterName : TransformationsUtil.toAllGetterNames(field.getName(), isBoolean)) { + for (String potentialGetterName : toAllGetterNames(field, isBoolean)) { for (EclipseNode potentialGetter : typeNode.down()) { if (potentialGetter.getKind() != Kind.METHOD) continue; if (!(potentialGetter.get() instanceof MethodDeclaration)) continue; @@ -820,7 +821,8 @@ public class EclipseHandlerUtil { } if (hasGetterAnnotation) { - String getterName = TransformationsUtil.toGetterName(field.getName(), isBoolean); + String getterName = toGetterName(field, isBoolean); + if (getterName == null) return null; return new GetterMethod(getterName.toCharArray(), fieldType); } @@ -930,6 +932,52 @@ public class EclipseHandlerUtil { } /** + * Translates the given field into all possible getter names. + * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) { + String fieldName = field.getName(); + AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean); + } + + /** + * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo). + * + * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toGetterName(EclipseNode field, boolean isBoolean) { + String fieldName = field.getName(); + AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean); + } + + /** + * Translates the given field into all possible setter names. + * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllSetterNames(EclipseNode field, boolean isBoolean) { + String fieldName = field.getName(); + AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean); + } + + /** + * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). + * + * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toSetterName(EclipseNode field, boolean isBoolean) { + String fieldName = field.getName(); + AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + + return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean); + } + + /** * Checks if the field should be included in operations that work on 'all' fields: * If the field is static, or starts with a '$', or is actually an enum constant, 'false' is returned, indicating you should skip it. */ @@ -949,6 +997,26 @@ public class EclipseHandlerUtil { return true; } + public static AnnotationValues<Accessors> getAccessorsForField(EclipseNode field) { + for (EclipseNode node : field.down()) { + if (annotationTypeMatches(Accessors.class, node)) { + return createAnnotation(Accessors.class, node); + } + } + + EclipseNode current = field.up(); + while (current != null) { + for (EclipseNode node : field.down()) { + if (annotationTypeMatches(Accessors.class, node)) { + return createAnnotation(Accessors.class, node); + } + } + current = current.up(); + } + + return AnnotationValues.of(Accessors.class, field); + } + /** * Checks if there is a field with the provided name. * diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 5aeda5a5..d53bf89b 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -187,13 +187,17 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { } TypeReference fieldType = copyType(field.type, source); - String fieldName = new String(field.name); boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; - String getterName = TransformationsUtil.toGetterName(fieldName, isBoolean); + String getterName = toGetterName(fieldNode, isBoolean); + + if (getterName == null) { + errorNode.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list."); + return; + } int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); - for (String altName : TransformationsUtil.toAllGetterNames(fieldName, isBoolean)) { + for (String altName : toAllGetterNames(fieldNode, isBoolean)) { switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 01926155..3fa872f9 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -148,11 +148,15 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); TypeReference fieldType = copyType(field.type, source); boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; - String setterName = TransformationsUtil.toSetterName(new String(field.name), isBoolean); + String setterName = toSetterName(fieldNode, isBoolean); + if (setterName == null) { + errorNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list."); + return; + } int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); - for (String altName : TransformationsUtil.toAllSetterNames(new String(field.name), isBoolean)) { + for (String altName : toAllSetterNames(fieldNode, isBoolean)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; |