diff options
Diffstat (limited to 'src/core/lombok/javac/handlers')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSetter.java | 23 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleWither.java | 274 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 62 |
3 files changed, 324 insertions, 35 deletions
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 02591736..a782e605 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -21,8 +21,8 @@ */ package lombok.javac.handlers; +import static lombok.javac.Javac.getCtcInt; import static lombok.javac.handlers.JavacHandlerUtil.*; -import static lombok.javac.Javac.*; import java.util.Collection; @@ -37,6 +37,7 @@ import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; +import lombok.javac.handlers.JavacHandlerUtil.FieldAccess; import org.mangosdk.spi.ProviderFor; @@ -171,6 +172,11 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { return; } + if ((fieldDecl.mods.flags & Flags.FINAL) != 0) { + source.addWarning("Not generating setter for this field: Setters cannot be generated for final fields."); + return; + } + for (String altName : toAllSetterNames(fieldNode)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: @@ -224,20 +230,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCExpression methodType = null; if (returnThis) { - JavacNode typeNode = field; - while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up(); - if (typeNode != null && typeNode.get() instanceof JCClassDecl) { - JCClassDecl type = (JCClassDecl) typeNode.get(); - ListBuffer<JCExpression> typeArgs = ListBuffer.lb(); - if (!type.typarams.isEmpty()) { - for (JCTypeParameter tp : type.typarams) { - typeArgs.append(treeMaker.Ident(tp.name)); - } - methodType = treeMaker.TypeApply(treeMaker.Ident(type.name), typeArgs.toList()); - } else { - methodType = treeMaker.Ident(type.name); - } - } + methodType = cloneSelfType(field); } if (methodType == null) { diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java new file mode 100644 index 00000000..61bf7bd0 --- /dev/null +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -0,0 +1,274 @@ +/* + * Copyright (C) 2012 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.Javac.getCtcInt; +import static lombok.javac.handlers.JavacHandlerUtil.*; + +import java.util.Collection; + +import lombok.AccessLevel; +import lombok.core.AST.Kind; +import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; +import lombok.experimental.Wither; +import lombok.javac.JavacAnnotationHandler; +import lombok.javac.JavacNode; +import lombok.javac.handlers.JavacHandlerUtil.FieldAccess; + +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.JCConditional; +import com.sun.tools.javac.tree.JCTree.JCExpression; +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCNewClass; +import com.sun.tools.javac.tree.JCTree.JCReturn; +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.tree.TreeMaker; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; +import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; +import com.sun.tools.javac.util.Name; + +/** + * Handles the {@code lombok.experimental.Wither} annotation for javac. + */ +@ProviderFor(JavacAnnotationHandler.class) +public class HandleWither extends JavacAnnotationHandler<Wither> { + public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) { + if (checkForTypeLevelWither) { + if (typeNode != null) for (JavacNode child : typeNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (annotationTypeMatches(Wither.class, child)) { + //The annotation will make it happen, so we can skip it. + 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 | Flags.ENUM)) != 0; + + if (typeDecl == null || notAClass) { + errorNode.addError("@Wither is only supported on a class or a field."); + return; + } + + for (JavacNode field : typeNode.down()) { + if (field.getKind() != Kind.FIELD) continue; + JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); + //Skip fields that start with $ + if (fieldDecl.name.toString().startsWith("$")) continue; + //Skip static fields. + if ((fieldDecl.mods.flags & Flags.STATIC) != 0) continue; + //Skip final initialized fields. + if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) continue; + + generateWitherForField(field, errorNode.get(), level); + } + } + + /** + * Generates a wither on the stated field. + * + * Used by {@link HandleValue}. + * + * The difference between this call and the handle method is as follows: + * + * If there is a {@code lombok.experimental.Wither} annotation on the field, it is used and the + * same rules apply (e.g. warning if the method already exists, stated access level applies). + * If not, the wither is still generated if it isn't already there, though there will not + * be a warning if its already there. The default access level is used. + * + * @param fieldNode The node representing the field you want a wither for. + * @param pos The node responsible for generating the wither (the {@code @Value} or {@code @Wither} annotation). + */ + public void generateWitherForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) { + for (JavacNode child : fieldNode.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (annotationTypeMatches(Wither.class, child)) { + //The annotation will make it happen, so we can skip it. + return; + } + } + } + + createWitherForField(level, fieldNode, fieldNode, false); + } + + @Override public void handle(AnnotationValues<Wither> annotation, JCAnnotation ast, JavacNode annotationNode) { + Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields(); + deleteAnnotationIfNeccessary(annotationNode, Wither.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); + JavacNode node = annotationNode.up(); + AccessLevel level = annotation.getInstance().value(); + + if (level == AccessLevel.NONE || node == null) return; + + switch (node.getKind()) { + case FIELD: + createWitherForFields(level, fields, annotationNode, true); + break; + case TYPE: + generateWitherForType(node, annotationNode, level, false); + break; + } + } + + private void createWitherForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists) { + for (JavacNode fieldNode : fieldNodes) { + createWitherForField(level, fieldNode, errorNode, whineIfExists); + } + } + + private void createWitherForField(AccessLevel level, + JavacNode fieldNode, JavacNode source, boolean whineIfExists) { + + if (fieldNode.getKind() != Kind.FIELD) { + fieldNode.addError("@Wither is only supported on a class or a field."); + return; + } + + JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); + String methodName = toWitherName(fieldNode); + + if (methodName == null) { + source.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list."); + return; + } + + if ((fieldDecl.mods.flags & Flags.STATIC) != 0) { + source.addWarning("Not generating wither for this field: Withers cannot be generated for static fields."); + return; + } + + if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) { + source.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields."); + return; + } + + if (fieldDecl.name.toString().startsWith("$")) { + source.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $."); + return; + } + + for (String altName : toAllWitherNames(fieldNode)) { + switch (methodExists(altName, fieldNode, false, 1)) { + case EXISTS_BY_LOMBOK: + return; + case EXISTS_BY_USER: + if (whineIfExists) { + String altNameExpl = ""; + if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName); + source.addWarning( + String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl)); + } + return; + default: + case NOT_EXISTS: + //continue scanning the other alt names. + } + } + + long access = toJavacModifier(level); + + JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source.get()); + injectMethod(fieldNode.up(), createdWither); + } + + private JCMethodDecl createWither(long access, JavacNode field, TreeMaker treeMaker, JCTree source) { + String witherName = toWitherName(field); + if (witherName == null) return null; + + JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); + + ListBuffer<JCStatement> statements = ListBuffer.lb(); + List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + + Name methodName = field.toName(witherName); + List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables); + + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); + + JCExpression selfType = cloneSelfType(field); + if (selfType == null) return null; + + TreeMaker maker = field.getTreeMaker(); + + ListBuffer<JCExpression> args = ListBuffer.lb(); + for (JavacNode child : field.up().down()) { + if (child.getKind() != Kind.FIELD) continue; + JCVariableDecl childDecl = (JCVariableDecl) child.get(); + // Skip fields that start with $ + if (childDecl.name.toString().startsWith("$")) continue; + long fieldFlags = childDecl.mods.flags; + // Skip static fields. + if ((fieldFlags & Flags.STATIC) != 0) continue; + // Skip initialized final fields. + if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null) continue; + if (child.get() == field.get()) { + args.append(maker.Ident(fieldDecl.name)); + } else { + args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD)); + } + } + + JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null); + JCExpression identityCheck = maker.Binary(getCtcInt(JCTree.class, "EQ"), createFieldAccessor(maker, field, FieldAccess.ALWAYS_FIELD), maker.Ident(fieldDecl.name)); + JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass); + JCReturn returnStatement = maker.Return(conditional); + + if (nonNulls.isEmpty()) { + statements.append(returnStatement); + } else { + JCStatement nullCheck = generateNullCheck(treeMaker, field); + if (nullCheck != null) statements.append(nullCheck); + statements.append(returnStatement); + } + + JCExpression returnType = cloneSelfType(field); + + JCBlock methodBody = treeMaker.Block(0, statements.toList()); + List<JCTypeParameter> methodGenericParams = List.nil(); + List<JCVariableDecl> parameters = List.of(param); + List<JCExpression> throwsClauses = List.nil(); + JCExpression annotationMethodDefaultValue = null; + + List<JCAnnotation> annsOnMethod = List.nil(); + if (isFieldDeprecated(field)) { + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + } + return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, returnType, + methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); + } +} diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 437ba6cb..f2cdee6d 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -61,6 +61,7 @@ import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.JCTree.JCModifiers; import com.sun.tools.javac.tree.JCTree.JCNewArray; 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.tree.TreeMaker; import com.sun.tools.javac.tree.TreeScanner; @@ -307,11 +308,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllGetterNames(JavacNode field) { - String fieldName = field.getName(); - boolean isBoolean = isBoolean(field); - AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - - return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean); + return TransformationsUtil.toAllGetterNames(getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -320,11 +317,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toGetterName(JavacNode field) { - String fieldName = field.getName(); - boolean isBoolean = isBoolean(field); - AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - - return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean); + return TransformationsUtil.toGetterName(getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -332,11 +325,7 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static java.util.List<String> toAllSetterNames(JavacNode field) { - String fieldName = field.getName(); - boolean isBoolean = isBoolean(field); - AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - - return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean); + return TransformationsUtil.toAllSetterNames(getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -345,11 +334,24 @@ public class JavacHandlerUtil { * Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ public static String toSetterName(JavacNode field) { - String fieldName = field.getName(); - boolean isBoolean = isBoolean(field); - AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - - return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean); + return TransformationsUtil.toSetterName(getAccessorsForField(field), field.getName(), isBoolean(field)); + } + + /** + * Translates the given field into all possible wither names. + * Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllWitherNames(JavacNode field) { + return TransformationsUtil.toAllWitherNames(getAccessorsForField(field), field.getName(), isBoolean(field)); + } + + /** + * @return the likely wither name for the stated field. (e.g. private boolean foo; to withFoo). + * + * Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toWitherName(JavacNode field) { + return TransformationsUtil.toWitherName(getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -366,6 +368,26 @@ public class JavacHandlerUtil { return instance.chain() || (instance.fluent() && !forced); } + public static JCExpression cloneSelfType(JavacNode field) { + JavacNode typeNode = field; + TreeMaker maker = field.getTreeMaker(); + while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up(); + if (typeNode != null && typeNode.get() instanceof JCClassDecl) { + JCClassDecl type = (JCClassDecl) typeNode.get(); + ListBuffer<JCExpression> typeArgs = ListBuffer.lb(); + if (!type.typarams.isEmpty()) { + for (JCTypeParameter tp : type.typarams) { + typeArgs.append(maker.Ident(tp.name)); + } + return maker.TypeApply(maker.Ident(type.name), typeArgs.toList()); + } else { + return maker.Ident(type.name); + } + } else { + return null; + } + } + private static boolean isBoolean(JavacNode field) { JCExpression varType = ((JCVariableDecl) field.get()).vartype; return varType != null && varType.toString().equals("boolean"); |