Skip to content

Commit bcb5d26

Browse files
committed
Avoid compilation issue on older JVMs.
1 parent e42d726 commit bcb5d26

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.file.DirectoryProperty;
2424
import org.gradle.api.file.FileCollection;
2525
import org.gradle.api.file.SourceDirectorySet;
26+
import org.gradle.api.plugins.ExtensionContainer;
2627
import org.gradle.api.tasks.SourceSet;
2728
import org.gradle.api.tasks.compile.AbstractCompile;
2829

@@ -61,17 +62,29 @@ public class KotlinByteBuddyTaskConfiguration implements Action<Project> {
6162
private static final Method GET_LIBRARIES;
6263

6364
/**
64-
* The {@code SourceDirectorySet#getDestinationDirectory} method, or {@code null} on legacy Gradle.
65+
* The {@code org.gradle.api.tasks.SourceSet#getExtensions} method, or {@code null} on legacy Gradle.
66+
*/
67+
@MaybeNull
68+
private static final Method GET_EXTENSIONS;
69+
70+
/**
71+
* The {@code org.gradle.api.file.SourceDirectorySet#getDestinationDirectory} method, or {@code null} on legacy Gradle.
6572
*/
6673
@MaybeNull
6774
private static final Method GET_DESTINATION_DIRECTORY_SOURCE;
6875

76+
/**
77+
* The {@code org.gradle.api.tasks.compile.AbstractCompile#getDestinationDirectory} method, or {@code null} on legacy Gradle.
78+
*/
79+
@MaybeNull
80+
private static final Method GET_DESTINATION_DIRECTORY_TARGET;
81+
6982
/*
7083
* Resolves Kotlin Gradle plugin API entry points, if available.
7184
*/
7285
static {
7386
Class<?> kotlinCompileTool;
74-
Method getDestinationDirectory, getLibraries, getDestinationDirectorySource;
87+
Method getDestinationDirectory, getLibraries;
7588
try {
7689
kotlinCompileTool = Class.forName("org.jetbrains.kotlin.gradle.tasks.KotlinCompileTool");
7790
getDestinationDirectory = kotlinCompileTool.getMethod("getDestinationDirectory");
@@ -81,25 +94,36 @@ public class KotlinByteBuddyTaskConfiguration implements Action<Project> {
8194
getDestinationDirectory = null;
8295
getLibraries = null;
8396
}
97+
Method getExtensions, getDestinationDirectorySource, getDestinationDirectoryTarget;
8498
try {
99+
getExtensions = SourceSet.class.getMethod("getExtensions");
85100
getDestinationDirectorySource = SourceDirectorySet.class.getMethod("getDestinationDirectory");
101+
getDestinationDirectoryTarget = AbstractCompile.class.getMethod("getDestinationDirectory");
86102
} catch (Throwable ignored) {
103+
getExtensions = null;
87104
getDestinationDirectorySource = null;
105+
getDestinationDirectoryTarget = null;
88106
}
89107
KOTLIN_COMPILE_TOOL = kotlinCompileTool;
90108
GET_DESTINATION_DIRECTORY = getDestinationDirectory;
91109
GET_LIBRARIES = getLibraries;
110+
GET_EXTENSIONS = getExtensions;
92111
GET_DESTINATION_DIRECTORY_SOURCE = getDestinationDirectorySource;
112+
GET_DESTINATION_DIRECTORY_TARGET = getDestinationDirectoryTarget;
93113
}
94114

95115
/**
96116
* Returns {@code true} if the Kotlin Gradle plugin API is available on the classpath and the
97-
* running Gradle version exposes {@code SourceDirectorySet#getDestinationDirectory}.
117+
* running Gradle version exposes {@code SourceSet#getExtensions}, {@code SourceDirectorySet#getDestinationDirectory}
118+
* and {@code AbstractCompile#getDestinationDirectory}.
98119
*
99120
* @return {@code true} if Kotlin support can be wired.
100121
*/
101122
public static boolean isAvailable() {
102-
return KOTLIN_COMPILE_TOOL != null && GET_DESTINATION_DIRECTORY_SOURCE != null;
123+
return KOTLIN_COMPILE_TOOL != null
124+
&& GET_EXTENSIONS != null
125+
&& GET_DESTINATION_DIRECTORY_SOURCE != null
126+
&& GET_DESTINATION_DIRECTORY_TARGET != null;
103127
}
104128

105129
/**
@@ -139,7 +163,12 @@ public void execute(Project project) {
139163
sourceSet.getName(), kotlinCompileTaskName);
140164
return;
141165
}
142-
Object kotlinSources = sourceSet.getExtensions().findByName("kotlin");
166+
Object kotlinSources;
167+
try {
168+
kotlinSources = ((ExtensionContainer) GET_EXTENSIONS.invoke(sourceSet)).findByName("kotlin");
169+
} catch (Exception exception) {
170+
throw new GradleException("Could not resolve extensions of source set " + sourceSet.getName(), exception);
171+
}
143172
if (!(kotlinSources instanceof SourceDirectorySet)) {
144173
project.getLogger().debug("Skipping Kotlin Byte Buddy configuration for source set '{}': no 'kotlin' SourceDirectorySet extension",
145174
sourceSet.getName());
@@ -167,7 +196,11 @@ public void execute(Project project) {
167196
Task compileJavaTask = project.getTasks().findByName(sourceSet.getCompileJavaTaskName());
168197
if (compileJavaTask instanceof AbstractCompile) {
169198
byteBuddyTask.dependsOn(compileJavaTask);
170-
byteBuddyTask.getClassPath().from(((AbstractCompile) compileJavaTask).getDestinationDirectory());
199+
try {
200+
byteBuddyTask.getClassPath().from(GET_DESTINATION_DIRECTORY_TARGET.invoke(compileJavaTask));
201+
} catch (Exception exception) {
202+
throw new GradleException("Could not resolve destination directory of " + compileJavaTask.getName(), exception);
203+
}
171204
}
172205
Task classesTask = project.getTasks().findByName(sourceSet.getClassesTaskName());
173206
if (classesTask != null) {

0 commit comments

Comments
 (0)