Skip to content

Commit 28400eb

Browse files
committed
Provide classPath elements to plugins
1 parent 01d06f9 commit 28400eb

File tree

4 files changed

+122
-17
lines changed

4 files changed

+122
-17
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ interface WithInitialization extends Plugin {
112112
Map<TypeDescription, byte[]> initialize(ClassFileLocator classFileLocator);
113113
}
114114

115+
interface WithClassPath extends Plugin {
116+
117+
/**
118+
* Provides the classPath elements from the {@link Plugin.Engine}.
119+
*
120+
* @param elements The classPath elements
121+
*/
122+
void processClassPath(Iterable<File> elements);
123+
}
124+
115125
/**
116126
* A factory for providing a build plugin.
117127
*/
@@ -872,6 +882,14 @@ interface Engine {
872882
*/
873883
Engine ignore(ElementMatcher<? super TypeDescription> matcher);
874884

885+
/**
886+
* Appends the classPath elements to the Engine.
887+
*
888+
* @param elements The classPath elements
889+
* @return A new plugin engine that is equal to this engine but with the supplied classpath elements.
890+
*/
891+
Engine withClassPath(Iterable<File> elements);
892+
875893
/**
876894
* Applies this plugin engine onto a given source and target.
877895
*
@@ -4659,6 +4677,11 @@ class Default extends AbstractBase {
46594677
*/
46604678
private final ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher;
46614679

4680+
/**
4681+
* The classPath elements.
4682+
*/
4683+
private final Iterable<File> classPath;
4684+
46624685
/**
46634686
* Creates a new default plugin engine that rebases types and fails fast and on unresolved types and on live initializers.
46644687
*/
@@ -4692,7 +4715,8 @@ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy) {
46924715
ErrorHandler.Enforcing.ALL_TYPES_RESOLVED,
46934716
ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS),
46944717
Dispatcher.ForSerialTransformation.Factory.INSTANCE,
4695-
none());
4718+
none(),
4719+
new ArrayList<File>());
46964720
}
46974721

