diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java index fa2850133f..dc657d989a 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java @@ -112,6 +112,16 @@ interface WithInitialization extends Plugin { Map initialize(ClassFileLocator classFileLocator); } + interface WithClassPath extends Plugin { + + /** + * Provides the classPath elements from the {@link Plugin.Engine}. + * + * @param elements The classPath elements + */ + void processClassPath(Iterable elements); + } + /** * A factory for providing a build plugin. */ @@ -872,6 +882,14 @@ interface Engine { */ Engine ignore(ElementMatcher matcher); + /** + * Appends the classPath elements to the Engine. + * + * @param elements The classPath elements + * @return A new plugin engine that is equal to this engine but with the supplied classpath elements. + */ + Engine withClassPath(Iterable elements); + /** * Applies this plugin engine onto a given source and target. * @@ -4659,6 +4677,11 @@ class Default extends AbstractBase { */ private final ElementMatcher.Junction ignoredTypeMatcher; + /** + * The classPath elements. + */ + private final Iterable classPath; + /** * Creates a new default plugin engine that rebases types and fails fast and on unresolved types and on live initializers. */ @@ -4692,7 +4715,8 @@ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy) { ErrorHandler.Enforcing.ALL_TYPES_RESOLVED, ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS), Dispatcher.ForSerialTransformation.Factory.INSTANCE, - none()); + none(), + new ArrayList()); } /** @@ -4707,6 +4731,7 @@ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy) { * @param errorHandler The error handler to use. * @param dispatcherFactory The dispatcher factory to use. * @param ignoredTypeMatcher A matcher for types to exclude from transformation. + * @param classPath The classPath elements. */ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy, @@ -4716,7 +4741,8 @@ protected Default(ByteBuddy byteBuddy, Listener listener, ErrorHandler errorHandler, Dispatcher.Factory dispatcherFactory, - ElementMatcher.Junction ignoredTypeMatcher) { + ElementMatcher.Junction ignoredTypeMatcher, + Iterable classPath) { this.byteBuddy = byteBuddy; this.typeStrategy = typeStrategy; this.poolStrategy = poolStrategy; @@ -4726,6 +4752,7 @@ protected Default(ByteBuddy byteBuddy, this.errorHandler = errorHandler; this.dispatcherFactory = dispatcherFactory; this.ignoredTypeMatcher = ignoredTypeMatcher; + this.classPath = classPath; } /** @@ -4796,7 +4823,8 @@ public Engine with(ByteBuddy byteBuddy) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4811,7 +4839,8 @@ public Engine with(TypeStrategy typeStrategy) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4826,7 +4855,8 @@ public Engine with(PoolStrategy poolStrategy) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4841,7 +4871,8 @@ public Engine with(ClassFileLocator classFileLocator) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4856,7 +4887,8 @@ public Engine with(@MaybeNull ClassFileVersion classFileVersion) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4871,7 +4903,8 @@ public Engine with(Listener listener) { new Listener.Compound(this.listener, listener), errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4886,7 +4919,8 @@ public Engine withoutErrorHandlers() { listener, Listener.NoOp.INSTANCE, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4901,7 +4935,8 @@ public Engine withErrorHandlers(List errorHandlers) { listener, new ErrorHandler.Compound(errorHandlers), dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4916,7 +4951,8 @@ public Engine with(Dispatcher.Factory dispatcherFactory) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher); + ignoredTypeMatcher, + classPath); } /** @@ -4931,7 +4967,21 @@ public Engine ignore(ElementMatcher matcher) { listener, errorHandler, dispatcherFactory, - ignoredTypeMatcher.or(matcher)); + ignoredTypeMatcher.or(matcher), + classPath); + } + + public Engine withClassPath(Iterable elements) { + return new Default(byteBuddy, + typeStrategy, + poolStrategy, + classFileLocator, + classFileVersion, + listener, + errorHandler, + dispatcherFactory, + ignoredTypeMatcher, + elements); } /** @@ -4946,6 +4996,7 @@ public Summary apply(Source source, Target target, List plugins = new ArrayList(factories.size()); List initializers = new ArrayList(); List preprocessors = new ArrayList(); + List classPathers = new ArrayList(); try { for (Plugin.Factory factory : factories) { Plugin plugin = factory.make(); @@ -4956,6 +5007,9 @@ public Summary apply(Source source, Target target, List classPath = new ArrayList(); + classPath.add(new File("")); + Plugin.Engine.Summary summary = new Plugin.Engine.Default() + .with(ClassFileLocator.ForClassLoader.of(SimplePlugin.class.getClassLoader())) + .with(dispatcherFactory) + .withClassPath(classPath) + .apply(source, target, new Plugin.Factory.Simple(plugin)); + assertThat(plugin.classPath.size(), is(classPath.size())); + assertThat(plugin.classPath, hasItem(classPath.get(0))); + } + @Test public void testSimpleTransformationIgnoredByPlugin() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); @@ -518,7 +531,7 @@ public void close() { } } - private static class PreprocessingPlugin implements Plugin.WithPreprocessor, Plugin.WithInitialization { + private static class PreprocessingPlugin implements Plugin.WithPreprocessor, Plugin.WithInitialization, Plugin.WithClassPath { private final Plugin plugin; @@ -526,6 +539,12 @@ private PreprocessingPlugin(Plugin plugin) { this.plugin = plugin; } + public void processClassPath(Iterable elements) { + if (plugin instanceof WithClassPath) { + ((WithClassPath) plugin).processClassPath(elements); + } + } + public void onPreprocess(TypeDescription typeDescription, ClassFileLocator classFileLocator) { /* empty */ } @@ -575,4 +594,27 @@ public void close() throws IOException { plugin.close(); } } + + private static class ClassPathPlugin implements Plugin.WithClassPath { + + private final List classPath = new ArrayList(); + + public void processClassPath(Iterable elements) { + for (File element : elements) { + classPath.add(element); + } + } + + public DynamicType.Builder apply(DynamicType.Builder builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) { + return builder; + } + + public boolean matches(TypeDescription target) { + return false; + } + + public void close() { + /* empty */ + } + } } diff --git a/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java b/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java index 9ca194df3b..e8419db534 100644 --- a/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java +++ b/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java @@ -511,10 +511,12 @@ public static void apply(Logger logger, } List classFileLocators = new ArrayList(); classFileLocators.add(rootLocator); + List classPathElements = new ArrayList(); for (File artifact : artifacts) { classFileLocators.add(artifact.isFile() ? ClassFileLocator.ForJarFile.of(artifact, multiReleaseClassFileVersion) : ClassFileLocator.ForFolder.of(artifact, multiReleaseClassFileVersion)); + classPathElements.add(artifact); } ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators); try { @@ -535,6 +537,7 @@ public static void apply(Logger logger, .with(threads == 0 ? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE : new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(threads)) + .withClassPath(classPathElements) .apply(source, target, factories); } finally { classFileLocator.close(); diff --git a/byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java b/byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java index 2f7296eefe..57ae9e0d53 100644 --- a/byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java +++ b/byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java @@ -410,11 +410,13 @@ protected Plugin.Engine.Summary transform(List classPath, : ClassFileVersion.ofJavaVersion(multiReleaseVersion); List classFileLocators = new ArrayList(classPath.size()); classFileLocators.add(ClassFileLocator.ForClassLoader.ofPlatformLoader()); + List classPathElements = new ArrayList(); for (String element : classPath) { File artifact = new File(element); classFileLocators.add(artifact.isFile() ? ClassFileLocator.ForJarFile.of(artifact, multiReleaseClassFileVersion) : ClassFileLocator.ForFolder.of(artifact, multiReleaseClassFileVersion)); + classPathElements.add(artifact); } ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators); Plugin.Engine.Summary summary; @@ -438,6 +440,7 @@ protected Plugin.Engine.Summary transform(List classPath, failOnLiveInitializer ? Plugin.Engine.ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS : Plugin.Engine.Listener.NoOp.INSTANCE, failFast ? Plugin.Engine.ErrorHandler.Failing.FAIL_FAST : Plugin.Engine.ErrorHandler.Failing.FAIL_LAST) .with(threads == 0 ? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE : new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(threads)) + .withClassPath(classPathElements) .apply(source, target, factories); } catch (Throwable throwable) { throw new MojoExecutionException("Failed to transform class files in " + file, throwable);