Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some initialization steps (especially reflection) to Plugin's static initialization #166

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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