4141
4242public class GroovyScriptSandbox extends GroovySandbox {
4343
44+ /**
45+ * Changing this number will force the cache to be deleted and every script has to be recompiled.
46+ * Useful when changes to the compilation process were made.
47+ */
48+ public static final int CACHE_VERSION = 2 ;
49+ /**
50+ * Setting this to false will cause compiled classes to never be cached.
51+ * As a side effect some compilation behaviour might change. Can be useful for debugging.
52+ */
53+ public static final boolean ENABLE_CACHE = true ;
54+ /**
55+ * Setting this to true will cause the cache to be deleted before each script run.
56+ * Useful for debugging.
57+ */
58+ public static final boolean DELETE_CACHE_ON_RUN = false ;
59+
4460 private final File cacheRoot ;
4561 private final File scriptRoot ;
4662 private final ImportCustomizer importCustomizer = new ImportCustomizer ();
4763 private final Map <List <StackTraceElement >, AtomicInteger > storedExceptions ;
48- /**
49- * Version of the cache. Used to auto delete current cache if changes to the cache system were made.
50- * 1: Default
51- */
52- private int cacheVersion = 1 ;
5364 private final Map <String , CompiledScript > index = new Object2ObjectOpenHashMap <>();
5465
55- public static final boolean ENABLE_CACHE = true ;
56-
5766 private LoadStage currentLoadStage ;
5867
5968 public GroovyScriptSandbox (File scriptRoot , File cacheRoot ) throws MalformedURLException {
@@ -105,9 +114,9 @@ private void readIndex() {
105114 JsonElement jsonElement = JsonHelper .loadJson (new File (this .cacheRoot , "_index.json" ));
106115 if (jsonElement == null || !jsonElement .isJsonObject ()) return ;
107116 JsonObject json = jsonElement .getAsJsonObject ();
108- this . cacheVersion = json .get ("version" ).getAsInt ();
109- if (this . cacheVersion != 1 ) {
110- // only version 1 allowed currently
117+ int cacheVersion = json .get ("version" ).getAsInt ();
118+ if (cacheVersion != CACHE_VERSION ) {
119+ // cache version changed -> force delete cache
111120 deleteScriptCache ();
112121 return ;
113122 }
@@ -125,7 +134,7 @@ private void writeIndex() {
125134 if (!ENABLE_CACHE ) return ;
126135 JsonObject json = new JsonObject ();
127136 json .addProperty ("!DANGER!" , "DO NOT EDIT THIS FILE!!!" );
128- json .addProperty ("version" , this . cacheVersion );
137+ json .addProperty ("version" , CACHE_VERSION );
129138 JsonArray index = new JsonArray ();
130139 json .add ("index" , index );
131140 for (Map .Entry <String , CompiledScript > entry : this .index .entrySet ()) {
@@ -241,9 +250,8 @@ public void onCompileClass(SourceUnit su, String path, Class<?> clazz, byte[] co
241250 }
242251
243252 /**
244- * Called via mixin when a script class needs to be recompiled. This happens when a script was loaded because
245- * another script depends on it. Groovy will then try to compile the script again. If we already compiled the class
246- * we just stop the compilation process.
253+ * Called via mixin when a script class needs to be recompiled. This happens when a script was loaded because another script depends on
254+ * it. Groovy will then try to compile the script again. If we already compiled the class we just stop the compilation process.
247255 */
248256 @ ApiStatus .Internal
249257 public Class <?> onRecompileClass (GroovyClassLoader classLoader , URL source , String className ) {
@@ -321,6 +329,7 @@ protected void initEngine(GroovyScriptEngine engine, CompilerConfiguration confi
321329
322330 @ Override
323331 protected void preRun () {
332+ if (DELETE_CACHE_ON_RUN ) deleteScriptCache ();
324333 // first clear all added events
325334 GroovyEventManager .INSTANCE .reset ();
326335 if (this .currentLoadStage .isReloadable () && !ReloadableRegistryManager .isFirstLoad ()) {
0 commit comments