From 1fc0a16be3adc1000b3ca83592a8d28d7d8890cf Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Tue, 31 Jan 2017 15:08:48 +0100 Subject: [PATCH 1/3] Compile and run the script only once. This should fix issue #116 --- .../gradle/ForbiddenApisPlugin.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index 4e4873f3..6bf03d70 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -19,6 +19,7 @@ import groovy.lang.Binding; import groovy.lang.GroovyCodeSource; import groovy.lang.GroovyShell; +import groovy.lang.Script; import groovy.util.DelegatingScript; import java.net.URL; @@ -45,14 +46,14 @@ public class ForbiddenApisPlugin implements Plugin { /** Name of the extension to define defaults for all tasks of this module. */ public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis"; - @Override - public void apply(final Project project) { + private final Script script; + + public ForbiddenApisPlugin() { final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName()); final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer); configuration.setScriptBaseClass(DelegatingScript.class.getName()); configuration.setSourceEncoding("UTF-8"); - final Binding binding = new Binding(Collections.singletonMap("project", project)); - final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), binding, configuration); + final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), new Binding(), configuration); final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT); if (scriptUrl == null) { throw new PluginInstantiationException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT); @@ -60,7 +61,15 @@ public void apply(final Project project) { final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl); final DelegatingScript script = (DelegatingScript) shell.parse(csrc); script.setDelegate(this); + this.script = script; + } + + // synchronized because we change the property "project" in the binding + @Override + public synchronized void apply(Project project) { + script.setProperty("project", project); script.run(); + script.setProperty("project", null); } } From 8caa319785d5593c99623dc2e4d577ffb4d45ec0 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Tue, 31 Jan 2017 15:20:20 +0100 Subject: [PATCH 2/3] Remove obsolete import --- .../de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index 6bf03d70..59083c3c 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -23,7 +23,6 @@ import groovy.util.DelegatingScript; import java.net.URL; -import java.util.Collections; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; From e43b6a9f6623d6c0e481c616a3159fef8c28ebf1 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Tue, 31 Jan 2017 15:50:10 +0100 Subject: [PATCH 3/3] Move script compilation to static initializer --- .../gradle/ForbiddenApisPlugin.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index 59083c3c..f14be05e 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -19,7 +19,6 @@ import groovy.lang.Binding; import groovy.lang.GroovyCodeSource; import groovy.lang.GroovyShell; -import groovy.lang.Script; import groovy.util.DelegatingScript; import java.net.URL; @@ -28,7 +27,6 @@ import org.codehaus.groovy.control.customizers.ImportCustomizer; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.plugins.PluginInstantiationException; /** * Forbiddenapis Gradle Plugin (requires at least Gradle 2.3) @@ -45,9 +43,8 @@ public class ForbiddenApisPlugin implements Plugin { /** Name of the extension to define defaults for all tasks of this module. */ public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis"; - private final Script script; - - public ForbiddenApisPlugin() { + private static final DelegatingScript compiledScript; + static { final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName()); final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer); configuration.setScriptBaseClass(DelegatingScript.class.getName()); @@ -55,20 +52,20 @@ public ForbiddenApisPlugin() { final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), new Binding(), configuration); final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT); if (scriptUrl == null) { - throw new PluginInstantiationException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT); + throw new RuntimeException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT); } final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl); - final DelegatingScript script = (DelegatingScript) shell.parse(csrc); - script.setDelegate(this); - this.script = script; + compiledScript = (DelegatingScript) shell.parse(csrc); } - // synchronized because we change the property "project" in the binding @Override - public synchronized void apply(Project project) { - script.setProperty("project", project); - script.run(); - script.setProperty("project", null); + public void apply(Project project) { + synchronized(compiledScript) { + compiledScript.setDelegate(this); + compiledScript.setProperty("project", project); + compiledScript.run(); + compiledScript.setProperty("project", null); // free resources + } } }