Skip to content

Commit

Permalink
Fix up plugin for Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 20, 2024
1 parent 9860522 commit 28c108d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package net.bytebuddy.build.gradle.android;

import groovy.lang.Closure;
import net.bytebuddy.build.EntryPoint;
import org.gradle.api.Action;
import org.gradle.api.Closure;
import org.gradle.api.Project;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import net.bytebuddy.build.BuildLogger;
import net.bytebuddy.build.EntryPoint;
import net.bytebuddy.build.Plugin;
import net.bytebuddy.build.gradle.GradleBuildLogger;
import net.bytebuddy.build.gradle.Transformation;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
Expand All @@ -33,34 +31,21 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.Directory;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
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.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipException;
import org.gradle.api.tasks.*;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipException;

/**
* Transformation task for instrumenting the project's local and dependencies' classes.
Expand Down Expand Up @@ -204,11 +189,26 @@ private static URL[] toUrls(Collection<File> files) {

/**
* Executes the plugin for transforming all project's classes.
*
* @throws IOException If an I/O exception occurs.
*/
@TaskAction
public void execute() {
List<Transformation> transformations = new ArrayList<Transformation>(getTransformations());
public void execute() throws IOException {
List<Transformation> transformations = new ArrayList<Transformation>(getTransformations().get());
Set<Plugin.Engine.Source> sources = new LinkedHashSet<Plugin.Engine.Source>();
Set<File> localClasspath = new HashSet<>();
for (Directory directory : getLocalClassesDirs().get()) {
File file = directory.getAsFile();
localClasspath.add(file);
sources.add(new Plugin.Engine.Source.ForFolder(file));
}
for (RegularFile jarFile : getInputJars().get()) {
sources.add(new Plugin.Engine.Source.ForJarFile(jarFile.getAsFile()));
}
Plugin.Engine.Summary summary;
ClassLoader classLoader = new URLClassLoader(
toUrls(getByteBuddyClasspath().getFiles()),
new URLClassLoader(toUrls(getAndroidBootClasspath().getFiles()), ByteBuddy.class.getClassLoader()));
try {
if (getDiscovery().get().isDiscover(transformations)) {
Set<String> undiscoverable = new HashSet<String>();
Expand Down Expand Up @@ -272,57 +272,40 @@ public void execute() {
classFileLocators.add(ClassFileLocator.ForClassLoader.of(ByteBuddy.class.getClassLoader()));
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators);
try {
Set<Plugin.Engine.Source> sources = new LinkedHashSet<Plugin.Engine.Source>();
Set<File> localClasspath = new HashSet<>();
for (Directory directory : getLocalClassesDirs().get()) {
File file = directory.getAsFile();
localClasspath.add(file);
sources.add(new Plugin.Engine.Source.ForFolder(file));
}
for (RegularFile jarFile : getInputJars().get()) {
sources.add(new Plugin.Engine.Source.ForJarFile(jarFile.getAsFile()));
}
ClassLoader classLoader = new URLClassLoader(
toUrls(getByteBuddyClasspath().getFiles()),
new URLClassLoader(toUrls(getAndroidBootClasspath().getFiles()), ByteBuddy.class.getClassLoader()));
try {
summary = Plugin.Engine.Default.of(getEntryPoint().get(), classFileVersion, getSuffix().get().length() == 0
? MethodNameTransformer.Suffixing.withRandomSuffix()
: new MethodNameTransformer.Suffixing(getSuffix().get()))
.with(getExtendedParsing().get()
? Plugin.Engine.PoolStrategy.Default.EXTENDED
: Plugin.Engine.PoolStrategy.Default.FAST)
.with(classFileLocator)
.with(new TransformationLogger(getLogger()))
.withErrorHandlers(Plugin.Engine.ErrorHandler.Enforcing.ALL_TYPES_RESOLVED, getFailOnLiveInitializer().get()
? Plugin.Engine.ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS
: Plugin.Engine.Listener.NoOp.INSTANCE, getFailFast().get()
? Plugin.Engine.ErrorHandler.Failing.FAIL_FAST
: Plugin.Engine.ErrorHandler.Failing.FAIL_LAST)
.with(getThreads().get() == 0
? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE
: new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(getThreads().get()))
.apply(new Plugin.Engine.Source.Compound(sources), new TargetForAndroidAppJarFile(getOutputFile().get().getAsFile()), factories);
} finally {
if (classLoader instanceof Closeable) {
((Closeable) classLoader).close();
}
if (classLoader.getParent() instanceof Closeable) {
((Closeable) classLoader.getParent()).close();
}
}
summary = Plugin.Engine.Default.of(getEntryPoint().get(), classFileVersion, getSuffix().get().length() == 0
? MethodNameTransformer.Suffixing.withRandomSuffix()
: new MethodNameTransformer.Suffixing(getSuffix().get()))
.with(getExtendedParsing().get()
? Plugin.Engine.PoolStrategy.Default.EXTENDED
: Plugin.Engine.PoolStrategy.Default.FAST)
.with(classFileLocator)
.with(new TransformationLogger(getLogger()))
.withErrorHandlers(Plugin.Engine.ErrorHandler.Enforcing.ALL_TYPES_RESOLVED, getFailOnLiveInitializer().get()
? Plugin.Engine.ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS
: Plugin.Engine.Listener.NoOp.INSTANCE, getFailFast().get()
? Plugin.Engine.ErrorHandler.Failing.FAIL_FAST
: Plugin.Engine.ErrorHandler.Failing.FAIL_LAST)
.with(getThreads().get() == 0
? Plugin.Engine.Dispatcher.ForSerialTransformation.Factory.INSTANCE
: new Plugin.Engine.Dispatcher.ForParallelTransformation.WithThrowawayExecutorService.Factory(getThreads().get()))
.apply(new Plugin.Engine.Source.Compound(sources), new TargetForAndroidAppJarFile(getOutputFile().get().getAsFile()), factories);
} finally {
classFileLocator.close();
}
if (!summary.getFailed().isEmpty()) {
throw new IllegalStateException(summary.getFailed() + " type transformation(s) have failed");
} else if (getWarnOnEmptyTypeSet().get() && summary.getTransformed().isEmpty()) {
getLogger().warn("No types were transformed during plugin execution");
} else {
getLogger().info("Transformed {} type(s)", summary.getTransformed().size());
} finally {
if (classLoader instanceof Closeable) {
((Closeable) classLoader).close();
}
} catch (IOException exception) {
throw new GradleException("Failed to transform classes", exception);
if (classLoader.getParent() instanceof Closeable) {
((Closeable) classLoader.getParent()).close();
}
}
if (!summary.getFailed().isEmpty()) {
throw new IllegalStateException(summary.getFailed() + " type transformation(s) have failed");
} else if (getWarnOnEmptyTypeSet().get() && summary.getTransformed().isEmpty()) {
getLogger().warn("No types were transformed during plugin execution");
} else {
getLogger().info("Transformed {} type(s)", summary.getTransformed().size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package net.bytebuddy.build.gradle.android;

import net.bytebuddy.build.gradle.Transformation;

import java.util.List;

/**
Expand All @@ -29,7 +27,7 @@ public enum Discovery {
*/
ALL(false) {
@Override
protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> transformations) {
protected boolean isDiscover(List<Transformation> transformations) {
return true;
}
},
Expand All @@ -39,7 +37,7 @@ protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> tra
*/
UNIQUE(true) {
@Override
protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> transformations) {
protected boolean isDiscover(List<Transformation> transformations) {
return true;
}
},
Expand All @@ -49,7 +47,7 @@ protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> tra
*/
EMPTY(true) {
@Override
protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> transformations) {
protected boolean isDiscover(List<Transformation> transformations) {
return transformations.isEmpty();
}
},
Expand All @@ -59,7 +57,7 @@ protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> tra
*/
NONE(true) {
@Override
protected boolean isDiscover(List<net.bytebuddy.build.gradle.Transformation> transformations) {
protected boolean isDiscover(List<Transformation> transformations) {
return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ public static class ConfigurationAction implements Action<LegacyByteBuddyLocalCl
* The current variant's Byte Buddy configuration.
*/
private final FileCollection byteBuddyConfiguration;

/**
* The android gradle extension.
*/

private final BaseExtension androidExtension;

/**
* The current variant's runtime classpath.
*/

private final FileCollection runtimeClasspath;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import groovy.lang.Closure;
import net.bytebuddy.build.Plugin;
import net.bytebuddy.build.gradle.PluginArgument;
import net.bytebuddy.utility.nullability.MaybeNull;
import net.bytebuddy.utility.nullability.UnknownNull;
import org.gradle.api.Action;
Expand Down

0 comments on commit 28c108d

Please sign in to comment.