Switch to new decompiler API for IDEA

Patch from Roman Shevchenko adapted
This commit is contained in:
Pavel V. Talanov
2014-02-19 17:19:31 +04:00
parent d31c2884aa
commit b052cb8aa9
8 changed files with 196 additions and 183 deletions
+1 -3
View File
@@ -326,13 +326,11 @@
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetAnnotationsIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetTopLevelShortObjectNameIndex"/>
<clsStubBuilderFactory implementation="org.jetbrains.jet.plugin.stubindex.builder.EmptyPackageFragmentClsStubBuilderFactory"/>
<psi.classFileDecompiler implementation="org.jetbrains.jet.plugin.libraries.JetClassFileDecompiler"/>
<fileBasedIndex implementation="org.jetbrains.jet.plugin.versions.KotlinAbiVersionIndex"/>
<fileBasedIndex implementation="org.jetbrains.jet.plugin.vfilefinder.KotlinClassFileIndex"/>
<contentBasedClassFileProcessor implementation="org.jetbrains.jet.plugin.libraries.JetContentBasedFileSubstitutor"/>
<editorNotificationProvider implementation="org.jetbrains.jet.plugin.quickfix.IncorrectSourceRootNameNotification"/>
<editorNotificationProvider implementation="org.jetbrains.jet.plugin.versions.UnsupportedAbiVersionNotificationPanelProvider"/>
<editorNotificationProvider implementation="org.jetbrains.jet.plugin.highlighter.ErrorDuringFileAnalyzeNotificationProvider"/>
@@ -55,7 +55,7 @@ public final class DecompiledNavigationUtils {
DeclarationDescriptor effectiveReferencedDescriptor = getEffectiveReferencedDescriptor(referencedDescriptor);
VirtualFile virtualFile = findVirtualFileContainingDescriptor(project, effectiveReferencedDescriptor);
if (virtualFile == null || !DecompiledUtils.isKotlinCompiledFile(project, virtualFile)) return null;
if (virtualFile == null || !DecompiledUtils.isKotlinCompiledFile(virtualFile)) return null;
JetDecompiledData data = JetDecompiledData.getDecompiledData(virtualFile, project);
JetDeclaration jetDeclaration = data.getDeclarationForDescriptor(effectiveReferencedDescriptor);
@@ -18,24 +18,57 @@ package org.jetbrains.jet.plugin.libraries;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClass;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.storage.LockBasedStorageManager;
public final class DecompiledUtils {
private static final String PACKAGE_FRAGMENT_SIGNATURE = PackageClassUtils.PACKAGE_CLASS_NAME_SUFFIX + "-";
public static boolean isKotlinCompiledFile(@NotNull Project project, @NotNull VirtualFile file) {
public static boolean isKotlinCompiledFile(@NotNull VirtualFile file) {
if (!StdFileTypes.CLASS.getDefaultExtension().equals(file.getExtension())) {
return false;
}
//TODO: check index
KotlinJvmBinaryClass kotlinClass = VirtualFileFinder.SERVICE.getInstance(project).createKotlinClass(file);
KotlinClassHeader header = kotlinClass.getClassHeader();
return isKotlinInternalClass(file) || checkFile(file);
}
public static boolean isKotlinInternalClass(@NotNull VirtualFile file) {
// FIXME: not sure if this is a good heuristic
String name = file.getName();
int pos = name.indexOf('$');
if (pos > 0) {
name = name.substring(0, pos) + ".class";
VirtualFile supposedHost = file.getParent().findChild(name);
if (supposedHost != null) {
return checkFile(supposedHost);
}
}
if (name.contains(PACKAGE_FRAGMENT_SIGNATURE)) {
KotlinClassHeader header = new VirtualFileKotlinClass(LockBasedStorageManager.NO_LOCKS, file).getClassHeader();
if (header != null && header.getKind() == KotlinClassHeader.Kind.PACKAGE_FRAGMENT) {
return true;
}
}
return false;
}
private static boolean checkFile(@NotNull VirtualFile file) {
KotlinClassHeader header = new VirtualFileKotlinClass(LockBasedStorageManager.NO_LOCKS, file).getClassHeader();
return header != null && header.getAnnotationData() != null;
}
public static CharSequence decompile(@NotNull VirtualFile file) {
Project project = ProjectManager.getInstance().getOpenProjects()[0]; // FIXME: get rid of project usage here
return JetDecompiledData.getDecompiledData(file, project).getFileText();
}
private DecompiledUtils() {
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.libraries;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiManager;
import com.intellij.psi.compiled.ClassFileDecompilers;
import com.intellij.psi.compiled.ClsStubBuilder;
import org.jetbrains.annotations.NotNull;
public class JetClassFileDecompiler extends ClassFileDecompilers.Full {
private final ClsStubBuilder stubBuilder = new JetClsStubBuilder();
@Override
public boolean accepts(@NotNull VirtualFile file) {
return DecompiledUtils.isKotlinCompiledFile(file);
}
@NotNull
@Override
public ClsStubBuilder getStubBuilder() {
return stubBuilder;
}
@NotNull
@Override
public FileViewProvider createFileViewProvider(@NotNull VirtualFile file, @NotNull PsiManager manager, boolean physical) {
return new JetClassFileViewProvider(manager, file, physical, DecompiledUtils.isKotlinInternalClass(file));
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.libraries;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.compiled.ClsFileImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetLanguage;
public class JetClassFileViewProvider extends SingleRootFileViewProvider {
private final boolean isInternal;
public JetClassFileViewProvider(@NotNull PsiManager manager, @NotNull VirtualFile file, boolean physical, boolean internal) {
super(manager, file, physical, JetLanguage.INSTANCE);
isInternal = internal;
}
@NotNull
@Override
public CharSequence getContents() {
return isInternal ? "" : DecompiledUtils.decompile(getVirtualFile());
}
@Nullable
@Override
protected PsiFile createFile(@NotNull Project project, @NotNull VirtualFile file, @NotNull FileType fileType) {
return isInternal ? null : new JetDecompiledFile(this);
}
@NotNull
@Override
public SingleRootFileViewProvider createCopy(@NotNull VirtualFile copy) {
return new JetClassFileViewProvider(getManager(), copy, false, isInternal);
}
private static class JetDecompiledFile extends ClsFileImpl {
public JetDecompiledFile(FileViewProvider viewProvider) {
super(viewProvider);
}
@Override
public PsiFile getDecompiledPsiFile() {
return JetDecompiledData.getDecompiledData(getVirtualFile(), getProject()).getFile();
}
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.libraries;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.compiled.ClsStubBuilder;
import com.intellij.psi.impl.compiled.ClassFileStubBuilder;
import com.intellij.psi.impl.compiled.ClsFileImpl;
import com.intellij.psi.stubs.PsiFileStub;
import com.intellij.util.cls.ClsFormatException;
import com.intellij.util.indexing.FileContent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JetClsStubBuilder extends ClsStubBuilder {
@Override
public int getStubVersion() {
return ClassFileStubBuilder.STUB_VERSION + 1;
}
@Nullable
@Override
public PsiFileStub<?> buildFileStub(@NotNull FileContent content) throws ClsFormatException {
VirtualFile file = content.getFile();
if (DecompiledUtils.isKotlinInternalClass(file)) {
return null;
}
return ClsFileImpl.buildFileStub(file, content.getContent());
}
}
@@ -1,115 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.libraries;
import com.google.common.collect.Maps;
import com.intellij.AppTopics;
import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.openapi.fileTypes.ContentBasedClassFileProcessor;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.PsiDocumentManagerBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.highlighter.JetHighlighter;
import java.util.concurrent.ConcurrentMap;
public final class JetContentBasedFileSubstitutor implements ContentBasedClassFileProcessor {
private static final JetContentBasedFileSubstitutor instance = new JetContentBasedFileSubstitutor();
private static final ConcurrentMap<VirtualFile, JetFile> deferredDocumentBinding = Maps.newConcurrentMap();
public static JetContentBasedFileSubstitutor getInstance() {
return instance;
}
private JetContentBasedFileSubstitutor() {
ApplicationManager.getApplication().getMessageBus().connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
@Override
public void fileContentLoaded(@NotNull VirtualFile loadedFile, @NotNull Document document) {
processDeferredBindings(loadedFile, document);
}
@Override
public void fileContentReloaded(VirtualFile loadedFile, @NotNull Document document) {
processDeferredBindings(loadedFile, document);
}
private void processDeferredBindings(VirtualFile loadedFile, @NotNull Document document) {
JetFile file = deferredDocumentBinding.remove(loadedFile);
if (file != null) {
PsiDocumentManagerBase.cachePsi(document, file);
}
}
});
}
@Override
public boolean isApplicable(@Nullable final Project project, @NotNull final VirtualFile file) {
if (project == null) {
return false;
}
if (DumbService.isDumb(project)) {
DumbService.getInstance(project).runWhenSmart(new Runnable() {
@Override
public void run() {
if (DecompiledUtils.isKotlinCompiledFile(project, file)) {
FileDocumentManager docManager = FileDocumentManager.getInstance();
docManager.getDocument(file); // force getting document because it can be collected
docManager.reloadFiles(file);
}
}
});
return false;
}
return DecompiledUtils.isKotlinCompiledFile(project, file);
}
@NotNull
@Override
public String obtainFileText(Project project, VirtualFile file) {
if (file != null && DecompiledUtils.isKotlinCompiledFile(project, file)) {
JetDecompiledData data = JetDecompiledData.getDecompiledData(file, project);
deferredDocumentBinding.put(file, data.getFile());
return data.getFileText();
}
return "";
}
@Override
public Language obtainLanguageForFile(VirtualFile file) {
return null;
}
@NotNull
@Override
public SyntaxHighlighter createHighlighter(Project project, VirtualFile vFile) {
return new JetHighlighter();
}
}
@@ -1,58 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.stubindex.builder;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.impl.compiled.ClsStubBuilderFactory;
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
import com.intellij.psi.stubs.PsiFileStub;
import com.intellij.util.cls.ClsFormatException;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClass;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.storage.LockBasedStorageManager;
/**
* This class is needed to build an empty PSI stub for compiled package fragment classes. This results in these classes not showing up
* in completion, go-to-class, etc.
*/
public class EmptyPackageFragmentClsStubBuilderFactory extends ClsStubBuilderFactory<PsiJavaFile> {
@Nullable
@Override
public PsiFileStub<PsiJavaFile> buildFileStub(VirtualFile file, byte[] bytes) throws ClsFormatException {
return new PsiJavaFileStubImpl(null, true);
}
@Override
public boolean canBeProcessed(VirtualFile file, byte[] bytes) {
if (file.getName().contains(PackageClassUtils.PACKAGE_CLASS_NAME_SUFFIX + "-") &&
StdFileTypes.CLASS.getDefaultExtension().equals(file.getExtension())) {
KotlinClassHeader header = new VirtualFileKotlinClass(LockBasedStorageManager.NO_LOCKS, file).getClassHeader();
return header != null && header.getKind() == KotlinClassHeader.Kind.PACKAGE_PART;
}
return false;
}
@Override
public boolean isInnerClass(VirtualFile file) {
return false;
}
}