[performance] improve typeMatches lookup. Especially in light of the built-in copyable annotations lists growing, this should help performance.

This commit is contained in:
Reinier Zwitserloot committed 2019-03-25 20:20:22 +01:00
1 parent cddd9d96ca
commit f8857fdffe
4 files changed
+34 -13

No files matched your search

+9 -2
View File
@@ -60,6 +60,7 @@ public abstract class AST<A extends AST<A, L, N>, L extends LombokNode<A, L, N>,
private final String fileName;
private final String packageDeclaration;
private final ImportList imports;
private TypeResolver importsAsResolver;
Map<N, N> identityDetector = new IdentityHashMap<N, N>();
private Map<N, L> nodeMap = new IdentityHashMap<N, L>();
private boolean changed = false;
@@ -112,13 +113,19 @@ public abstract class AST<A extends AST<A, L, N>, L extends LombokNode<A, L, N>,
/**
* Return the contents of each non-static import statement on this AST's top (Compilation Unit) node.
*
* Example: "java.util.IOException".
*/
public final ImportList getImportList() {
return imports;
}
/**
* Return the contents of each non-static import statement on this AST's top (Compilation Unit) node, packed into a (cached) TypeResolver.
*/
public final TypeResolver getImportListAsTypeResolver() {
if (importsAsResolver != null) return importsAsResolver;
return importsAsResolver = new TypeResolver(getImportList());
}
/**
* Puts the given node in the map so that javac/Eclipse's own internal AST object can be translated to
* an AST.Node object. Also registers the object as visited to avoid endless loops.
+9
View File
@@ -96,6 +96,15 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
return getAst().getImportList();
}
/**
* Convenient shortcut to the owning ast object's {@code getImportListAsTypeResolver} method.
*
* @see AST#getImportListAsTypeResolver()
*/
public TypeResolver getImportListAsTypeResolver() {
return getAst().getImportListAsTypeResolver();
}
/**
* See {@link #isStructurallySignificant}.
*/
@@ -221,14 +221,14 @@ public class EclipseHandlerUtil {
* @param typeRef A type reference to check.
*/
public static boolean typeMatches(String type, EclipseNode node, TypeReference typeRef) {
if (typeRef == null || typeRef.getTypeName() == null || typeRef.getTypeName().length == 0) return false;
String lastPartA = new String(typeRef.getTypeName()[typeRef.getTypeName().length -1]);
int lastIndex = type.lastIndexOf('.');
String lastPartB = lastIndex == -1 ? type : type.substring(lastIndex + 1);
if (!lastPartA.equals(lastPartB)) return false;
String typeName = toQualifiedName(typeRef.getTypeName());
TypeResolver resolver = new TypeResolver(node.getImportList());
char[][] tn = typeRef == null ? null : typeRef.getTypeName();
if (tn == null || tn.length == 0) return false;
char[] lastPartA = tn[tn.length - 1];
int lastIndex = type.lastIndexOf('.') + 1;
if (lastPartA.length != type.length() - lastIndex) return false;
for (int i = 0; i < lastPartA.length; i++) if (lastPartA[i] != type.charAt(i + lastIndex)) return false;
String typeName = toQualifiedName(tn);
TypeResolver resolver = node.getImportListAsTypeResolver();
return resolver.typeMatches(node, type, typeName);
}
@@ -298,9 +298,14 @@ public class JavacHandlerUtil {
* @param typeNode A type reference to check.
*/
public static boolean typeMatches(String type, JavacNode node, JCTree typeNode) {
String typeName = typeNode.toString();
TypeResolver resolver = new TypeResolver(node.getImportList());
String typeName = typeNode == null ? null : typeNode.toString();
if (typeName == null || typeName.length() == 0) return false;
int lastIndexA = typeName.lastIndexOf('.') + 1;
int lastIndexB = type.lastIndexOf('.') + 1;
int len = typeName.length() - lastIndexA;
if (len != type.length() - lastIndexB) return false;
for (int i = 0; i < len; i++) if (typeName.charAt(i + lastIndexA) != type.charAt(i + lastIndexB)) return false;
TypeResolver resolver = node.getImportListAsTypeResolver();
return resolver.typeMatches(node, type, typeName);
}