From 76cb06579ef56607b1dbd957d05a857b367fd88a Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 18 Nov 2010 13:13:50 +0300 Subject: [PATCH 1/2] skip binary files --- .../refactoring/move/FileReferenceContextUtil.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/platform/lang-impl/src/com/intellij/refactoring/move/FileReferenceContextUtil.java b/platform/lang-impl/src/com/intellij/refactoring/move/FileReferenceContextUtil.java index 315867943524..94b6e0668c52 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/move/FileReferenceContextUtil.java +++ b/platform/lang-impl/src/com/intellij/refactoring/move/FileReferenceContextUtil.java @@ -36,6 +36,7 @@ public class FileReferenceContextUtil { public static Map encodeFileReferences(PsiElement element) { final Map map = new HashMap(); + if (isBinary(element)) return map; element.accept(new PsiRecursiveElementWalkingVisitor(true) { @Override public void visitElement(PsiElement element) { final PsiReference[] refs = element.getReferences(); @@ -59,7 +60,14 @@ public class FileReferenceContextUtil { return map; } + private static boolean isBinary(PsiElement element) { + final PsiFile containingFile = element.getContainingFile(); + if (containingFile == null || containingFile.getFileType().isBinary()) return true; + return false; + } + public static void decodeFileReferences(PsiElement element) { + if (isBinary(element)) return; element.accept(new PsiRecursiveElementVisitor(true) { @Override public void visitElement(PsiElement element) { final PsiFileSystemItem item = element.getCopyableUserData(REF_FILE_SYSTEM_ITEM_KEY); @@ -73,6 +81,7 @@ public class FileReferenceContextUtil { } public static void decodeFileReferences(PsiElement element, final Map map, final TextRange range) { + if (isBinary(element)) return; element.accept(new PsiRecursiveElementVisitor(true) { @Override public void visitElement(PsiElement element) { if (!range.intersects(element.getTextRange())) return; From c727d2a20f566cad727ebd6c325784cfc3ce5798 Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 18 Nov 2010 14:00:22 +0300 Subject: [PATCH 2/2] do not leak project --- .../ex/EntryPointsManagerImpl.java | 32 ++++++++--------- .../reference/RefJavaManagerImpl.java | 3 +- .../EntryPointsConverterTest.java | 34 ++++--------------- .../codeInspection/ex/EntryPointsManager.java | 3 +- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/ex/EntryPointsManagerImpl.java b/java/java-impl/src/com/intellij/codeInspection/ex/EntryPointsManagerImpl.java index ed5d741b40ae..4bcff1c8454b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/ex/EntryPointsManagerImpl.java +++ b/java/java-impl/src/com/intellij/codeInspection/ex/EntryPointsManagerImpl.java @@ -27,7 +27,6 @@ package com.intellij.codeInspection.ex; import com.intellij.ExtensionPoints; import com.intellij.codeInspection.reference.*; import com.intellij.codeInspection.util.SpecialAnnotationsUtil; -import com.intellij.openapi.Disposable; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.ServiceManager; @@ -56,7 +55,7 @@ import java.util.*; name = "EntryPointsManager", storages = {@Storage(id = "default", file = "$PROJECT_FILE$")} ) -public class EntryPointsManagerImpl implements PersistentStateComponent, EntryPointsManager, Disposable { +public class EntryPointsManagerImpl implements PersistentStateComponent, EntryPointsManager { @NonNls private static final String[] STANDARD_ANNOS = { "javax.ws.rs.*", "javax.annotation.Resource", @@ -99,9 +98,9 @@ public class EntryPointsManagerImpl implements PersistentStateComponent myTemporaryEntryPoints = new HashSet(); myPersistentEntryPoints = new LinkedHashMap(); // To keep the order between readExternal to writeExternal - + Disposer.register(project, this); final ExtensionPoint point = Extensions.getRootArea().getExtensionPoint(ExtensionPoints.DEAD_CODE_TOOL); - myExtensionPointListener = new ExtensionPointListener() { + point.addExtensionPointListener(new ExtensionPointListener() { @Override public void extensionAdded(@NotNull EntryPoint extension, @Nullable PluginDescriptor pluginDescriptor) { extensionRemoved(extension, pluginDescriptor); @@ -120,8 +119,7 @@ public class EntryPointsManagerImpl implements PersistentStateComponent }); } } - }; - point.addExtensionPointListener(myExtensionPointListener); + }, this); } public static EntryPointsManagerImpl getInstance(Project project) { @@ -133,7 +131,7 @@ public class EntryPointsManagerImpl implements PersistentStateComponent Element entryPointsElement = element.getChild("entry_points"); final String version = entryPointsElement.getAttributeValue(VERSION_ATTR); if (!Comparing.strEqual(version, VERSION)) { - convert(entryPointsElement); + convert(entryPointsElement, myPersistentEntryPoints); } else { List content = entryPointsElement.getChildren(); @@ -146,7 +144,7 @@ public class EntryPointsManagerImpl implements PersistentStateComponent } } try { - DefaultJDOMExternalizer.readExternal(this, element); + ADDITIONAL_ANNOTATIONS.readExternal(element); } catch (InvalidDataException ignored) { } @@ -155,23 +153,25 @@ public class EntryPointsManagerImpl implements PersistentStateComponent @SuppressWarnings({"HardCodedStringLiteral"}) public Element getState() { Element element = new Element("state"); - writeExternal(element); + writeExternal(element, myPersistentEntryPoints, ADDITIONAL_ANNOTATIONS); return element; } @SuppressWarnings({"HardCodedStringLiteral"}) - public void writeExternal(final Element element) { + public static void writeExternal(final Element element, + final Map persistentEntryPoints, + final JDOMExternalizableStringList additional_annotations) { Element entryPointsElement = new Element("entry_points"); entryPointsElement.setAttribute(VERSION_ATTR, VERSION); - for (SmartRefElementPointer entryPoint : myPersistentEntryPoints.values()) { + for (SmartRefElementPointer entryPoint : persistentEntryPoints.values()) { assert entryPoint.isPersistent(); entryPoint.writeExternal(entryPointsElement); } element.addContent(entryPointsElement); - if (!ADDITIONAL_ANNOTATIONS.isEmpty()) { + if (!additional_annotations.isEmpty()) { try { - DefaultJDOMExternalizer.writeExternal(this, element); + additional_annotations.writeExternal(element); } catch (WriteExternalException ignored) { } @@ -302,8 +302,6 @@ public class EntryPointsManagerImpl implements PersistentStateComponent } public void dispose() { - final ExtensionPoint extensionPoint = Extensions.getRootArea().getExtensionPoint(ExtensionPoints.DEAD_CODE_TOOL); - extensionPoint.removeExtensionPointListener(myExtensionPointListener); cleanup(); } @@ -362,7 +360,7 @@ public class EntryPointsManagerImpl implements PersistentStateComponent myPersistentEntryPoints.putAll(manager.myPersistentEntryPoints); } - public void convert(Element element) { + public static void convert(Element element, final Map persistentEntryPoints) { List content = element.getChildren(); for (final Object aContent : content) { Element entryElement = (Element)aContent; @@ -401,7 +399,7 @@ public class EntryPointsManagerImpl implements PersistentStateComponent } } SmartRefElementPointerImpl entryPoint = new SmartRefElementPointerImpl(type, fqName); - myPersistentEntryPoints.put(entryPoint.getFQName(), entryPoint); + persistentEntryPoints.put(entryPoint.getFQName(), entryPoint); } } } diff --git a/java/java-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java b/java/java-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java index e043ac150d5f..d50ae3b28d77 100644 --- a/java/java-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java +++ b/java/java-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java @@ -27,6 +27,7 @@ import com.intellij.codeInspection.ex.EntryPointsManagerImpl; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.Disposer; import com.intellij.psi.*; import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.psi.javadoc.PsiDocTag; @@ -147,7 +148,7 @@ public class RefJavaManagerImpl extends RefJavaManager { public void cleanup() { if (myEntryPointsManager != null) { - myEntryPointsManager.cleanup(); + Disposer.dispose(myEntryPointsManager); myEntryPointsManager = null; } myPackages = null; diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/EntryPointsConverterTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/EntryPointsConverterTest.java index f7355a6dfb0a..104af74d92a1 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/EntryPointsConverterTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/EntryPointsConverterTest.java @@ -1,36 +1,14 @@ package com.intellij.codeInspection; -import com.intellij.ExtensionPoints; import com.intellij.codeInspection.ex.EntryPointsManagerImpl; -import com.intellij.codeInspection.reference.EntryPoint; -import com.intellij.openapi.extensions.Extensions; +import com.intellij.codeInspection.reference.SmartRefElementPointer; +import com.intellij.openapi.util.JDOMExternalizableStringList; import com.intellij.openapi.util.JDOMUtil; +import com.intellij.util.containers.HashMap; import junit.framework.TestCase; import org.jdom.Element; public class EntryPointsConverterTest extends TestCase { - private boolean myUnregisterExtensionPoint = false; - - @Override - protected void setUp() throws Exception { - super.setUp(); - try { - Extensions.getExtensions(ExtensionPoints.DEAD_CODE_TOOL, null); - } - catch (IllegalArgumentException e) { - myUnregisterExtensionPoint = true; - Extensions.getRootArea().registerExtensionPoint(ExtensionPoints.DEAD_CODE_TOOL, EntryPoint.class.getName()); - } - } - - @Override - protected void tearDown() throws Exception { - if (myUnregisterExtensionPoint) { - Extensions.getRootArea().unregisterExtensionPoint(ExtensionPoints.DEAD_CODE_TOOL); - } - super.tearDown(); - } - public void testMethodConverter1() throws Exception { doTest("method", "String java.lang.String.replace(char oldChar, char newChar)", "java.lang.String String replace(char oldChar, char newChar)"); } @@ -50,11 +28,11 @@ public class EntryPointsConverterTest extends TestCase { private static void doTest(String type, String fqName, String expectedFQName) throws Exception { final Element entryPoints = setUpEntryPoint(type, fqName); - final EntryPointsManagerImpl manager = new EntryPointsManagerImpl(null); - manager.convert(entryPoints); + final HashMap persistentEntryPoints = new HashMap(); + EntryPointsManagerImpl.convert(entryPoints, persistentEntryPoints); final Element testElement = new Element("comp"); - manager.writeExternal(testElement); + EntryPointsManagerImpl.writeExternal(testElement, persistentEntryPoints, new JDOMExternalizableStringList()); final Element expectedEntryPoints = setUpEntryPoint(type, expectedFQName); expectedEntryPoints.setAttribute("version", "2.0"); diff --git a/java/openapi/src/com/intellij/codeInspection/ex/EntryPointsManager.java b/java/openapi/src/com/intellij/codeInspection/ex/EntryPointsManager.java index d9d61204e18c..3b90822d8fca 100644 --- a/java/openapi/src/com/intellij/codeInspection/ex/EntryPointsManager.java +++ b/java/openapi/src/com/intellij/codeInspection/ex/EntryPointsManager.java @@ -22,8 +22,9 @@ package com.intellij.codeInspection.ex; import com.intellij.codeInspection.reference.RefElement; import com.intellij.codeInspection.reference.RefManager; +import com.intellij.openapi.Disposable; -public interface EntryPointsManager { +public interface EntryPointsManager extends Disposable { void resolveEntryPoints(RefManager manager); void addEntryPoint(RefElement newEntryPoint, boolean isPersistent);