Merge pull request #2809 from Rawi01/eclipse-javadoc-performance

Avoid reading file from disk by reusing the parser source
This commit is contained in:
Reinier Zwitserloot authored and GitHub committed 2021-04-18 06:33:56 +02:00
commit 561870c7cc
4 files changed
+30 -21

No files matched your search

+9
View File
@@ -232,6 +232,14 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> {
}
}
public void setSource(char[] source) {
this.source = source;
}
public char[] getSource() {
return source;
}
/**
* Eclipse starts off with a 'diet' parse which leaves method bodies blank, amongst other shortcuts.
*
@@ -309,6 +317,7 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> {
}
private final CompilationUnitDeclaration compilationUnitDeclaration;
private char[] source;
private boolean completeParse;
private static String toFileName(CompilationUnitDeclaration ast) {
@@ -182,6 +182,7 @@ public class TransformEclipseAST {
DebugSnapshotStore.INSTANCE.snapshot(ast, "transform entry");
long histoToken = lombokTracker == null ? 0L : lombokTracker.start();
EclipseAST existing = getAST(ast, false);
existing.setSource(parser.scanner.getSource());
new TransformEclipseAST(existing).go();
if (lombokTracker != null) lombokTracker.end(histoToken);
DebugSnapshotStore.INSTANCE.snapshot(ast, "transform exit");
@@ -95,7 +95,6 @@ import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.CaptureBinding;
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
@@ -2689,11 +2688,13 @@ public class EclipseHandlerUtil {
return annotations;
}
public static String getDocComment(CompilationUnitDeclaration cud, ASTNode node) {
ICompilationUnit compilationUnit = cud.compilationResult.compilationUnit;
public static String getDocComment(EclipseNode eclipseNode) {
if (eclipseNode.getAst().getSource() == null) return null;
final ASTNode node = eclipseNode.get();
if (node instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
char[] rawContent = CharOperation.subarray(compilationUnit.getContents(), fieldDeclaration.declarationSourceStart, fieldDeclaration.declarationSourceEnd);
char[] rawContent = CharOperation.subarray(eclipseNode.getAst().getSource(), fieldDeclaration.declarationSourceStart, fieldDeclaration.declarationSourceEnd);
String rawContentString = new String(rawContent);
int startIndex = rawContentString.indexOf("/**");
int endIndex = rawContentString.indexOf("*/");
@@ -2739,14 +2740,13 @@ public class EclipseHandlerUtil {
public static enum CopyJavadoc {
VERBATIM {
@Override public String apply(final CompilationUnitDeclaration cu, final EclipseNode node) {
return getDocComment(cu, node.get());
@Override public String apply(final EclipseNode node) {
return getDocComment(node);
}
},
GETTER {
@Override public String apply(final CompilationUnitDeclaration cu, final EclipseNode node) {
final ASTNode n = node.get();
String javadoc = getDocComment(cu, n);
@Override public String apply(final EclipseNode node) {
String javadoc = getDocComment(node);
// step 1: Check if there is a 'GETTER' section. If yes, that becomes the new method's javadoc.
String out = getJavadocSection(javadoc, "GETTER");
final boolean sectionBased = out != null;
@@ -2757,26 +2757,25 @@ public class EclipseHandlerUtil {
}
},
SETTER {
@Override public String apply(final CompilationUnitDeclaration cu, final EclipseNode node) {
return applySetter(cu, node, "SETTER");
@Override public String apply(final EclipseNode node) {
return applySetter(node, "SETTER");
}
},
WITH {
@Override public String apply(final CompilationUnitDeclaration cu, final EclipseNode node) {
return addReturnsUpdatedSelfIfNeeded(applySetter(cu, node, "WITH|WITHER"));
@Override public String apply(final EclipseNode node) {
return addReturnsUpdatedSelfIfNeeded(applySetter(node, "WITH|WITHER"));
}
},
WITH_BY {
@Override public String apply(final CompilationUnitDeclaration cu, final EclipseNode node) {
return applySetter(cu, node, "WITHBY|WITH_BY");
@Override public String apply( final EclipseNode node) {
return applySetter(node, "WITHBY|WITH_BY");
}
};
public abstract String apply(final CompilationUnitDeclaration cu, final EclipseNode node);
public abstract String apply(final EclipseNode node);
private static String applySetter(final CompilationUnitDeclaration cu, EclipseNode node, String sectionName) {
final ASTNode n = node.get();
String javadoc = getDocComment(cu, n);
private static String applySetter(EclipseNode node, String sectionName) {
String javadoc = getDocComment(node);
// step 1: Check if there is a 'SETTER' section. If yes, that becomes the new method's javadoc.
String out = getJavadocSection(javadoc, sectionName);
final boolean sectionBased = out != null;
@@ -2824,7 +2823,7 @@ public class EclipseHandlerUtil {
if (copyMode == null) copyMode = CopyJavadoc.VERBATIM;
try {
CompilationUnitDeclaration cud = ((CompilationUnitDeclaration) from.top().get());
String newJavadoc = copyMode.apply(cud, from);
String newJavadoc = copyMode.apply(from);
if (forceAddReturn) {
newJavadoc = addReturnsThisIfNeeded(newJavadoc);
}
@@ -1057,7 +1057,7 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
private void copyJavadocFromParam(EclipseNode from, MethodDeclaration to, TypeDeclaration type, String param) {
try {
CompilationUnitDeclaration cud = (CompilationUnitDeclaration) from.top().get();
String methodComment = getDocComment(cud, from.get());
String methodComment = getDocComment(from);
String newJavadoc = addReturnsThisIfNeeded(getParamJavadoc(methodComment, param));
setDocComment(cud, type, to, newJavadoc);
} catch (Exception ignore) {}