diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index 9ee29665..f749a763 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -20,10 +20,15 @@ import groovy.lang.GroovyCodeSource; import groovy.util.DelegatingScript; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; @@ -61,6 +66,12 @@ public class ForbiddenApisPlugin implements Plugin { /** Minimum Gradle version this plugin requires to run (v3.2). */ public static final GradleVersion MIN_GRADLE_VERSION = GradleVersion.version("3.2"); + /** True, if this version of Gradle supports task avoidance API (>=v4.9). */ + public static final boolean TASK_AVOIDANCE_AVAILABLE = GradleVersion.current().compareTo(GradleVersion.version("4.9")) >= 0; + + /** All properties that our ForbiddenApisExtension provides. Used by plugin init script to create convention mapping. */ + public static final List FORBIDDEN_APIS_EXTENSION_PROPS = determineExtensionProps(); + /** Java Package that contains the Gradle Daemon (needed to detect it on startup). */ private static final String GRADLE_DAEMON_PACKAGE = "org.gradle.launcher.daemon."; @@ -97,6 +108,17 @@ public Class run() { }); } + private static List determineExtensionProps() { + final List props = new ArrayList<>(); + for (final Field f : CheckForbiddenApisExtension.class.getDeclaredFields()) { + final int mods = f.getModifiers(); + if (Modifier.isPublic(mods) && !f.isSynthetic() && !Modifier.isStatic(mods)) { + props.add(f.getName()); + } + } + return Collections.unmodifiableList(props); + } + private static boolean isGradleDaemon() { // see: http://stackoverflow.com/questions/23265217/how-to-know-whether-you-are-running-inside-a-gradle-daemon if (System.getProperty("sun.java.command", "").startsWith(GRADLE_DAEMON_PACKAGE)) { diff --git a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy index 8d301484..fa6e0ce6 100644 --- a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy +++ b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy @@ -20,16 +20,10 @@ import java.lang.reflect.Modifier; import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.util.GradleVersion; -final boolean TASK_AVOIDANCE_AVAILABLE = GradleVersion.current() >= GradleVersion.version("4.9"); - project.plugins.apply(JavaBasePlugin.class); // create Extension for defaults: def extension = project.extensions.create(FORBIDDEN_APIS_EXTENSION_NAME, CheckForbiddenApisExtension.class, project); -def extensionProps = CheckForbiddenApisExtension.class.declaredFields.findAll{ f -> - int mods = f.modifiers; - return Modifier.isPublic(mods) && !f.synthetic && !Modifier.isStatic(mods) -}*.name; // Create a convenience task for all checks (this does not conflict with extension, as it has higher priority in DSL): def forbiddenTask = TASK_AVOIDANCE_AVAILABLE ? project.tasks.register(FORBIDDEN_APIS_TASK_NAME) : project.tasks.create(FORBIDDEN_APIS_TASK_NAME) @@ -48,7 +42,7 @@ project.sourceSets.all{ sourceSet -> dependsOn(sourceSet.output); outputs.upToDateWhen { true } conventionMapping.with{ - extensionProps.each{ key -> + FORBIDDEN_APIS_EXTENSION_PROPS.each{ key -> map(key, { extension[key] }); } classesDirs = { sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : project.files(sourceSet.output.classesDir) }