From 1b1d58ce33689dbf7c554079f2360e93503f7cc1 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 26 Sep 2014 18:00:30 +0200 Subject: [PATCH] java: ensure PSI/document compatibility for decompiled .class files --- .../impl/compiled/ClassFileDecompiler.java | 13 ------ .../psi/impl/compiled/ClsFileImpl.java | 12 +++++- .../psi/cls/mirror/pkg/ReuseTestV1.class | Bin 0 -> 213 bytes .../psi/cls/mirror/pkg/ReuseTestV2.class | Bin 0 -> 226 bytes .../intellij/psi/ClsMirrorBuildingTest.java | 39 +++++++++++++++--- 5 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV1.class create mode 100644 java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV2.class diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClassFileDecompiler.java b/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClassFileDecompiler.java index 31c35c617dcc..25baddb42d11 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClassFileDecompiler.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClassFileDecompiler.java @@ -17,8 +17,6 @@ package com.intellij.psi.impl.compiled; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileTypes.BinaryFileDecompiler; import com.intellij.openapi.project.DefaultProjectFactory; import com.intellij.openapi.vfs.VirtualFile; @@ -54,17 +52,6 @@ public class ClassFileDecompiler implements BinaryFileDecompiler { if (result != null) return result; } - return decompileText(file); - } - - @NotNull - public static CharSequence decompileText(@NotNull VirtualFile file) { - Document document = FileDocumentManager.getInstance().getCachedDocument(file); - if (document != null) { - return document.getImmutableCharSequence(); - } - - ClassFileDecompilers.Decompiler decompiler = ClassFileDecompilers.find(file); if (decompiler instanceof ClassFileDecompilers.Light) { try { return ((ClassFileDecompilers.Light)decompiler).getText(file); diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClsFileImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClsFileImpl.java index 685bffafcf89..a64de9938bd0 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClsFileImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/compiled/ClsFileImpl.java @@ -24,8 +24,10 @@ import com.intellij.lang.FileASTNode; import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.extensions.PluginId; +import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.DefaultProjectFactory; @@ -33,6 +35,7 @@ import com.intellij.openapi.roots.FileIndexFacade; import com.intellij.openapi.ui.Queryable; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Computable; +import com.intellij.openapi.util.Key; import com.intellij.openapi.util.ModificationTracker; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.java.LanguageLevel; @@ -77,6 +80,8 @@ public class ClsFileImpl extends ClsRepositoryPsiElement implements PsiJavaFile, PsiFileWithStubSupport, PsiFileEx, Queryable, PsiClassOwnerEx, PsiCompiledFile { private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsFileImpl"); + private static final Key CLS_DOCUMENT_LINK_KEY = Key.create("cls.document.link"); + /** NOTE: you absolutely MUST NOT hold PsiLock under the mirror lock */ private final Object myMirrorLock = new Object(); private final Object myStubLock = new Object(); @@ -342,7 +347,10 @@ public class ClsFileImpl extends ClsRepositoryPsiElement } private TreeElement trySetMirror(VirtualFile file, String fileName, boolean usePlugin) { - CharSequence mirrorText = ClassFileDecompiler.decompileText(file); + final Document document = FileDocumentManager.getInstance().getDocument(file); + assert document != null : file.getUrl(); + + CharSequence mirrorText = document.getImmutableCharSequence(); PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject()); PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false); mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel()); @@ -353,6 +361,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement @Override public void run() { setMirror(mirrorTreeElement); + putUserData(CLS_DOCUMENT_LINK_KEY, document); } }); } @@ -515,6 +524,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement ClsPackageStatementImpl packageStatement = new ClsPackageStatementImpl(this); synchronized (myMirrorLock) { + putUserData(CLS_DOCUMENT_LINK_KEY, null); myMirrorFileElement = null; myPackageStatement = packageStatement; } diff --git a/java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV1.class b/java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV1.class new file mode 100644 index 0000000000000000000000000000000000000000..3dcddcb1c9b0862837f91556e75f0b57c5d221e8 GIT binary patch literal 213 zcmX|*%?g5W5X5KwEYs`-f{xW8iLTKl2!bdm>8|!qG}G{-_v#dM=mC1DX!X!#W`8p* zyPxmt4PXY_fDBWIrK3xb&Qk)d3EDhLqjEuzhod7wS!O3eus2aEw$;@ax$F4}C&nSG z@<6Pj8oGN?6~YxoIf=cyXF`Kcux^*o`8R}K-8qSuhR)89MNnb@iJt)YJ`||UgnF5i Z7E_;{*wyh9^l0iftJZ`vtHC6p^8*>@CF1}9 literal 0 HcmV?d00001 diff --git a/java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV2.class b/java/java-tests/testData/psi/cls/mirror/pkg/ReuseTestV2.class new file mode 100644 index 0000000000000000000000000000000000000000..bc0344f51fe7adc95278964880e38dd18eed6973 GIT binary patch literal 226 zcmX|*&kn(05X5Kumn!WGNE~s{;3_T>i9|?f+}rvzttz4QUQQAR58$E1em%I%{AM=U z-Ou;+1~7(eK}FL<%Y;Kv4nqPhB#a2gB*~LvN>IDK9YLF&_k!RolU%IIQz))}5N6z1 z+jDu1#5|FqwGrh__~KR!(%>H0Fi|5kFUQ#XPYAA@d0CLh-a1T0RG