diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/GroovyDslFileIndex.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/GroovyDslFileIndex.java index 5c05c4fb80cf..64b0803db1a0 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/GroovyDslFileIndex.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/GroovyDslFileIndex.java @@ -43,6 +43,7 @@ import com.intellij.psi.util.CachedValue; import com.intellij.psi.util.CachedValueProvider; import com.intellij.psi.util.CachedValuesManager; import com.intellij.psi.util.PsiModificationTracker; +import com.intellij.reference.SoftReference; import com.intellij.util.ExceptionUtil; import com.intellij.util.PathUtil; import com.intellij.util.concurrency.Semaphore; @@ -54,7 +55,6 @@ import com.intellij.util.io.KeyDescriptor; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.TestOnly; import org.jetbrains.plugins.groovy.annotator.GroovyFrameworkConfigNotification; import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; @@ -62,6 +62,7 @@ import javax.swing.event.HyperlinkEvent; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; @@ -74,7 +75,7 @@ import java.util.concurrent.TimeUnit; public class GroovyDslFileIndex extends ScalarIndexExtension { private static final Key> CACHED_EXECUTOR = Key.create("CachedGdslExecutor"); private static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.dsl.GroovyDslFileIndex"); - private static final FileAttribute ENABLED = new FileAttribute("ENABLED", 0); + private static final FileAttribute ENABLED = new FileAttribute("ENABLED", 0, false); @NonNls public static final ID NAME = ID.create("GroovyDslFileIndex"); @NonNls private static final String OUR_KEY = "ourKey"; @@ -88,8 +89,9 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { new ConcurrentMultiMap>>(); private static final ThreadPoolExecutor ourPool = new ThreadPoolExecutor(0, 1, 10, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactory() { + @NotNull @Override - public Thread newThread(Runnable r) { + public Thread newThread(@NotNull Runnable r) { return new Thread(r, "Groovy DSL File Index Executor"); } }); @@ -200,7 +202,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(error.length() * 2 + 1); stream.write(42); - stream.write(error.getBytes()); + stream.write(error.getBytes(Charset.defaultCharset())); ENABLED.writeAttributeBytes(vfile, stream.toByteArray()); } catch (IOException e1) { @@ -266,10 +268,16 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { return false; } - private static volatile List> ourStandardScripts; + private static volatile SoftReference>> ourStandardScripts; + + @Nullable + private static List> derefStandardScripts() { + SoftReference>> ref = ourStandardScripts; + return ref == null ? null : ref.get(); + } private static List> getStandardScripts() { - List> result = ourStandardScripts; + List> result = derefStandardScripts(); if (result != null) { return result; } @@ -280,7 +288,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { @SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod") @Override public void run() { - if (ourStandardScripts != null) { + if (derefStandardScripts() != null) { return; } @@ -298,21 +306,24 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { List> executors = new ArrayList>(); for (File file : scriptFolders) { if (file.exists()) { - for (File child : file.listFiles()) { - final String fileName = child.getName(); - if (fileName.endsWith(".gdsl")) { - try { - final String text = new String(FileUtil.loadFileText(child)); - executors.add(Pair.create(child, new GroovyDslExecutor(text, fileName))); - } - catch (IOException e) { - LOG.error(e); + File[] children = file.listFiles(); + if (children != null) { + for (File child : children) { + final String fileName = child.getName(); + if (fileName.endsWith(".gdsl")) { + try { + final String text = new String(FileUtil.loadFileText(child)); + executors.add(Pair.create(child, new GroovyDslExecutor(text, fileName))); + } + catch (IOException e) { + LOG.error(e); + } } } } } } - ourStandardScripts = executors; + ourStandardScripts = new SoftReference>>(executors); } catch (OutOfMemoryError e) { stopGdsl = true; @@ -328,13 +339,13 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { } }); - while (ourStandardScripts == null && !stopGdsl && !semaphore.waitFor(20)) { + while (derefStandardScripts() == null && !stopGdsl && !semaphore.waitFor(20)) { ProgressManager.checkCanceled(); } if (stopGdsl) { return Collections.emptyList(); } - return ourStandardScripts; + return derefStandardScripts(); } private static final Key>> SCRIPTS_CACHE = Key.create("GdslScriptCache"); @@ -461,7 +472,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { final boolean isNewRequest = !filesInProcessing.containsKey(fileUrl); filesInProcessing.putValue(fileUrl, queue); if (isNewRequest) { - ourPool.execute(parseScript); //todo bring back multithreading when Groovy team fixes http://jira.codehaus.org/browse/GROOVY-4292 + ourPool.execute(parseScript); //todo bring back multi-threading when Groovy team fixes http://jira.codehaus.org/browse/GROOVY-4292 //ApplicationManager.getApplication().executeOnPooledThread(parseScript); } } @@ -490,10 +501,12 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { } invokeDslErrorPopup(e, project, vfile); + //noinspection InstanceofCatchParameter if (e instanceof OutOfMemoryError) { stopGdsl = true; throw (Error)e; } + //noinspection InstanceofCatchParameter if (e instanceof NoClassDefFoundError) { stopGdsl = true; throw (NoClassDefFoundError) e; @@ -528,8 +541,4 @@ public class GroovyDslFileIndex extends ScalarIndexExtension { }).notify(project); } - @TestOnly - public static void clearAllInTests() { - ourStandardScripts = null; - } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy index 9b87f539a4f5..8d23d45fb79e 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/LightGroovyTestCase.groovy @@ -24,11 +24,6 @@ import org.jetbrains.annotations.NotNull * @author peter */ public abstract class LightGroovyTestCase extends LightCodeInsightFixtureTestCase { - @Override - protected void tearDown() throws Exception { - super.tearDown() - org.jetbrains.plugins.groovy.dsl.GroovyDslFileIndex.clearAllInTests(); - } @Override @NotNull