Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide classPath elements to plugins #1734

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ interface WithInitialization extends Plugin {
Map<TypeDescription, byte[]> initialize(ClassFileLocator classFileLocator);
}

interface WithClassPath extends Plugin {

/**
* Provides the classPath elements from the {@link Plugin.Engine}.
*
* @param elements The classPath elements
*/
void processClassPath(Iterable<File> elements);
}

/**
* A factory for providing a build plugin.
*/
Expand Down Expand Up @@ -872,6 +882,14 @@ interface Engine {
*/
Engine ignore(ElementMatcher<? super TypeDescription> 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<File> elements);

/**
* Applies this plugin engine onto a given source and target.
*
Expand Down Expand Up @@ -4659,6 +4677,11 @@ class Default extends AbstractBase {
*/
private final ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher;

/**
* The classPath elements.
*/
private final Iterable<File> classPath;

/**
* Creates a new default plugin engine that rebases types and fails fast and on unresolved types and on live initializers.
*/
Expand Down Expand Up @@ -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<File>());
}

/**
Expand All @@ -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,
Expand All @@ -4716,7 +4741,8 @@ protected Default(ByteBuddy byteBuddy,
Listener listener,
ErrorHandler errorHandler,
Dispatcher.Factory dispatcherFactory,
ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher) {
ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher,
Iterable<File> classPath) {
this.byteBuddy = byteBuddy;
this.typeStrategy = typeStrategy;
this.poolStrategy = poolStrategy;
Expand All @@ -4726,6 +4752,7 @@ protected Default(ByteBuddy byteBuddy,
this.errorHandler = errorHandler;
this.dispatcherFactory = dispatcherFactory;
this.ignoredTypeMatcher = ignoredTypeMatcher;
this.classPath = classPath;
}

/**
Expand Down Expand Up @@ -4796,7 +4823,8 @@ public Engine with(ByteBuddy byteBuddy) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4811,7 +4839,8 @@ public Engine with(TypeStrategy typeStrategy) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4826,7 +4855,8 @@ public Engine with(PoolStrategy poolStrategy) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4841,7 +4871,8 @@ public Engine with(ClassFileLocator classFileLocator) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4856,7 +4887,8 @@ public Engine with(@MaybeNull ClassFileVersion classFileVersion) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4871,7 +4903,8 @@ public Engine with(Listener listener) {
new Listener.Compound(this.listener, listener),
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4886,7 +4919,8 @@ public Engine withoutErrorHandlers() {
listener,
Listener.NoOp.INSTANCE,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4901,7 +4935,8 @@ public Engine withErrorHandlers(List<? extends ErrorHandler> errorHandlers) {
listener,
new ErrorHandler.Compound(errorHandlers),
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4916,7 +4951,8 @@ public Engine with(Dispatcher.Factory dispatcherFactory) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher);
ignoredTypeMatcher,
classPath);
}

/**
Expand All @@ -4931,7 +4967,21 @@ public Engine ignore(ElementMatcher<? super TypeDescription> matcher) {
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher.<TypeDescription>or(matcher));
ignoredTypeMatcher.<TypeDescription>or(matcher),
classPath);
}

public Engine withClassPath(Iterable<File> elements) {
return new Default(byteBuddy,
typeStrategy,
poolStrategy,
classFileLocator,
classFileVersion,
listener,
errorHandler,
dispatcherFactory,
ignoredTypeMatcher,
elements);
}

/**
Expand All @@ -4946,6 +4996,7 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
List<Plugin> plugins = new ArrayList<Plugin>(factories.size());
List<WithInitialization> initializers = new ArrayList<WithInitialization>();
List<WithPreprocessor> preprocessors = new ArrayList<WithPreprocessor>();
List<WithClassPath> classPathers = new ArrayList<WithClassPath>();
try {
for (Plugin.Factory factory : factories) {
Plugin plugin = factory.make();
Expand All @@ -4956,6 +5007,9 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
if (plugin instanceof WithInitialization) {
initializers.add((WithInitialization) plugin);
}
if (plugin instanceof WithClassPath) {
classPathers.add((WithClassPath) plugin);
}
}
Source.Origin origin = source.read();
try {
Expand All @@ -4965,6 +5019,9 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
listener.onManifest(manifest);
Target.Sink sink = target.write(manifest);
try {
for (WithClassPath classPather : classPathers) {
classPather.processClassPath(classPath);
}
for (WithInitialization initializer : initializers) {
sink.store(initializer.initialize(classFileLocator));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.*;
import java.util.jar.*;

import static net.bytebuddy.test.utility.FieldByFieldComparison.hasPrototype;
Expand Down Expand Up @@ -118,6 +115,22 @@ public void testSimpleTransformationWithInitialization() throws Exception {
verifyNoMoreInteractions(listener);
}

@Test
public void testSimpleTransformationWithClassPath() throws Exception {
ClassPathPlugin plugin = new ClassPathPlugin();
Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class);
Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory();
List<File> classPath = new ArrayList<File>();
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);
Expand Down Expand Up @@ -518,14 +531,20 @@ 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;

private PreprocessingPlugin(Plugin plugin) {
this.plugin = plugin;
}

public void processClassPath(Iterable<File> elements) {
if (plugin instanceof WithClassPath) {
((WithClassPath) plugin).processClassPath(elements);
}
}

public void onPreprocess(TypeDescription typeDescription, ClassFileLocator classFileLocator) {
/* empty */
}
Expand Down Expand Up @@ -575,4 +594,27 @@ public void close() throws IOException {
plugin.close();
}
}

private static class ClassPathPlugin implements Plugin.WithClassPath {

private final List<File> classPath = new ArrayList<File>();

public void processClassPath(Iterable<File> 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 */
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,12 @@ public static void apply(Logger logger,
}
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>();
classFileLocators.add(rootLocator);
List<File> classPathElements = new ArrayList<File>();
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 {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,13 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
: ClassFileVersion.ofJavaVersion(multiReleaseVersion);
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(classPath.size());
classFileLocators.add(ClassFileLocator.ForClassLoader.ofPlatformLoader());
List<File> classPathElements = new ArrayList<File>();
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;
Expand All @@ -438,6 +440,7 @@ protected Plugin.Engine.Summary transform(List<? extends String> 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);
Expand Down