Skip to content

Commit

Permalink
Javadoc support for eclipse 2024-03
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 authored and rspilker committed Mar 18, 2024
1 parent e4824cb commit 98cdf67
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,14 @@ private static void patchNullCheck(ScriptManager sm) {
}

private static void patchJavadoc(ScriptManager sm) {
// Moved to new package and changed to instance method in 2024-03
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.wrapMethodCall()
.target(new MethodTarget("org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess", "getHTMLContent", "java.lang.String", "org.eclipse.jdt.core.IJavaElement", "boolean"))
.methodToWrap(new Hook("org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess", "getHTMLContentFromSource", "java.lang.String", "org.eclipse.jdt.core.IJavaElement"))
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "java.lang.Object", "org.eclipse.jdt.core.IJavaElement"))
.requestExtra(StackRequest.THIS, StackRequest.PARAM1)
.build());

sm.addScriptIfWitness(OSGI_TYPES, 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"))
Expand Down
32 changes: 22 additions & 10 deletions src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public class PatchJavadoc {

public static String getHTMLContentFromSource(String original, Object member) {
public static String getHTMLContentFromSource(Object instance, String original, Object member) {
if (original != null) {
return original;
}
Expand All @@ -54,7 +54,7 @@ public static String getHTMLContentFromSource(String original, Object member) {
String rawJavadoc = docs.get(signature);
if (rawJavadoc == null) return null;

return Reflection.javadoc2HTML((IMember) member, (IJavaElement) member, rawJavadoc);
return Reflection.javadoc2HTML(instance, (IMember) member, (IJavaElement) member, rawJavadoc);
}
}

Expand Down Expand Up @@ -85,9 +85,10 @@ static final String getSignature(SourceMethod sourceMethod) {
private static class Reflection {
private static final Method javadoc2HTML;
private static final Method oldJavadoc2HTML;
private static final Method reallyOldJavadoc2HTML;
private static final Method lsJavadoc2HTML;
static {
Method a = null, b = null, c = null;
Method a = null, b = null, c = null, d = null;

try {
a = Permit.getMethod(JavadocContentAccess2.class, "javadoc2HTML", IMember.class, IJavaElement.class, String.class);
Expand All @@ -98,30 +99,41 @@ private static class Reflection {
try {
c = Permit.getMethod(Class.forName("org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess2"), "javadoc2HTML", IMember.class, IJavaElement.class, String.class);
} catch (Throwable t) {}
try {
d = Permit.getMethod(Class.forName("org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess"), "javadoc2HTML", IMember.class, IJavaElement.class, String.class);
} catch (Throwable t) {}

javadoc2HTML = a;
oldJavadoc2HTML = b;
oldJavadoc2HTML = a;
reallyOldJavadoc2HTML = b;
lsJavadoc2HTML = c;
javadoc2HTML = d;
}

private static String javadoc2HTML(IMember member, IJavaElement element, String rawJavadoc) {
private static String javadoc2HTML(Object instance, IMember member, IJavaElement element, String rawJavadoc) {
if (javadoc2HTML != null) {
try {
return (String) javadoc2HTML.invoke(null, member, element, rawJavadoc);
return (String) javadoc2HTML.invoke(instance, member, element, rawJavadoc);
} catch (Throwable t) {
return null;
}
}
if (oldJavadoc2HTML != null) {
try {
return (String) oldJavadoc2HTML.invoke(instance, member, element, rawJavadoc);
} catch (Throwable t) {
return null;
}
}
if (lsJavadoc2HTML != null) {
try {
return (String) lsJavadoc2HTML.invoke(null, member, element, rawJavadoc);
return (String) lsJavadoc2HTML.invoke(instance, member, element, rawJavadoc);
} catch (Throwable t) {
return null;
}
}
if (oldJavadoc2HTML != null) {
if (reallyOldJavadoc2HTML != null) {
try {
return (String) oldJavadoc2HTML.invoke(null, member, rawJavadoc);
return (String) reallyOldJavadoc2HTML.invoke(instance, member, rawJavadoc);
} catch (Throwable t) {
return null;
}
Expand Down
8 changes: 6 additions & 2 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,15 @@ public static final class Javadoc {

static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchJavadoc");
GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", String.class, Object.class);
GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", Object.class, String.class, Object.class);
}

public static String getHTMLContentFromSource(String original, IJavaElement member) {
return (String) Util.invokeMethod(GET_HTML, original, member);
return (String) Util.invokeMethod(GET_HTML, null, original, member);
}

public static String getHTMLContentFromSource(String original, Object instance, IJavaElement member) {
return (String) Util.invokeMethod(GET_HTML, instance, original, member);
}
}

Expand Down
13 changes: 10 additions & 3 deletions test/eclipse/src/lombok/eclipse/misc/JavadocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
*/
package lombok.eclipse.misc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2;
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -45,10 +46,16 @@ public void getterSetter() throws Exception {

IJavaElement getterInvocation = cu.codeSelect(306, 0)[0];
String getterHtmlContent = JavadocContentAccess2.getHTMLContent(getterInvocation, true);
assertEquals("Getter section<dl><dt>Returns:</dt><dd> Sky is blue3</dd></dl>", getterHtmlContent);
assertThat(getterHtmlContent, CoreMatchers.containsString("Getter section"));
assertThat(getterHtmlContent, CoreMatchers.containsString("Returns:"));
assertThat(getterHtmlContent, CoreMatchers.containsString("Sky is blue3"));

IJavaElement setterInvocation = cu.codeSelect(320, 0)[0];
String setterHtmlContent = JavadocContentAccess2.getHTMLContent(setterInvocation, true);
assertEquals("Setter section<dl><dt>Parameters:</dt><dd><b>fieldName</b> Hello, World3</dd><dd><b>field</b> </dd></dl>", setterHtmlContent);
assertThat(setterHtmlContent, CoreMatchers.containsString("Setter section"));
assertThat(setterHtmlContent, CoreMatchers.containsString("Parameters:"));
assertThat(setterHtmlContent, CoreMatchers.containsString("fieldName"));
assertThat(setterHtmlContent, CoreMatchers.containsString("Hello, World3"));
assertThat(setterHtmlContent, CoreMatchers.containsString("field"));
}
}

0 comments on commit 98cdf67

Please sign in to comment.