diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2020-07-29 12:55:02 +0200 |
---|---|---|
committer | Rawi01 <Rawi01@users.noreply.github.com> | 2020-07-29 13:39:42 +0200 |
commit | 2b4b5c983540af4a4c08cfb7a3d4057df0b84c39 (patch) | |
tree | 78a310e0f6d611d2113caf9a4b0a38889ac1926f /src/eclipseAgent | |
parent | 7dfbe4323c15cbd88983380b27a250d2a381d148 (diff) | |
download | lombok-2b4b5c983540af4a4c08cfb7a3d4057df0b84c39.tar.gz lombok-2b4b5c983540af4a4c08cfb7a3d4057df0b84c39.tar.bz2 lombok-2b4b5c983540af4a4c08cfb7a3d4057df0b84c39.zip |
Support javadoc copying in eclipse
Diffstat (limited to 'src/eclipseAgent')
3 files changed, 193 insertions, 0 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index c560f002..6a271973 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -126,6 +126,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { patchExtensionMethod(sm, ecjOnly); patchRenameField(sm); patchNullCheck(sm); + patchJavadoc(sm); if (reloadExistingClasses) sm.reloadClasses(instrumentation); } @@ -830,5 +831,37 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .request(StackRequest.PARAM1) .transplant().build()); } + + private static void patchJavadoc(ScriptManager sm) { + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2", "getHTMLContent", "java.lang.String", "org.eclipse.jdt.core.IJavaElement", "boolean")) + .methodToWrap(new Hook("org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2", "getHTMLContentFromSource", "java.lang.String", "org.eclipse.jdt.core.IJavaElement")) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "org.eclipse.jdt.core.IJavaElement")) + .requestExtra(StackRequest.PARAM1) + .build()); + + /* This is an older version that uses IMember instead of IJavaElement */ + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2", "getHTMLContent", "java.lang.String", "org.eclipse.jdt.core.IMember", "boolean")) + .methodToWrap(new Hook("org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2", "getHTMLContentFromSource", "java.lang.String", "org.eclipse.jdt.core.IMember")) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "org.eclipse.jdt.core.IJavaElement")) + .requestExtra(StackRequest.PARAM1) + .build()); + + sm.addScript(ScriptBuilder.replaceMethodCall() + .target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.TypeDeclaration", "printBody", "java.lang.StringBuffer", "int", "java.lang.StringBuffer")) + .methodToReplace(new Hook("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print", "java.lang.StringBuffer", "int", "java.lang.StringBuffer")) + .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "printMethod", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "int", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.TypeDeclaration")) + .requestExtra(StackRequest.THIS) + .build()); + + sm.addScript(ScriptBuilder.addField() + .fieldName("$javadoc") + .fieldType("Ljava/util/Map;") + .setPublic() + .setTransient() + .targetClass("org.eclipse.jdt.internal.core.CompilationUnit") + .build()); + } } diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java new file mode 100644 index 00000000..a91e4d8b --- /dev/null +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2020 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.eclipse.agent; + +import static lombok.eclipse.EclipseAugments.CompilationUnit_javadoc; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMember; +import org.eclipse.jdt.internal.compiler.ast.ASTNode; +import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; +import org.eclipse.jdt.internal.core.CompilationUnit; +import org.eclipse.jdt.internal.core.SourceMethod; +import org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2; + +import lombok.eclipse.EclipseAugments; +import lombok.eclipse.handlers.EclipseHandlerUtil; +import lombok.permit.Permit; + +public class PatchJavadoc { + + public static String getHTMLContentFromSource(String original, IJavaElement member) { + if (original != null) { + return original; + } + + if (member instanceof SourceMethod) { + SourceMethod sourceMethod = (SourceMethod) member; + ICompilationUnit iCompilationUnit = sourceMethod.getCompilationUnit(); + if (iCompilationUnit instanceof CompilationUnit) { + CompilationUnit compilationUnit = (CompilationUnit) iCompilationUnit; + Map<String, String> docs = EclipseAugments.CompilationUnit_javadoc.get(compilationUnit); + + String signature = getSignature(sourceMethod); + String rawJavadoc = docs.get(signature); + if (rawJavadoc == null) return null; + + return Reflection.javadoc2HTML((IMember) member, member, rawJavadoc); + } + } + + return null; + } + + public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, Integer tab, StringBuffer output, TypeDeclaration type) { + if (methodDeclaration.compilationResult.compilationUnit instanceof CompilationUnit) { + Map<String, String> docs = CompilationUnit_javadoc.get((CompilationUnit) methodDeclaration.compilationResult.compilationUnit); + if (docs != null) { + String signature = EclipseHandlerUtil.getSignature(type, methodDeclaration); + String rawJavadoc = docs.get(signature); + if (rawJavadoc != null) { + for (String line : rawJavadoc.split("\r?\n")) { + ASTNode.printIndent(tab, output).append(line).append("\n"); + } + } + } + } + return methodDeclaration.print(tab, output); + } + + private static String getSignature(SourceMethod sourceMethod) { + StringBuilder sb = new StringBuilder(); + sb.append(sourceMethod.getParent().getElementName()); + sb.append("."); + sb.append(sourceMethod.getElementName()); + sb.append("("); + for (String type : sourceMethod.getParameterTypes()) { + sb.append(type); + } + sb.append(")"); + + return sb.toString(); + } + + /** + * The method <code>javadoc2HTML</code> changed 2014-12 to accept an + * additional IJavaElement parameter. To support older versions, try to + * find that one too. + */ + private static class Reflection { + private static final Method javadoc2HTML; + private static final Method oldJavadoc2HTML; + static { + Method a = null, b = null; + + try { + a = Permit.getMethod(JavadocContentAccess2.class, "javadoc2HTML", IMember.class, IJavaElement.class, String.class); + } catch (Throwable t) {} + try { + b = Permit.getMethod(JavadocContentAccess2.class, "javadoc2HTML", IMember.class, String.class); + } catch (Throwable t) {} + + javadoc2HTML = a; + oldJavadoc2HTML = b; + } + + private static String javadoc2HTML(IMember member, IJavaElement element, String rawJavadoc) { + if (javadoc2HTML != null) { + try { + return (String) javadoc2HTML.invoke(null, member, element, rawJavadoc); + } catch (Throwable t) { + return null; + } + } + if (oldJavadoc2HTML != null) { + try { + return (String) oldJavadoc2HTML.invoke(null, member, rawJavadoc); + } catch (Throwable t) { + return null; + } + } + return null; + } + } +} diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index deab0be1..73a4b82d 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -35,6 +35,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IAnnotatable; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; @@ -50,6 +51,7 @@ import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MessageSend; +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.Scope; @@ -297,6 +299,26 @@ final class PatchFixesHider { } } + /** Contains patch code to support Javadoc for generated methods */ + public static final class Javadoc { + private static final Method GET_HTML; + private static final Method PRINT_METHOD; + + static { + Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchJavadoc"); + GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", String.class, IJavaElement.class); + PRINT_METHOD = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuffer.class, TypeDeclaration.class); + } + + public static String getHTMLContentFromSource(String original, IJavaElement member) { + return (String) Util.invokeMethod(GET_HTML, original, member); + } + + public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuffer output, TypeDeclaration type) { + return (StringBuffer) Util.invokeMethod(PRINT_METHOD, methodDeclaration, tab, output, type); + } + } + /** * Contains a mix of methods: ecj only, ecj+eclipse, and eclipse only. As a consequence, _EVERY_ method from here used for ecj MUST be * transplanted, as ecj itself cannot load this class (signatures refer to things that don't exist in ecj-only mode). |