diff options
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleLog.java | 2 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSynchronized.java | 2 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 57 |
3 files changed, 30 insertions, 31 deletions
diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index ee7268f7..06b7c7ef 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -105,7 +105,7 @@ public class HandleLog { maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext()); - injectFieldSuppressWarnings(typeNode, fieldDecl); + injectFieldAndMarkGenerated(typeNode, fieldDecl); return true; } diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java index fb6678e6..20e85d7e 100644 --- a/src/core/lombok/javac/handlers/HandleSynchronized.java +++ b/src/core/lombok/javac/handlers/HandleSynchronized.java @@ -99,7 +99,7 @@ public class HandleSynchronized extends JavacAnnotationHandler<Synchronized> { JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef( maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (isStatic ? Flags.STATIC : 0)), methodNode.toName(lockName), objectType, newObjectArray), ast, context); - injectFieldSuppressWarnings(methodNode.up(), fieldDecl); + injectFieldAndMarkGenerated(methodNode.up(), fieldDecl); } if (method.body == null) return; diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index bf364641..a6bc388a 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -69,7 +69,6 @@ import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCImport; -import com.sun.tools.javac.tree.JCTree.JCLiteral; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.JCTree.JCModifiers; @@ -818,7 +817,7 @@ public class JavacHandlerUtil { * The field carries the @{@link SuppressWarnings}("all") annotation. * Also takes care of updating the JavacAST. */ - public static void injectFieldSuppressWarnings(JavacNode typeNode, JCVariableDecl field) { + public static void injectFieldAndMarkGenerated(JavacNode typeNode, JCVariableDecl field) { injectField(typeNode, field, true); } @@ -831,10 +830,13 @@ public class JavacHandlerUtil { return injectField(typeNode, field, false); } - private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addSuppressWarnings) { + private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addGenerated) { JCClassDecl type = (JCClassDecl) typeNode.get(); - if (addSuppressWarnings) addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext()); + if (addGenerated) { + addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext()); + addGenerated(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext()); + } List<JCTree> insertAfter = null; List<JCTree> insertBefore = type.defs; @@ -950,44 +952,41 @@ public class JavacHandlerUtil { public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return; - for (JCAnnotation ann : mods.annotations) { - JCTree annType = ann.getAnnotationType(); - Name lastPart = null; - if (annType instanceof JCIdent) lastPart = ((JCIdent) annType).name; - else if (annType instanceof JCFieldAccess) lastPart = ((JCFieldAccess) annType).name; - - if (lastPart != null && lastPart.contentEquals("SuppressWarnings")) return; - } - JavacTreeMaker maker = node.getTreeMaker(); - JCExpression suppressWarningsType = genJavaLangTypeRef(node, "SuppressWarnings"); - JCLiteral allLiteral = maker.Literal("all"); - suppressWarningsType.pos = pos; - allLiteral.pos = pos; - JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(suppressWarningsType, List.<JCExpression>of(allLiteral)), source, context); - annotation.pos = pos; - mods.annotations = mods.annotations.append(annotation); + addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all")); } - + public static void addGenerated(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateGenerated()) return; + addAnnotation(mods, node, pos, source, context, "javax.annotation.Generated", node.getTreeMaker().Literal("lombok")); + } + + private static void addAnnotation(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context, String annotationTypeFqn, JCExpression arg) { + boolean isJavaLangBased; + String simpleName; { + int idx = annotationTypeFqn.lastIndexOf('.'); + simpleName = idx == -1 ? annotationTypeFqn : annotationTypeFqn.substring(idx + 1); + + isJavaLangBased = idx == 9 && annotationTypeFqn.regionMatches(0, "java.lang.", 0, 10); + } + for (JCAnnotation ann : mods.annotations) { JCTree annType = ann.getAnnotationType(); Name lastPart = null; if (annType instanceof JCIdent) lastPart = ((JCIdent) annType).name; else if (annType instanceof JCFieldAccess) lastPart = ((JCFieldAccess) annType).name; - - if (lastPart != null && lastPart.contentEquals("Generated")) return; + + if (lastPart != null && lastPart.contentEquals(simpleName)) return; } JavacTreeMaker maker = node.getTreeMaker(); - JCExpression generatedType = chainDots(node, "javax", "annotation", "Generated"); - JCExpression lombokLiteral = maker.Literal("lombok"); - generatedType.pos = pos; - lombokLiteral.pos = pos; - JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(generatedType, List.<JCExpression>of(lombokLiteral)), source, context); + JCExpression annType = isJavaLangBased ? genJavaLangTypeRef(node, simpleName) : chainDotsString(node, annotationTypeFqn); + annType.pos = pos; + if (arg != null) arg.pos = pos; + List<JCExpression> argList = arg != null ? List.of(arg) : List.<JCExpression>nil(); + JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(annType, argList), source, context); annotation.pos = pos; mods.annotations = mods.annotations.append(annotation); } - + private static List<JCTree> addAllButOne(List<JCTree> defs, int idx) { ListBuffer<JCTree> out = new ListBuffer<JCTree>(); int i = 0; |