46984722
/**
@@ -4707,6 +4731,7 @@ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy) {
47074731
* @param errorHandler The error handler to use.
47084732
* @param dispatcherFactory The dispatcher factory to use.
47094733
* @param ignoredTypeMatcher A matcher for types to exclude from transformation.
4734+
* @param classPath The classPath elements.
47104735
*/
47114736
protected Default(ByteBuddy byteBuddy,
47124737
TypeStrategy typeStrategy,
@@ -4716,7 +4741,8 @@ protected Default(ByteBuddy byteBuddy,
47164741
Listener listener,
47174742
ErrorHandler errorHandler,
47184743
Dispatcher.Factory dispatcherFactory,
4719-
ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher) {
4744+
ElementMatcher.Junction<? super TypeDescription> ignoredTypeMatcher,
4745+
Iterable<File> classPath) {
47204746
this.byteBuddy = byteBuddy;
47214747
this.typeStrategy = typeStrategy;
47224748
this.poolStrategy = poolStrategy;
@@ -4726,6 +4752,7 @@ protected Default(ByteBuddy byteBuddy,
47264752
this.errorHandler = errorHandler;
47274753
this.dispatcherFactory = dispatcherFactory;
47284754
this.ignoredTypeMatcher = ignoredTypeMatcher;
4755+
this.classPath = classPath;
47294756
}
47304757

47314758
/**
@@ -4796,7 +4823,8 @@ public Engine with(ByteBuddy byteBuddy) {
47964823
listener,
47974824
errorHandler,
47984825
dispatcherFactory,
4799-
ignoredTypeMatcher);
4826+
ignoredTypeMatcher,
4827+
classPath);
48004828
}
48014829

48024830
/**
@@ -4811,7 +4839,8 @@ public Engine with(TypeStrategy typeStrategy) {
48114839
listener,
48124840
errorHandler,
48134841
dispatcherFactory,
4814-
ignoredTypeMatcher);
4842+
ignoredTypeMatcher,
4843+
classPath);
48154844
}
48164845

48174846
/**
@@ -4826,7 +4855,8 @@ public Engine with(PoolStrategy poolStrategy) {
48264855
listener,
48274856
errorHandler,
48284857
dispatcherFactory,
4829-
ignoredTypeMatcher);
4858+
ignoredTypeMatcher,
4859+
classPath);
48304860
}
48314861

48324862
/**
@@ -4841,7 +4871,8 @@ public Engine with(ClassFileLocator classFileLocator) {
48414871
listener,
48424872
errorHandler,
48434873
dispatcherFactory,
4844-
ignoredTypeMatcher);
4874+
ignoredTypeMatcher,
4875+
classPath);
48454876
}
48464877

48474878
/**
@@ -4856,7 +4887,8 @@ public Engine with(@MaybeNull ClassFileVersion classFileVersion) {
48564887
listener,
48574888
errorHandler,
48584889
dispatcherFactory,
4859-
ignoredTypeMatcher);
4890+
ignoredTypeMatcher,
4891+
classPath);
48604892
}
48614893

48624894
/**
@@ -4871,7 +4903,8 @@ public Engine with(Listener listener) {
48714903
new Listener.Compound(this.listener, listener),
48724904
errorHandler,
48734905
dispatcherFactory,
4874-
ignoredTypeMatcher);
4906+
ignoredTypeMatcher,
4907+
classPath);
48754908
}
48764909

48774910
/**
@@ -4886,7 +4919,8 @@ public Engine withoutErrorHandlers() {
48864919
listener,
48874920
Listener.NoOp.INSTANCE,
48884921
dispatcherFactory,
4889-
ignoredTypeMatcher);
4922+
ignoredTypeMatcher,
4923+
classPath);
48904924
}
48914925

48924926
/**
@@ -4901,7 +4935,8 @@ public Engine withErrorHandlers(List<? extends ErrorHandler> errorHandlers) {
49014935
listener,
49024936
new ErrorHandler.Compound(errorHandlers),
49034937
dispatcherFactory,
4904-
ignoredTypeMatcher);
4938+
ignoredTypeMatcher,
4939+
classPath);
49054940
}
49064941

49074942
/**
@@ -4916,7 +4951,8 @@ public Engine with(Dispatcher.Factory dispatcherFactory) {
49164951
listener,
49174952
errorHandler,
49184953
dispatcherFactory,
4919-
ignoredTypeMatcher);
4954+
ignoredTypeMatcher,
4955+
classPath);
49204956
}
49214957

49224958
/**
@@ -4931,7 +4967,21 @@ public Engine ignore(ElementMatcher<? super TypeDescription> matcher) {
49314967
listener,
49324968
errorHandler,
49334969
dispatcherFactory,
4934-
ignoredTypeMatcher.<TypeDescription>or(matcher));
4970+
ignoredTypeMatcher.<TypeDescription>or(matcher),
4971+
classPath);
4972+
}
4973+
4974+
public Engine withClassPath(Iterable<File> elements) {
4975+
return new Default(byteBuddy,
4976+
typeStrategy,
4977+
poolStrategy,
4978+
classFileLocator,
4979+
classFileVersion,
4980+
listener,
4981+
errorHandler,
4982+
dispatcherFactory,
4983+
ignoredTypeMatcher,
4984+
elements);
49354985
}
49364986

49374987
/**
@@ -4946,6 +4996,7 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
49464996
List<Plugin> plugins = new ArrayList<Plugin>(factories.size());
49474997
List<WithInitialization> initializers = new ArrayList<WithInitialization>();
49484998
List<WithPreprocessor> preprocessors = new ArrayList<WithPreprocessor>();
4999+
List<WithClassPath> classPathers = new ArrayList<WithClassPath>();
49495000
try {
49505001
for (Plugin.Factory factory : factories) {
49515002
Plugin plugin = factory.make();
@@ -4956,6 +5007,9 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
49565007
if (plugin instanceof WithInitialization) {
49575008
initializers.add((WithInitialization) plugin);
49585009
}
5010+
if (plugin instanceof WithClassPath) {
5011+
classPathers.add((WithClassPath) plugin);
5012+
}
49595013
}
49605014
Source.Origin origin = source.read();
49615015
try {
@@ -4965,6 +5019,9 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
49655019
listener.onManifest(manifest);
49665020
Target.Sink sink = target.write(manifest);
49675021
try {
5022+
for (WithClassPath classPather : classPathers) {
5023+
classPather.processClassPath(classPath);
5024+
}
49685025
for (WithInitialization initializer : initializers) {
49695026
sink.store(initializer.initialize(classFileLocator));
49705027
}

byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineDefaultTest.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import java.io.File;
2424
import java.io.FileOutputStream;
2525
import java.io.IOException;
26-
import java.util.Arrays;
27-
import java.util.Collection;
28-
import java.util.Collections;
29-
import java.util.Map;
26+
import java.util.*;
3027
import java.util.jar.*;
3128

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

118+
@Test
119+
public void testSimpleTransformationWithClassPath() throws Exception {
120+
ClassPathPlugin plugin = new ClassPathPlugin();
121+
Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class);
122+
Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory();
123+
List<File> classPath = new ArrayList<File>();
124+
classPath.add(new File(""));
125+
Plugin.Engine.Summary summary = new Plugin.Engine.Default()
126+
.with(ClassFileLocator.ForClassLoader.of(SimplePlugin.class.getClassLoader()))
127+
.with(dispatcherFactory)
128+
.withClassPath(classPath)
129+
.apply(source, target, new Plugin.Factory.Simple(plugin));
130+
assertThat(plugin.classPath.size(), is(classPath.size()));
131+
assertThat(plugin.classPath, hasItem(classPath.get(0)));
132+
}
133+
121134
@Test
122135
public void testSimpleTransformationIgnoredByPlugin() throws Exception {
123136
Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class);
@@ -518,14 +531,20 @@ public void close() {
518531
}
519532
}
520533

521-
private static class PreprocessingPlugin implements Plugin.WithPreprocessor, Plugin.WithInitialization {
534+
private static class PreprocessingPlugin implements Plugin.WithPreprocessor, Plugin.WithInitialization, Plugin.WithClassPath {
522535

523536
private final Plugin plugin;
524537

525538
private PreprocessingPlugin(Plugin plugin) {
526539
this.plugin = plugin;
527540
}
528541

542+
public void processClassPath(Iterable<File> elements) {
543+
if (plugin instanceof WithClassPath) {
544+
((WithClassPath) plugin).processClassPath(elements);
545+
}
546+
}
547+
529548
public void onPreprocess(TypeDescription typeDescription, ClassFileLocator classFileLocator) {
530549
/* empty */
531550
}
@@ -575,4 +594,27 @@ public void close() throws IOException {
575594
plugin.close();
576595
}
577596
}
597+
598+
private static class ClassPathPlugin implements Plugin.WithClassPath {
599+
600+
private final List<File> classPath = new ArrayList<File>();
601+
602+
public void processClassPath(Iterable<File> elements) {
603+
for (File element : elements) {
604+
classPath.add(element);
605+
}
606+
}
607+
608+
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
609+
return builder;
610+
}
611+
612+
public boolean matches(TypeDescription target) {
613+
return false;
614+
}
615+
616+
public void close() {
617+
/* empty */
618+
}
619+
}
578620
}

byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,12 @@ public static void apply(Logger logger,
511511
}
512512
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>();
513513
classFileLocators.add(rootLocator);
514+
List<File> classPathElements = new ArrayList<File>();
514515
for (File artifact : artifacts) {
515516
classFileLocators.add(artifact.isFile()
516517
? ClassFileLocator.ForJarFile.of(artifact, multiReleaseClassFileVersion)
517518
: ClassFileLocator.ForFolder.of(artifact, multiReleaseClassFileVersion));
519+
classPathElements.add(artifact);
518520
}
519521
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators);
520522
try {
@@ -535,6 +537,7 @@ public static void apply(Logger logger,
535537
.with(threads == 0
536538
? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE
537539
: new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(threads))
540+
.withClassPath(classPathElements)
538541
.apply(source, target, factories);
539542
} finally {
540543
classFileLocator.close();

byte-buddy-maven-plugin/src/main/java/net/bytebuddy/build/maven/ByteBuddyMojo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,13 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
410410
: ClassFileVersion.ofJavaVersion(multiReleaseVersion);
411411
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(classPath.size());
412412
classFileLocators.add(ClassFileLocator.ForClassLoader.ofPlatformLoader());
413+
List<File> classPathElements = new ArrayList<File>();
413414
for (String element : classPath) {
414415
File artifact = new File(element);
415416
classFileLocators.add(artifact.isFile()
416417
? ClassFileLocator.ForJarFile.of(artifact, multiReleaseClassFileVersion)
417418
: ClassFileLocator.ForFolder.of(artifact, multiReleaseClassFileVersion));
419+
classPathElements.add(artifact);
418420
}
419421
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators);
420422
Plugin.Engine.Summary summary;
@@ -438,6 +440,7 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
438440
failOnLiveInitializer ? Plugin.Engine.ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS : Plugin.Engine.Listener.NoOp.INSTANCE,
439441
failFast ? Plugin.Engine.ErrorHandler.Failing.FAIL_FAST : Plugin.Engine.ErrorHandler.Failing.FAIL_LAST)
440442
.with(threads == 0 ? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE : new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(threads))
443+
.withClassPath(classPathElements)
441444
.apply(source, target, factories);
442445
} catch (Throwable throwable) {
443446
throw new MojoExecutionException("Failed to transform class files in " + file, throwable);

0 commit comments

Comments
 (0)