Refactor to reflection. · raphw/byte-buddy@91f4dc8 (original) (raw)
`@@ -31,13 +31,15 @@
`
31
31
`import org.gradle.api.Project;
`
32
32
`import org.gradle.api.artifacts.ArtifactView;
`
33
33
`import org.gradle.api.artifacts.Configuration;
`
``
34
`+
import org.gradle.api.artifacts.PublishArtifactSet;
`
34
35
`import org.gradle.api.attributes.Attribute;
`
35
36
`import org.gradle.api.attributes.AttributeCompatibilityRule;
`
36
37
`import org.gradle.api.attributes.AttributeContainer;
`
37
38
`import org.gradle.api.attributes.AttributeMatchingStrategy;
`
38
39
`import org.gradle.api.attributes.Category;
`
39
40
`import org.gradle.api.attributes.CompatibilityCheckDetails;
`
40
41
`import org.gradle.api.attributes.Usage;
`
``
42
`+
import org.gradle.api.component.Artifact;
`
41
43
`import org.gradle.api.file.FileCollection;
`
42
44
`import org.gradle.api.provider.Provider;
`
43
45
`import org.gradle.api.tasks.TaskProvider;
`
`@@ -46,6 +48,7 @@
`
46
48
`import java.lang.reflect.Method;
`
47
49
`import java.util.concurrent.ConcurrentHashMap;
`
48
50
`import java.util.concurrent.ConcurrentMap;
`
``
51
`+
import java.util.concurrent.ExecutionException;
`
49
52
``
50
53
`/**
`
51
54
` * A Byte Buddy plugin-variant to use in combination with Gradle's support for Android.
`
`@@ -60,13 +63,29 @@ public class ByteBuddyAndroidPlugin implements Plugin {
`
60
63
`/**
`
61
64
` * The dispatcher to use for registering the transformation.
`
62
65
` */
`
63
``
`-
protected static final TransformationDispatcher TRANSFORMATION_DISPATCHER = TransformationDispatcher.resolve();
`
``
66
`+
protected static final TransformationDispatcher TRANSFORMATION_DISPATCHER;
`
64
67
``
65
68
`/**
`
66
69
` * The name of the Byte Buddy jar type.
`
67
70
` */
`
68
71
`private static final String BYTE_BUDDY_JAR_TYPE = "bytebuddy-jar";
`
69
72
``
``
73
`+
static {
`
``
74
`+
TransformationDispatcher dispatcher;
`
``
75
`+
try {
`
``
76
`+
Class<?> scope = Class.forName("com.android.build.api.variant.ScopedArtifacts$Scope");
`
``
77
`+
@SuppressWarnings("unchecked")
`
``
78
`+
Object project = Enum.valueOf((Class) scope, "PROJECT");
`
``
79
`+
dispatcher = new TransformationDispatcher.ForApk74CompatibleAndroid(
`
``
80
`+
PublishArtifactSet.class.getMethod("forScope", scope),
`
``
81
`+
project,
`
``
82
`+
(Artifact) Class.forName("com.android.build.api.variant.ScopedArtifacts$CLASSES").getField("INSTANCE").get(null));
`
``
83
`+
} catch (Throwable ignored) {
`
``
84
`+
dispatcher = TransformationDispatcher.ForLegacyAndroid.INSTANCE;
`
``
85
`+
}
`
``
86
`+
TRANSFORMATION_DISPATCHER = dispatcher;
`
``
87
`+
}
`
``
88
+
70
89
`/**
`
71
90
` * {@inheritDoc}
`
72
91
` */
`
`@@ -416,10 +435,14 @@ public void execute(CompatibilityCheckDetails details) {
`
416
435
`/**
`
417
436
` * Used to wire the local transformation task depending on the API being used in the host project.
`
418
437
` */
`
419
``
`-
protected enum TransformationDispatcher {
`
420
``
`-
LEGACY {
`
``
438
`+
protected interface TransformationDispatcher {
`
``
439
+
``
440
`+
enum ForLegacyAndroid implements TransformationDispatcher {
`
``
441
+
``
442
`+
INSTANCE;
`
``
443
+
421
444
`@Override
`
422
``
`-
protected void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
`
``
445
`+
public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
`
423
446
`TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform",
`
424
447
`LegacyByteBuddyLocalClassesEnhancerTask.class,
`
425
448
`new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(configuration, project.getExtensions().getByType(BaseExtension.class), classPath));
`
`@@ -428,32 +451,36 @@ protected void accept(Project project, Variant variant, Configuration configurat
`
428
451
` .wiredWith(LegacyByteBuddyLocalClassesEnhancerTask::getLocalClassesDirs, LegacyByteBuddyLocalClassesEnhancerTask::getOutputDir)
`
429
452
` .toTransform(MultipleArtifact.ALL_CLASSES_DIRS.INSTANCE);
`
430
453
` }
`
431
``
`-
},
`
``
454
`+
}
`
``
455
+
``
456
`+
class ForApk74CompatibleAndroid implements TransformationDispatcher {
`
``
457
+
``
458
`+
private final Method forScope;
`
``
459
+
``
460
`+
private final Object scope;
`
``
461
+
``
462
`+
private final Artifact artifact;
`
``
463
+
``
464
`+
protected ForApk74CompatibleAndroid(Method forScope, Object scope, Artifact artifact) {
`
``
465
`+
this.forScope = forScope;
`
``
466
`+
this.scope = scope;
`
``
467
`+
this.artifact = artifact;
`
``
468
`+
}
`
432
469
``
433
``
`-
AKP_7_4_CAPABLE {
`
434
470
`@Override
`
435
``
`-
protected void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
`
``
471
`+
public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
`
436
472
`TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform",
`
437
473
`ByteBuddyLocalClassesEnhancerTask.class,
`
438
474
`new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(configuration, project.getExtensions().getByType(BaseExtension.class), classPath));
`
439
``
`-
variant.getArtifacts()
`
440
``
`-
.forScope(ScopedArtifacts.Scope.PROJECT)
`
441
``
`-
.use(provider)
`
442
``
`-
.toTransform(ScopedArtifact.CLASSES.INSTANCE, ByteBuddyLocalClassesEnhancerTask::getLocalJars, ByteBuddyLocalClassesEnhancerTask::getLocalClassesDirs, ByteBuddyLocalClassesEnhancerTask::getOutputFile);
`
443
``
`-
}
`
444
``
`-
};
`
445
``
-
446
``
`-
/**
`
447
``
`-
- Resolves an appropriate dispatcher for the current VM.
`
448
``
`-
`
449
``
`-
- @return The appropriate dispatcher.
`
450
``
`-
*/
`
451
``
`-
protected static TransformationDispatcher resolve() {
`
452
``
`-
try {
`
453
``
`-
Class.forName("com.android.build.api.variant.ScopedArtifacts");
`
454
``
`-
return AKP_7_4_CAPABLE;
`
455
``
`-
} catch (ClassNotFoundException ignored) {
`
456
``
`-
return LEGACY;
`
``
475
`+
try {
`
``
476
`+
((PublishArtifactSet) forScope.invoke(variant.getArtifacts(), scope))
`
``
477
`+
.use(provider)
`
``
478
`+
.toTransform(artifact, ByteBuddyLocalClassesEnhancerTask::getLocalJars, ByteBuddyLocalClassesEnhancerTask::getLocalClassesDirs, ByteBuddyLocalClassesEnhancerTask::getOutputFile);
`
``
479
`+
} catch (IllegalAccessException exception) {
`
``
480
`+
throw new IllegalStateException("Failed to variant scope", exception);
`
``
481
`+
} catch (InvocationTargetException exception) {
`
``
482
`+
throw new IllegalStateException("Failed to resolve runtime scope", exception.getCause());
`
``
483
`+
}
`
457
484
` }
`
458
485
` }
`
459
486
``
`@@ -465,6 +492,6 @@ protected static TransformationDispatcher resolve() {
`
465
492
` * @param configuration The configuration to use.
`
466
493
` * @param classPath The class path to use.
`
467
494
` */
`
468
``
`-
protected abstract void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath);
`
``
495
`+
void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath);
`
469
496
` }
`
470
497
`}
`