diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2019-01-08 03:33:24 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2019-01-08 03:33:52 +0100 |
commit | ccfab5ebffbd14df59689b23ff6f3df52890dca8 (patch) | |
tree | 3022d684cae61f0fdb2d59856d72b677f0721a02 /src/utils/lombok/javac/Javac.java | |
parent | 20df86149d3fe45a82d9b83e33b801f5dc7c34ca (diff) | |
download | lombok-ccfab5ebffbd14df59689b23ff6f3df52890dca8.tar.gz lombok-ccfab5ebffbd14df59689b23ff6f3df52890dca8.tar.bz2 lombok-ccfab5ebffbd14df59689b23ff6f3df52890dca8.zip |
[#1033] steps on the way to issue 1033: You can add cleanup tasks which are deferred (during the javac run) until the end.
This already fixes the exotic-to-the-point-of-nonexistent bug where setter and wither compete to steal the `@param` off of the field’s javadoc. Next are to fix builder and setter/wither competing whilst bringing javadocs to `@Builder`.
Then for various other conflicts, we should defer removal of lombok imports and annotations until the end too.
Diffstat (limited to 'src/utils/lombok/javac/Javac.java')
-rw-r--r-- | src/utils/lombok/javac/Javac.java | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java index a2cb32c6..7a264e39 100644 --- a/src/utils/lombok/javac/Javac.java +++ b/src/utils/lombok/javac/Javac.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 The Project Lombok Authors. + * Copyright (C) 2009-2019 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 @@ -28,6 +28,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,17 +43,22 @@ import lombok.javac.JavacTreeMaker.TreeTag; import lombok.javac.JavacTreeMaker.TypeTag; import lombok.permit.Permit; +import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Source; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.main.JavaCompiler; +import com.sun.tools.javac.parser.Tokens.Comment; +import com.sun.tools.javac.tree.DocCommentTable; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCLiteral; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; /** @@ -269,6 +275,73 @@ public class Javac { } } + public static String getDocComment(JCCompilationUnit cu, JCTree node) { + Object dc = getDocComments(cu); + if (dc instanceof Map) return (String) ((Map<?, ?>) dc).get(node); + if (instanceOfDocCommentTable(dc)) return JavadocOps_8.getJavadoc(dc, node); + return null; + } + + @SuppressWarnings("unchecked") + public static void setDocComment(JCCompilationUnit cu, JCTree node, String javadoc) { + Object dc = getDocComments(cu); + if (dc instanceof Map) { + ((Map<JCTree, String>) dc).put(node, javadoc); + return; + } + + if (instanceOfDocCommentTable(dc)) { + JavadocOps_8.setJavadoc(dc, node, javadoc); + return; + } + } + + private static class JavadocOps_8 { + static String getJavadoc(Object dc, JCTree node) { + DocCommentTable dct = (DocCommentTable) dc; + Comment javadoc = dct.getComment(node); + if (javadoc == null) return null; + return javadoc.getText(); + } + + static void setJavadoc(Object dc, JCTree node, String javadoc) { + DocCommentTable dct = (DocCommentTable) dc; + Comment newCmt = createJavadocComment(javadoc, node); + dct.putComment(node, newCmt); + } + + private static Comment createJavadocComment(final String text, final JCTree field) { + return new Comment() { + @Override public String getText() { + return text; + } + + @Override public int getSourcePos(int index) { + return -1; + } + + @Override public CommentStyle getStyle() { + return CommentStyle.JAVADOC; + } + + @Override public boolean isDeprecated() { + return text.contains("@deprecated") && field instanceof JCVariableDecl && isFieldDeprecated(field); + } + }; + } + } + + public static boolean isFieldDeprecated(JCTree field) { + if (!(field instanceof JCVariableDecl)) return false; + JCVariableDecl fieldNode = (JCVariableDecl) field; + if ((fieldNode.mods.flags & Flags.DEPRECATED) != 0) return true; + if (fieldNode.mods.annotations != null) for (JCAnnotation ann : fieldNode.mods.annotations) { + String at = ann.getAnnotationType().toString(); + return at.equals("Deprecated") || at.endsWith(".Deprecated"); + } + return false; + } + public static void initDocComments(JCCompilationUnit cu) { try { JCCOMPILATIONUNIT_DOCCOMMENTS.set(cu, new HashMap<Object, String>()); |