Skip to content

Commit

Permalink
Add additional arguments to plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Dec 11, 2024
1 parent 01d06f9 commit f071a02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.gradle.api.file.*;
import org.gradle.api.logging.Logger;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;
Expand Down Expand Up @@ -226,7 +226,7 @@ public void execute() throws IOException {
try {
Class<?> discovery = Class.forName("net.bytebuddy.build.gradle.Discovery");
Class.forName("net.bytebuddy.build.gradle.AbstractByteBuddyTask").getMethod("apply",
Logger.class,
Project.class,
ClassLoader.class,
List.class,
discovery,
Expand All @@ -245,7 +245,7 @@ public void execute() throws IOException {
boolean.class,
Plugin.Engine.Source.class,
Plugin.Engine.Target.class).invoke(null,
getLogger(),
getProject(),
classLoader,
transformations,
discovery.getMethod("valueOf", String.class).invoke(null, getDiscovery().get().name()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.bytebuddy.utility.nullability.UnknownNull;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
Expand Down Expand Up @@ -399,7 +400,7 @@ protected void doApply(Plugin.Engine.Source source, Plugin.Engine.Target target)
classFileVersion = this.classFileVersion;
getLogger().debug("Java version was configured: {}", classFileVersion.getJavaVersion());
}
apply(getLogger(),
apply(getProject(),
getClass().getClassLoader(),
new ArrayList<Transformation>(getTransformations()),
getDiscovery(),
Expand All @@ -423,7 +424,7 @@ protected void doApply(Plugin.Engine.Source source, Plugin.Engine.Target target)
/**
* Dispatches a Byte Buddy instrumentation Gradle task.
*
* @param logger The logger to use.
* @param project The current project.
* @param rootLoader The class loader that is used for searching types and applying plugins.
* @param transformations The transformations to apply.
* @param discovery The discovery for plugins to use.
Expand All @@ -444,7 +445,7 @@ protected void doApply(Plugin.Engine.Source source, Plugin.Engine.Target target)
* @param target The target to use for instrumenting.
* @throws IOException If an I/O error occurs.
*/
public static void apply(Logger logger,
public static void apply(Project project,
ClassLoader rootLoader,
List<Transformation> transformations,
Discovery discovery,
Expand Down Expand Up @@ -484,27 +485,33 @@ public static void apply(Logger logger,
} catch (ClassNotFoundException exception) {
throw new IllegalStateException("Discovered plugin is not available: " + name, exception);
}
logger.debug("Registered discovered plugin: {}", name);
project.getLogger().debug("Registered discovered plugin: {}", name);
} else {
logger.info("Skipping discovered plugin {} which was previously discovered or registered", name);
project.getLogger().info("Skipping discovered plugin {} which was previously discovered or registered", name);
}
}
}
if (transformations.isEmpty()) {
logger.warn("No transformations are specified or discovered. Application will be non-operational.");
project.getLogger().warn("No transformations are specified or discovered. Application will be non-operational.");
} else {
logger.debug("{} plugins are being applied via configuration and discovery", transformations.size());
project.getLogger().debug("{} plugins are being applied via configuration and discovery", transformations.size());
}
List<File> classPath = new ArrayList<File>();
for (File file : artifacts) {
classPath.add(file);
}
List<Plugin.Factory> factories = new ArrayList<Plugin.Factory>(transformations.size());
for (Transformation transformation : transformations) {
try {
factories.add(new Plugin.Factory.UsingReflection(transformation.toPlugin(classLoader))
.with(transformation.makeArgumentResolvers())
.with(rootLocationResolver,
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Logger.class, logger),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(org.slf4j.Logger.class, logger),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, new GradleBuildLogger(logger))));
logger.info("Resolved plugin: {}", transformation.toPluginName());
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Logger.class, project.getLogger()),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(org.slf4j.Logger.class, project.getLogger()),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, new GradleBuildLogger(project.getLogger())),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Project.class, project),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(File[].class, classPath.toArray(new File[0]))));
project.getLogger().info("Resolved plugin: {}", transformation.toPluginName());
} catch (Throwable throwable) {
throw new IllegalStateException("Cannot resolve plugin: " + transformation.toPluginName(), throwable);
}
Expand All @@ -526,7 +533,7 @@ public static void apply(Logger logger,
: Plugin.Engine.PoolStrategy.Default.FAST)
.with(classFileLocator)
.with(multiReleaseClassFileVersion)
.with(new TransformationLogger(logger))
.with(new TransformationLogger(project.getLogger()))
.withErrorHandlers(Plugin.Engine.ErrorHandler.Enforcing.ALL_TYPES_RESOLVED, failOnLiveInitializer
? Plugin.Engine.ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS
: Plugin.Engine.Listener.NoOp.INSTANCE, failFast
Expand All @@ -547,9 +554,9 @@ public static void apply(Logger logger,
if (!summary.getFailed().isEmpty()) {
throw new IllegalStateException(summary.getFailed() + " type transformation(s) have failed");
} else if (warnOnEmptyTypeSet && summary.getTransformed().isEmpty()) {
logger.warn("No types were transformed during plugin execution");
project.getLogger().warn("No types were transformed during plugin execution");
} else {
logger.info("Transformed {} type(s)", summary.getTransformed().size());
project.getLogger().info("Transformed {} type(s)", summary.getTransformed().size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
stalenessFilter = null;
getLog().debug("Did not discover previous staleness file");
}
List<File> artifacts = new ArrayList<File>(classPath.size());
for (String element : classPath) {
artifacts.add(new File(element));
}
ClassLoaderResolver classLoaderResolver = new ClassLoaderResolver(getLog(), repositorySystem, repositorySystemSession == null ? MavenRepositorySystemUtils.newSession() : repositorySystemSession, project.getRemotePluginRepositories());
try {
List<Plugin.Factory> factories = new ArrayList<Plugin.Factory>(transformers.size());
Expand All @@ -384,7 +388,9 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
.with(transformer.toArgumentResolvers())
.with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(File.class, file),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Log.class, getLog()),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, new MavenBuildLogger(getLog()))));
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, new MavenBuildLogger(getLog())),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(MavenProject.class, project),
Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(File[].class, artifacts.toArray(new File[0]))));
getLog().info("Resolved plugin: " + plugin);
} catch (Throwable throwable) {
throw new MojoExecutionException("Cannot resolve plugin: " + plugin, throwable);
Expand All @@ -408,10 +414,9 @@ protected Plugin.Engine.Summary transform(List<? extends String> classPath,
ClassFileVersion multiReleaseClassFileVersion = multiReleaseVersion == null
? classFileVersion
: ClassFileVersion.ofJavaVersion(multiReleaseVersion);
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(classPath.size());
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(artifacts.size());
classFileLocators.add(ClassFileLocator.ForClassLoader.ofPlatformLoader());
for (String element : classPath) {
File artifact = new File(element);
for (File artifact : artifacts) {
classFileLocators.add(artifact.isFile()
? ClassFileLocator.ForJarFile.of(artifact, multiReleaseClassFileVersion)
: ClassFileLocator.ForFolder.of(artifact, multiReleaseClassFileVersion));
Expand Down

0 comments on commit f071a02

Please sign in to comment.