Skip to content

Commit

Permalink
Simplify plugin init: move the Daemon checker and init of extension t…
Browse files Browse the repository at this point in the history
…o not rely on plugin-init.groovy and a current project. This allows custom variants of the task to be initialized correctly.
  • Loading branch information
uschindler committed Apr 9, 2020
1 parent 46177a5 commit 3242ec4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class CheckForbiddenApis extends DefaultTask implements PatternFilterable

private static final String NL = System.getProperty("line.separator", "\n");

private final CheckForbiddenApisExtension data = new CheckForbiddenApisExtension();
private final CheckForbiddenApisExtension data = new CheckForbiddenApisExtension(this.getProject());
private final PatternSet patternSet = new PatternSet().include("**/*.class");
private FileCollection classesDirs;
private FileCollection classpath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Set;

import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;

/**
Expand All @@ -32,7 +33,11 @@
*/
public class CheckForbiddenApisExtension {

public FileCollection signaturesFiles; // initialized by plugin-init.groovy
public CheckForbiddenApisExtension(Project project) {
signaturesFiles = project.files();
}

public FileCollection signaturesFiles;
public Set<URL> signaturesURLs = new LinkedHashSet<>();
public List<String> signatures = new ArrayList<>();
public Set<String> bundledSignatures = new LinkedHashSet<>(),
Expand All @@ -41,6 +46,6 @@ public class CheckForbiddenApisExtension {
failOnMissingClasses = true,
failOnUnresolvableSignatures = true,
ignoreFailures = false,
disableClassloadingCache = false;
disableClassloadingCache = ForbiddenApisPlugin.DEFAULT_DISABLE_CLASSLOADING_CACHE;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.atomic.AtomicBoolean;

import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.util.GradleVersion;

/**
Expand All @@ -38,26 +39,37 @@
*/
public class ForbiddenApisPlugin implements Plugin<Project> {

/** Resource with Groovy script that initializes the plugin. */
private static final String PLUGIN_INIT_SCRIPT = "plugin-init.groovy";
private static final Logger LOG = Logging.getLogger(ForbiddenApisPlugin.class);

/** Name of the base task that depends on one for every SourceSet. */
public static final String FORBIDDEN_APIS_TASK_NAME = "forbiddenApis";

/** Name of the extension to define defaults for all tasks of this module. */
public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis";

/**
* Default value for {@link CheckForbiddenApis#getDisableClassloadingCache}.
* <p>
* The default is {@code false}, unless the plugin detects that your build is
* running in the <em>Gradle Daemon</em> (which has this problem), setting the
* default to {@code true} as a consequence.
* @see CheckForbiddenApis#getDisableClassloadingCache
*/
public static final boolean DEFAULT_DISABLE_CLASSLOADING_CACHE = detectAndLogGradleDaemon();

/** Minimum Gradle version this plugin requires to run (v3.2). */
public static final GradleVersion MIN_GRADLE_VERSION = GradleVersion.version("3.2");

/** Java Package that contains the Gradle Daemon (needed to detect it on startup). */
private static final String GRADLE_DAEMON_PACKAGE = "org.gradle.launcher.daemon.";

/** Don't log info about Gradle Daemon multiple times. */
private static final AtomicBoolean logGradleDaemonWarning = new AtomicBoolean(true);
/** Resource with Groovy script that initializes the plugin. */
private static final String PLUGIN_INIT_SCRIPT = "plugin-init.groovy";

/** Compiled class instance of the plugin init script, an instance is executed per {@link #apply(Project)} */
private static final Class<? extends DelegatingScript> COMPILED_SCRIPT = loadScript();

private static final Class<? extends DelegatingScript> compiledScript;
static {
private static Class<? extends DelegatingScript> loadScript() {
final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName());
final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer);
configuration.setScriptBaseClass(DelegatingScript.class.getName());
Expand All @@ -66,7 +78,7 @@ public class ForbiddenApisPlugin implements Plugin<Project> {
if (scriptUrl == null) {
throw new RuntimeException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT);
}
compiledScript = AccessController.doPrivileged(new PrivilegedAction<Class<? extends DelegatingScript>>() {
return AccessController.doPrivileged(new PrivilegedAction<Class<? extends DelegatingScript>>() {
@Override
public Class<? extends DelegatingScript> run() {
try {
Expand Down Expand Up @@ -97,10 +109,10 @@ private static boolean isGradleDaemon() {
return false;
}

private static boolean detectAndLogGradleDaemon(Project project) {
private static boolean detectAndLogGradleDaemon() {
final boolean daemon = isGradleDaemon();
if (daemon && logGradleDaemonWarning.getAndSet(false)) {
project.getLogger().info("You are running forbidden-apis in the Gradle Daemon; disabling classloading cache by default to work around resource leak.");
if (daemon) {
LOG.info("You are running forbidden-apis in the Gradle Daemon; disabling classloading cache by default to work around resource leak.");
}
return daemon;
}
Expand All @@ -112,13 +124,12 @@ public void apply(Project project) {
}
final DelegatingScript script;
try {
script = compiledScript.newInstance();
script = COMPILED_SCRIPT.newInstance();
} catch (Exception e) {
throw new GradleException("Cannot instantiate Groovy script to apply forbiddenapis plugin.", e);
}
script.setDelegate(this);
script.setProperty("project", project);
script.setProperty("isGradleDaemon", detectAndLogGradleDaemon(project));
script.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ final boolean TASK_AVOIDANCE_AVAILABLE = GradleVersion.current() >= GradleVersio
project.plugins.apply(JavaBasePlugin.class);

// create Extension for defaults:
def extension = project.extensions.create(FORBIDDEN_APIS_EXTENSION_NAME, CheckForbiddenApisExtension.class);
extension.with{
signaturesFiles = project.files();
disableClassloadingCache |= isGradleDaemon;
}
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)
Expand Down
23 changes: 18 additions & 5 deletions src/test/gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,31 @@ apply plugin: 'de.thetaphi.forbiddenapis'

sourceCompatibility = forbiddenSourceCompatibility

sourceSets {
main {
compileClasspath = files(forbiddenClasspath.tokenize(File.pathSeparator))
java {
srcDirs = [new File(forbiddenRootDir, 'src/main/java')]
}
}
test {
compileClasspath = files(forbiddenTestClasspath.tokenize(File.pathSeparator))
java {
srcDirs = [new File(forbiddenRootDir, 'src/test/java')]
}
}
}

forbiddenApis {
bundledSignatures = [ 'jdk-unsafe', 'jdk-deprecated', 'jdk-non-portable' ]
bundledSignatures = [ 'jdk-unsafe', 'jdk-deprecated', 'jdk-non-portable', 'jdk-reflection' ]
signaturesFiles += files(new File(forbiddenRootDir, 'src/tools/signatures/mysignatures.txt'))
failOnUnsupportedJava = false
}

forbiddenApisMain {
classesDirs = files(new File(forbiddenRootDir, 'build/main'))
classpath = files(forbiddenClasspath.tokenize(File.pathSeparator))
bundledSignatures += 'jdk-system-out'
}

forbiddenApisTest {
classesDirs = files(new File(forbiddenRootDir, 'build/test'))
classpath = files(forbiddenTestClasspath.tokenize(File.pathSeparator))
// keep defaults from "forbiddenApis" above
}

0 comments on commit 3242ec4

Please sign in to comment.