Remove some dead code from SpockRunner

And also reformat it
This commit is contained in:
Nikolay Martynov 2018-10-18 09:48:08 -04:00
parent 3cc520ecb9
commit e02f889f09
1 changed files with 18 additions and 27 deletions

View File

@ -63,13 +63,13 @@ public class SpockRunner extends Sputnik {
private final InstrumentationClassLoader customLoader; private final InstrumentationClassLoader customLoader;
public SpockRunner(Class<?> clazz) public SpockRunner(final Class<?> clazz)
throws InitializationError, NoSuchFieldException, SecurityException, IllegalArgumentException, throws InitializationError, NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException { IllegalAccessException {
super(shadowTestClass(clazz)); super(shadowTestClass(clazz));
assertNoBootstrapClassesInTestClass(clazz); assertNoBootstrapClassesInTestClass(clazz);
// access the classloader created in shadowTestClass above // access the classloader created in shadowTestClass above
Field clazzField = Sputnik.class.getDeclaredField("clazz"); final Field clazzField = Sputnik.class.getDeclaredField("clazz");
try { try {
clazzField.setAccessible(true); clazzField.setAccessible(true);
customLoader = customLoader =
@ -79,7 +79,7 @@ public class SpockRunner extends Sputnik {
} }
} }
private static void assertNoBootstrapClassesInTestClass(Class<?> testClass) { private static void assertNoBootstrapClassesInTestClass(final Class<?> testClass) {
for (final Field field : testClass.getDeclaredFields()) { for (final Field field : testClass.getDeclaredFields()) {
assertNotBootstrapClass(testClass, field.getType()); assertNotBootstrapClass(testClass, field.getType());
} }
@ -92,7 +92,7 @@ public class SpockRunner extends Sputnik {
} }
} }
private static void assertNotBootstrapClass(Class<?> testClass, Class<?> clazz) { private static void assertNotBootstrapClass(final Class<?> testClass, final Class<?> clazz) {
if ((!clazz.isPrimitive()) && isBootstrapClass(clazz.getName())) { if ((!clazz.isPrimitive()) && isBootstrapClass(clazz.getName())) {
throw new IllegalStateException( throw new IllegalStateException(
testClass.getName() testClass.getName()
@ -101,7 +101,7 @@ public class SpockRunner extends Sputnik {
} }
} }
private static boolean isBootstrapClass(String className) { private static boolean isBootstrapClass(final String className) {
for (int i = 0; i < TEST_BOOTSTRAP_PREFIXES.length; ++i) { for (int i = 0; i < TEST_BOOTSTRAP_PREFIXES.length; ++i) {
if (className.startsWith(TEST_BOOTSTRAP_PREFIXES[i])) { if (className.startsWith(TEST_BOOTSTRAP_PREFIXES[i])) {
return true; return true;
@ -113,11 +113,11 @@ public class SpockRunner extends Sputnik {
// Shadow the test class with bytes loaded by InstrumentationClassLoader // Shadow the test class with bytes loaded by InstrumentationClassLoader
private static Class<?> shadowTestClass(final Class<?> clazz) { private static Class<?> shadowTestClass(final Class<?> clazz) {
try { try {
InstrumentationClassLoader customLoader = final InstrumentationClassLoader customLoader =
new InstrumentationClassLoader( new InstrumentationClassLoader(
datadog.trace.agent.test.SpockRunner.class.getClassLoader(), clazz.getName()); datadog.trace.agent.test.SpockRunner.class.getClassLoader(), clazz.getName());
return customLoader.shadow(clazz); return customLoader.shadow(clazz);
} catch (Exception e) { } catch (final Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
@ -141,14 +141,14 @@ public class SpockRunner extends Sputnik {
// Utils cannot be referenced before this line, as its static initializers load bootstrap // Utils cannot be referenced before this line, as its static initializers load bootstrap
// classes (for example, the bootstrap proxy). // classes (for example, the bootstrap proxy).
datadog.trace.agent.tooling.Utils.getBootstrapProxy().addURL(bootstrapJar.toURI().toURL()); datadog.trace.agent.tooling.Utils.getBootstrapProxy().addURL(bootstrapJar.toURI().toURL());
} catch (IOException e) { } catch (final IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private static File createBootstrapJar() throws IOException { private static File createBootstrapJar() throws IOException {
Set<String> bootstrapClasses = new HashSet<String>(); final Set<String> bootstrapClasses = new HashSet<>();
for (ClassPath.ClassInfo info : TestUtils.getTestClasspath().getAllClasses()) { for (final ClassPath.ClassInfo info : TestUtils.getTestClasspath().getAllClasses()) {
// if info starts with bootstrap prefix: add to bootstrap jar // if info starts with bootstrap prefix: add to bootstrap jar
if (isBootstrapClass(info.getName())) { if (isBootstrapClass(info.getName())) {
bootstrapClasses.add(info.getResourceName()); bootstrapClasses.add(info.getResourceName());
@ -165,48 +165,39 @@ public class SpockRunner extends Sputnik {
final ClassLoader parent; final ClassLoader parent;
final String shadowPrefix; final String shadowPrefix;
public InstrumentationClassLoader(ClassLoader parent, String shadowPrefix) { public InstrumentationClassLoader(final ClassLoader parent, final String shadowPrefix) {
super(parent); super(parent);
this.parent = parent; this.parent = parent;
this.shadowPrefix = shadowPrefix; this.shadowPrefix = shadowPrefix;
} }
/** Forcefully inject the bytes of clazz into this classloader. */ /** Forcefully inject the bytes of clazz into this classloader. */
public Class<?> shadow(Class<?> clazz) throws IOException { public Class<?> shadow(final Class<?> clazz) throws IOException {
Class<?> loaded = this.findLoadedClass(clazz.getName()); final Class<?> loaded = findLoadedClass(clazz.getName());
if (loaded != null && loaded.getClassLoader() == this) { if (loaded != null && loaded.getClassLoader() == this) {
return loaded; return loaded;
} }
final ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(clazz.getClassLoader()); final ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(clazz.getClassLoader());
final byte[] classBytes = locator.locate(clazz.getName()).resolve(); final byte[] classBytes = locator.locate(clazz.getName()).resolve();
return this.defineClass(clazz.getName(), classBytes, 0, classBytes.length); return defineClass(clazz.getName(), classBytes, 0, classBytes.length);
} }
@Override @Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { protected Class<?> loadClass(final String name, final boolean resolve)
throws ClassNotFoundException {
synchronized (super.getClassLoadingLock(name)) { synchronized (super.getClassLoadingLock(name)) {
Class c = this.findLoadedClass(name); final Class c = findLoadedClass(name);
if (c != null) { if (c != null) {
return c; return c;
} }
if (name.startsWith(shadowPrefix)) { if (name.startsWith(shadowPrefix)) {
try { try {
return shadow(super.loadClass(name, resolve)); return shadow(super.loadClass(name, resolve));
} catch (Exception e) { } catch (final Exception e) {
} }
} }
/*
if (!name.startsWith("datadog.trace.agent.test.")) {
for (int i = 0; i < AGENT_PACKAGE_PREFIXES.length; ++i) {
if (name.startsWith(AGENT_PACKAGE_PREFIXES[i])) {
throw new ClassNotFoundException(
"refusing to load agent class: " + name + " on test classloader.");
}
}
}
*/
return parent.loadClass(name); return parent.loadClass(name);
} }
} }