use of net.bytebuddy.pool.TypePool in project byte-buddy by raphw.
the class ByteBuddyTutorialExamplesTest method testTutorialGettingStartedTypePool.
@Test
public void testTutorialGettingStartedTypePool() throws Exception {
TypePool typePool = TypePool.Default.ofClassPath();
// Assure repeatability.
ClassLoader classLoader = new URLClassLoader(new URL[0], null);
new ByteBuddy().redefine(typePool.describe(UnloadedBar.class.getName()).resolve(), ClassFileLocator.ForClassLoader.ofClassPath()).defineField("qux", String.class).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION);
assertThat(classLoader.loadClass(UnloadedBar.class.getName()).getDeclaredField("qux"), notNullValue(java.lang.reflect.Field.class));
}
use of net.bytebuddy.pool.TypePool in project byte-buddy by raphw.
the class TransformationAction 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 IOException If an I/O exception occurs.
*/
private void transform(File root, Iterable<? extends File> classPath, EntryPoint entryPoint, List<Plugin> plugins) throws IOException {
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>();
classFileLocators.add(new ClassFileLocator.ForFolder(root));
for (File artifact : classPath) {
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());
project.getLogger().info("Processing class files located in in: {}", root);
ByteBuddy byteBuddy;
try {
byteBuddy = entryPoint.getByteBuddy();
} catch (Throwable throwable) {
throw new GradleException("Cannot create Byte Buddy instance", throwable);
}
processDirectory(root, root, byteBuddy, entryPoint, byteBuddyExtension.getMethodNameTransformer(), classFileLocator, typePool, plugins);
} finally {
classFileLocator.close();
}
}
use of net.bytebuddy.pool.TypePool 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();
}
}
Aggregations