Make explicit imports orders of times faster

- We can tell if a name can be resolved or not, and we can do so in
    constant time.
This commit is contained in:
Juan Martín Sotuyo Dodero
2016-10-14 16:05:20 -03:00
parent 09ac963709
commit d350bcb49e

View File

@ -108,7 +108,8 @@ public class TypeSet {
* explicit import statements.
*/
public static class ExplicitImportResolver extends AbstractResolver {
private Set<String> importStmts;
private Map<String, String> importStmts;
/**
* Creates a new {@link ExplicitImportResolver}.
* @param pmdClassLoader the class loader to use.
@ -116,21 +117,36 @@ public class TypeSet {
*/
public ExplicitImportResolver(PMDASMClassLoader pmdClassLoader, Set<String> importStmts) {
super(pmdClassLoader);
this.importStmts = importStmts;
// unfold imports, to store both FQ and unqualified names mapped to the FQ name
this.importStmts = new HashMap<>();
for (final String stmt : importStmts) {
if (stmt.endsWith("*")) {
continue;
}
this.importStmts.put(stmt, stmt);
final int lastDotItdx = stmt.lastIndexOf('.');
if (lastDotItdx != -1) {
this.importStmts.put(stmt.substring(lastDotItdx + 1), stmt);
}
}
}
@Override
public Class<?> resolve(String name) throws ClassNotFoundException {
if (name == null) {
throw new ClassNotFoundException();
}
for (String importStmt : importStmts) {
if (importStmt.endsWith(name)) {
return pmdClassLoader.loadClass(importStmt);
}
final String fqName = importStmts.get(name);
if (fqName != null) {
return pmdClassLoader.loadClass(fqName);
}
throw new ClassNotFoundException("Type " + name + " not found");
}
@Override
public boolean couldResolve(String name) {
return importStmts.containsKey(name);
}
}
/**