Search in sources :

Example 1 with ClassLoaderFinder

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);
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) ClassCache(com.newrelic.weave.utils.ClassCache)

Example 2 with ClassLoaderFinder

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);
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache)

Example 3 with ClassLoaderFinder

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()));
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) ClassFinder(com.newrelic.weave.utils.ClassFinder) Strategy(com.newrelic.weave.classloading.CustomClassLoader.Strategy) URL(java.net.URL) Test(org.junit.Test)

Example 4 with ClassLoaderFinder

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()));
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) Bar(com.newrelic.weave.classloading.testclasses.Bar) ClassFinder(com.newrelic.weave.utils.ClassFinder) Strategy(com.newrelic.weave.classloading.CustomClassLoader.Strategy)

Example 5 with ClassLoaderFinder

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"));
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Test(org.junit.Test)

Aggregations

ClassLoaderFinder (com.newrelic.weave.utils.ClassLoaderFinder)24 ClassCache (com.newrelic.weave.utils.ClassCache)20 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 Strategy (com.newrelic.weave.classloading.CustomClassLoader.Strategy)4 ClassFinder (com.newrelic.weave.utils.ClassFinder)3 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)3 PackageValidationResult (com.newrelic.weave.weavepackage.PackageValidationResult)3 WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)3 MyOriginalExact (com.newrelic.weave.weavepackage.testclasses.MyOriginalExact)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 ClassNode (org.objectweb.asm.tree.ClassNode)3 MyOriginalTarget1 (com.newrelic.weave.weavepackage.testclasses.MyOriginalTarget1)2 WeaveUtilityClass (com.newrelic.weave.weavepackage.testclasses.WeaveUtilityClass)2 HashMap (java.util.HashMap)2 Bar (com.newrelic.weave.classloading.testclasses.Bar)1 ClassInformation (com.newrelic.weave.utils.ClassInformation)1