Skip to content

Commit

Permalink
Move some initialization steps (especially reflection) to Plugin's st…
Browse files Browse the repository at this point in the history
…atic initialization (#166)
  • Loading branch information
uschindler authored May 7, 2020
1 parent 6e1c5a8 commit bcd08c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +66,12 @@ public class ForbiddenApisPlugin implements Plugin<Project> {
/** 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 (&gt;=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<String> 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.";

Expand Down Expand Up @@ -97,6 +108,17 @@ public Class<? extends DelegatingScript> run() {
});
}

private static List<String> determineExtensionProps() {
final List<String> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) }
Expand Down

0 comments on commit bcd08c0

Please sign in to comment.