use of com.newrelic.weave.utils.ClassLoaderFinder in project newrelic-java-agent by newrelic.
the class WeavePackageManager method weave.
/**
* Weave all of the matched packages with the specified target bytes and return the composite class.
*
* @param classloader classloader to resolve classes with
* @param className target class name
* @param targetBytes target class bytes
* @return composite class bytes, or <code>null</code> if no weaving occurred
*/
public byte[] weave(ClassLoader classloader, String className, byte[] targetBytes, Map<Method, Collection<String>> skipMethods) throws IOException {
classloader = classLoaderSub(classloader);
ClassCache cache = new ClassCache(new ClassLoaderFinder(classloader));
return weave(classloader, cache, className, targetBytes, skipMethods, null);
}
use of com.newrelic.weave.utils.ClassLoaderFinder in project newrelic-java-agent by newrelic.
the class WeaveTestUtils method expectViolations.
/**
* Validate the WeavePackage against a ClassLoader and assert that exactly the expected violations occur.
*
* @param weavePackage package to validate
* @param classloader classloader to validate against
* @param expected exepcted violations
* @throws IOException
*/
public static void expectViolations(WeavePackage weavePackage, ClassLoader classloader, WeaveViolation... expected) throws IOException {
List<WeaveViolation> actual = weavePackage.validate(new ClassCache(new ClassLoaderFinder(classloader))).getViolations();
expectViolations(actual, expected);
}
use of com.newrelic.weave.utils.ClassLoaderFinder in project newrelic-java-agent by newrelic.
the class ClassLoaderTest method testSelfFirst.
@Test
public void testSelfFirst() throws IOException {
URL[] parentClassPath = new URL[] { WeaveTestUtils.createJarFile("ParentClassLoader", Foo.class, Bar.class) };
URL[] childClassPath = new URL[] { WeaveTestUtils.createJarFile("ChildClassLoader", Foo.class, Bar.class) };
Strategy classLoadingStrategy = Strategy.SELF_FIRST;
Strategy resourceLoadingStrategy = Strategy.SELF_FIRST;
ClassLoader parentLoader = new CustomClassLoader(classLoadingStrategy, resourceLoadingStrategy, parentClassPath, null);
ClassLoader childLoader = new CustomClassLoader(classLoadingStrategy, resourceLoadingStrategy, childClassPath, parentLoader);
ClassFinder parentFinder = new ClassLoaderFinder(parentLoader);
ClassFinder childFinder = new ClassLoaderFinder(childLoader);
Assert.assertNotEquals(parentFinder.findResource(Foo.class.getName()), childFinder.findResource(Foo.class.getName()));
}
use of com.newrelic.weave.utils.ClassLoaderFinder in project newrelic-java-agent by newrelic.
the class ClassLoaderTest method testInvisibleResources.
// @Test // this does not pass yet
public void testInvisibleResources() throws Exception {
Strategy classLoadingStrategy = Strategy.PARENT_FIRST;
Strategy resourceLoadingStrategy = Strategy.HIDDEN;
ClassLoader invisibleClassLoader = new CustomClassLoader(classLoadingStrategy, resourceLoadingStrategy, classpath, null);
ClassLoaderUtils.runOnClassLoader(invisibleClassLoader, new TestInvisibleCallable());
ClassFinder finder = new ClassLoaderFinder(invisibleClassLoader);
Assert.assertTrue(ClassLoaderUtils.isClassLoadedOnClassLoader(invisibleClassLoader, Foo.class.getName()));
Assert.assertNotNull(finder.findResource(Foo.class.getName()));
Assert.assertFalse(ClassLoaderUtils.isClassLoadedOnClassLoader(invisibleClassLoader, Bar.class.getName()));
Assert.assertNotNull(finder.findResource(Bar.class.getName()));
Assert.assertFalse(ClassLoaderUtils.isClassLoadedOnClassLoader(invisibleClassLoader, Bar.class.getName()));
invisibleClassLoader.loadClass(Bar.class.getName());
Assert.assertTrue(ClassLoaderUtils.isClassLoadedOnClassLoader(invisibleClassLoader, Bar.class.getName()));
}
use of com.newrelic.weave.utils.ClassLoaderFinder in project newrelic-java-agent by newrelic.
the class EnumNoNewFieldsTest method testEnumInvalidNewFields.
@Test
public void testEnumInvalidNewFields() throws IOException {
List<byte[]> weaveBytes = new ArrayList<>();
WeavePackageConfig config = WeavePackageConfig.builder().name("weave_unittest").source("com.newrelic.weave.weavepackage.testclasses").build();
weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumNoNewFieldsTest$Day_Weave"));
WeavePackage weavePackage = new WeavePackage(config, weaveBytes);
ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumNoNewFieldsTest.class.getClassLoader()));
PackageValidationResult result = weavePackage.validate(cache);
WeaveTestUtils.expectViolations(result, new WeaveViolation(WeaveViolationType.ENUM_NEW_FIELD, "com/newrelic/weave/EnumNoNewFieldsTest$Day"));
}
Aggregations