[java] Delete old typeres

This commit is contained in:
Clément Fournier
2022-07-26 16:02:05 +02:00
parent 8bf54b8a68
commit 6688165ee3
4 changed files with 0 additions and 609 deletions

View File

@ -1,39 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.typeresolution;
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.lang.java.typeresolution.internal.NullableClassLoader;
//
// Helpful reading:
// http://www.janeg.ca/scjp/oper/promotions.html
// http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html
//
/**
* @deprecated Some rules still use this so we keep it around, but it's dysfunctional
*/
@Deprecated
@DeprecatedUntil700
@InternalApi
public class ClassTypeResolver extends JavaParserVisitorAdapter implements NullableClassLoader {
/**
* Check whether the supplied class name exists.
*/
public boolean classNameExists(String fullyQualifiedClassName) {
return false;
}
@Override
public Class<?> loadClassOrNull(String fullyQualifiedClassName) {
return null;
}
}

View File

@ -1,152 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.typeresolution;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.objectweb.asm.ClassReader;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.java.typeresolution.internal.NullableClassLoader;
import net.sourceforge.pmd.lang.java.typeresolution.visitors.PMDASMVisitor;
/*
* I've refactored this class to not cache the results any more. This is a
* tradeoff in testing I've found the CPU tradeoff is negligeable. With the
* cache, large codebases consumed a lot of memory and slowed down greatly when
* approaching 3,000 classes. I'm adding this comment in case someone is looking
* at this code and thinks a cache may help.
*
* see: git show 9e7deee88f63870a1de2cd86458278a027deb6d6
*
* However, there seems to be a big performance improvement by caching
* the negative cases only. The cache is shared between loadClass and getImportedClasses,
* as they are using the same (parent) class loader, e.g. if the class foo.Bar cannot be loaded,
* then the resource foo/Bar.class will not exist, too.
*
* Note: since git show 46ad3a4700b7a233a177fa77d08110127a85604c the cache is using
* a concurrent hash map to avoid synchronizing on the class loader instance.
*/
@InternalApi
@Deprecated
public final class PMDASMClassLoader extends ClassLoader implements NullableClassLoader {
private static PMDASMClassLoader cachedPMDASMClassLoader;
private static ClassLoader cachedClassLoader;
private static final Object CACHE_LOCK = new Object();
/**
* Caches the names of the classes that we can't load or that don't exist.
*/
private final ConcurrentMap<String, Boolean> dontBother = new ConcurrentHashMap<>();
static {
registerAsParallelCapable();
}
private PMDASMClassLoader(ClassLoader parent) {
super(parent);
}
/**
* A new PMDASMClassLoader is created for each compilation unit, this method
* allows to reuse the same PMDASMClassLoader across all the compilation
* units.
*/
public static PMDASMClassLoader getInstance(ClassLoader parent) {
if (parent instanceof PMDASMClassLoader) {
return (PMDASMClassLoader) parent;
}
synchronized (CACHE_LOCK) {
if (parent.equals(cachedClassLoader)) {
return cachedPMDASMClassLoader;
}
cachedClassLoader = parent;
cachedPMDASMClassLoader = new PMDASMClassLoader(parent);
return cachedPMDASMClassLoader;
}
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> aClass = loadClassOrNull(name);
if (aClass == null) {
throw new ClassNotFoundException(name);
}
return aClass;
}
/**
* Not throwing CNFEs to represent failure makes a huge performance
* difference. Typeres as a whole is 2x faster.
*/
@Override
public Class<?> loadClassOrNull(String name) {
if (dontBother.containsKey(name)) {
return null;
}
try {
return super.loadClass(name);
} catch (ClassNotFoundException | LinkageError e) {
dontBother.put(name, Boolean.TRUE);
return null;
}
}
/**
* Checks if the class loader could resolve a given class name (ie: it
* doesn't know for sure it will fail). Notice, that the ability to resolve
* a class does not imply that the class will actually be found and
* resolved.
*
* @param name
* the name of the class
* @return whether the class can be resolved
*/
public boolean couldResolve(String name) {
return !dontBother.containsKey(name);
}
/**
* FIXME what does this do?
*/
public synchronized Map<String, String> getImportedClasses(String name) throws ClassNotFoundException {
if (dontBother.containsKey(name)) {
throw new ClassNotFoundException(name);
}
try (InputStream classResource = getResourceAsStream(name.replace('.', '/') + ".class")) {
ClassReader reader = new ClassReader(classResource);
PMDASMVisitor asmVisitor = new PMDASMVisitor(name);
reader.accept(asmVisitor, 0);
List<String> inner = asmVisitor.getInnerClasses();
if (inner != null && !inner.isEmpty()) {
// to avoid ConcurrentModificationException
inner = new ArrayList<>(inner);
for (String str : inner) {
try (InputStream innerClassStream = getResourceAsStream(str.replace('.', '/') + ".class")) {
if (innerClassStream != null) {
reader = new ClassReader(innerClassStream);
reader.accept(asmVisitor, 0);
}
}
}
}
return asmVisitor.getPackages();
} catch (IOException e) {
dontBother.put(name, Boolean.TRUE);
throw new ClassNotFoundException(name, e);
}
}
}

View File

@ -1,49 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.typeresolution.internal;
import net.sourceforge.pmd.lang.java.typeresolution.PMDASMClassLoader;
/**
* A classloader that doesn't throw a {@link ClassNotFoundException}
* to report unresolved classes. This is a big performance improvement
* for {@link PMDASMClassLoader}, which caches negative cases.
*
* <p>See https://github.com/pmd/pmd/pull/2236
*/
public interface NullableClassLoader {
/**
* Load a class from its binary name. Returns null if not found.
*/
Class<?> loadClassOrNull(String binaryName);
final class ClassLoaderWrapper implements NullableClassLoader {
private final ClassLoader classLoader;
private ClassLoaderWrapper(ClassLoader classLoader) {
assert classLoader != null : "Null classloader";
this.classLoader = classLoader;
}
@Override
public Class<?> loadClassOrNull(String binaryName) {
try {
return classLoader.loadClass(binaryName);
} catch (ClassNotFoundException e) {
return null;
}
}
public static ClassLoaderWrapper wrapNullable(ClassLoader classLoader) {
if (classLoader == null) {
classLoader = ClassLoader.getSystemClassLoader();
}
return new ClassLoaderWrapper(classLoader);
}
}
}