use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class ByteBuddyMojo method transform.
/**
* Applies all registered transformations.
*
* @param root The root directory to process.
* @param entryPoint The transformation's entry point.
* @param classPath A list of class path elements expected by the processed classes.
* @param plugins The plugins to apply.
* @throws MojoExecutionException If the user configuration results in an error.
* @throws MojoFailureException If the plugin application raises an error.
* @throws IOException If an I/O exception occurs.
*/
private void transform(File root, EntryPoint entryPoint, List<? extends String> classPath, List<Plugin> plugins) throws MojoExecutionException, MojoFailureException, IOException {
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(classPath.size() + 1);
classFileLocators.add(new ClassFileLocator.ForFolder(root));
for (String target : classPath) {
File artifact = new File(target);
classFileLocators.add(artifact.isFile() ? ClassFileLocator.ForJarFile.of(artifact) : new ClassFileLocator.ForFolder(artifact));
}
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators);
try {
TypePool typePool = new TypePool.Default.WithLazyResolution(new TypePool.CacheProvider.Simple(), classFileLocator, TypePool.Default.ReaderMode.FAST, TypePool.ClassLoading.ofBootPath());
getLog().info("Processing class files located in in: " + root);
ByteBuddy byteBuddy;
try {
byteBuddy = entryPoint.getByteBuddy();
} catch (Throwable throwable) {
throw new MojoExecutionException("Cannot create Byte Buddy instance", throwable);
}
processDirectory(root, root, byteBuddy, entryPoint, suffix == null || suffix.isEmpty() ? MethodNameTransformer.Suffixing.withRandomSuffix() : new MethodNameTransformer.Suffixing(suffix), classFileLocator, typePool, plugins);
} finally {
classFileLocator.close();
}
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testTypeIsCached.
@Test
public void testTypeIsCached() throws Exception {
ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
TypePool typePool = TypePool.Default.WithLazyResolution.of(classFileLocator);
TypePool.Resolution resolution = typePool.describe(Object.class.getName());
assertThat(resolution.resolve().getModifiers(), CoreMatchers.is(TypeDescription.OBJECT.getModifiers()));
assertThat(resolution.resolve().getInterfaces(), CoreMatchers.is(TypeDescription.OBJECT.getInterfaces()));
assertThat(typePool.describe(Object.class.getName()).resolve(), CoreMatchers.is(resolution.resolve()));
verify(classFileLocator).locate(Object.class.getName());
verifyNoMoreInteractions(classFileLocator);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testGenericSuperInterfaceHierarchyResolutionIsLazy.
@Test
public void testGenericSuperInterfaceHierarchyResolutionIsLazy() throws Exception {
ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
assertThat(describe(GenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()).getInterfaces().getOnly().asErasure(), CoreMatchers.is((TypeDescription) new TypeDescription.ForLoadedType(SampleGenericInterface.class)));
verify(classFileLocator).locate(GenericType.class.getName());
verifyNoMoreInteractions(classFileLocator);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class ClassReloadingStrategyTest method testResetEmptyNoEffect.
@Test
public void testResetEmptyNoEffect() throws Exception {
Instrumentation instrumentation = mock(Instrumentation.class);
ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
ClassReloadingStrategy.of(instrumentation).reset(classFileLocator);
verify(instrumentation, times(2)).isRedefineClassesSupported();
verifyNoMoreInteractions(instrumentation);
verifyZeroInteractions(classFileLocator);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class ClassReloadingStrategyTest method testAnonymousType.
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(retransformsClasses = true)
public void testAnonymousType() throws Exception {
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Class.forName(LAMBDA_SAMPLE_FACTORY)), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
Instrumentation instrumentation = ByteBuddyAgent.install();
Class<?> factory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
@SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) factory.getDeclaredMethod("nonCapturing").invoke(factory.getDeclaredConstructor().newInstance());
// Anonymous types can only be reset to their original format, if a retransformation is applied.
ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(instrumentation, ClassReloadingStrategy.Strategy.RETRANSFORMATION).preregistered(instance.getClass());
ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.of(instrumentation, instance.getClass());
try {
assertThat(instance.call(), is(FOO));
new ByteBuddy().redefine(instance.getClass(), classFileLocator).method(named("call")).intercept(FixedValue.value(BAR)).make().load(instance.getClass().getClassLoader(), classReloadingStrategy);
assertThat(instance.call(), is(BAR));
} finally {
classReloadingStrategy.reset(classFileLocator, instance.getClass());
assertThat(instance.call(), is(FOO));
}
}
Aggregations