[java] Restrict type searches for inner classes
- There are certain cases where we know exactly what to look for, not needing to brute force our way to inner classes. For those, use a direct approach.
This commit is contained in:

committed by
Andreas Dangel

parent
cc6cc2c6bc
commit
2e64907776
@ -228,31 +228,25 @@ public class TypeSet {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
|
||||
final String fqName = qualifyName(name);
|
||||
final Class<?> c = resolveMaybeInner(fqName, fqName);
|
||||
|
||||
if (c == null) {
|
||||
throw new ClassNotFoundException("Type " + name + " not found");
|
||||
}
|
||||
|
||||
return c;
|
||||
return pmdClassLoader.loadClass(qualifyName(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean couldResolve(String name) {
|
||||
return super.couldResolve(qualifyName(name));
|
||||
return pmdClassLoader.couldResolve(qualifyName(name));
|
||||
}
|
||||
|
||||
private String qualifyName(final String name) {
|
||||
final String qualifiedName = name.replace('.', '$');
|
||||
if (pkg == null) {
|
||||
return name;
|
||||
return qualifiedName;
|
||||
}
|
||||
|
||||
/*
|
||||
* String.concat is bad in general, but for simple 2 string concatenation, it's the fastest
|
||||
* See http://www.rationaljava.com/2015/02/the-optimum-method-to-concatenate.html
|
||||
*/
|
||||
return pkg.concat(name);
|
||||
return pkg.concat(qualifiedName);
|
||||
}
|
||||
}
|
||||
|
||||
@ -291,7 +285,7 @@ public class TypeSet {
|
||||
* String.concat is bad in general, but for simple 2 string concatenation, it's the fastest
|
||||
* See http://www.rationaljava.com/2015/02/the-optimum-method-to-concatenate.html
|
||||
*/
|
||||
clazz = pmdClassLoader.loadClass("java.lang.".concat(name));
|
||||
clazz = pmdClassLoader.loadClass("java.lang.".concat(name.replace('.', '$')));
|
||||
CLASS_CACHE.putIfAbsent(name, clazz);
|
||||
|
||||
return clazz;
|
||||
@ -303,7 +297,7 @@ public class TypeSet {
|
||||
* String.concat is bad in general, but for simple 2 string concatenation, it's the fastest
|
||||
* See http://www.rationaljava.com/2015/02/the-optimum-method-to-concatenate.html
|
||||
*/
|
||||
return super.couldResolve("java.lang.".concat(name));
|
||||
return pmdClassLoader.couldResolve("java.lang.".concat(name.replace('.', '$')));
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,12 +327,16 @@ public class TypeSet {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
|
||||
name = name.replace('.', '$');
|
||||
for (String importStmt : importStmts) {
|
||||
final String fqClassName = new StringBuilder(importStmt.length() + name.length()).append(importStmt)
|
||||
.replace(importStmt.length() - 1, importStmt.length(), name).toString();
|
||||
final Class<?> c = resolveMaybeInner(name, fqClassName);
|
||||
if (c != null) {
|
||||
return c;
|
||||
if (pmdClassLoader.couldResolve(fqClassName)) {
|
||||
try {
|
||||
return pmdClassLoader.loadClass(fqClassName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -347,11 +345,12 @@ public class TypeSet {
|
||||
|
||||
@Override
|
||||
public boolean couldResolve(String name) {
|
||||
name = name.replace('.', '$');
|
||||
for (String importStmt : importStmts) {
|
||||
final String fqClassName = new StringBuilder(importStmt.length() + name.length()).append(importStmt)
|
||||
.replace(importStmt.length() - 1, importStmt.length(), name).toString();
|
||||
// can any class be resolved / was never attempted?
|
||||
if (super.couldResolve(fqClassName)) {
|
||||
if (pmdClassLoader.couldResolve(fqClassName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user