1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.psi.compiled;
4 import com.intellij.openapi.application.Application;
5 import com.intellij.openapi.application.ApplicationManager;
6 import com.intellij.openapi.components.Service;
7 import com.intellij.openapi.extensions.ExtensionPointName;
8 import com.intellij.openapi.fileTypes.BinaryFileTypeDecompilers;
9 import com.intellij.openapi.vfs.VirtualFile;
10 import com.intellij.psi.FileViewProvider;
11 import com.intellij.psi.PsiManager;
12 import org.jetbrains.annotations.NotNull;
15 * An API to extend default IDEA .class file decompiler and handle files compiled from sources other than Java.
18 public final class ClassFileDecompilers {
20 * Actual implementations should extend either {@link Light} or {@link Full} classes -
21 * those that don't are silently ignored.
23 public interface Decompiler {
24 boolean accepts(@NotNull VirtualFile file);
28 * <p>"Light" decompilers are intended for augmenting file text constructed by standard IDEA decompiler
29 * without changing it's structure - i.e. providing additional information in comments,
30 * or replacing standard "compiled code" method body comment with something more meaningful.</p>
32 * <p>If a plugin by somewhat reason cannot decompile a file it can throw {@link Light.CannotDecompileException}
33 * and thus make IDEA to fall back to a built-in decompiler implementation.</p>
35 * <p>Plugins registering extension of this type normally should accept all files and use {@code order="last"}
36 * attribute to avoid interfering with other decompilers.</p>
38 public abstract static class Light implements Decompiler {
39 public static class CannotDecompileException extends RuntimeException {
40 public CannotDecompileException(String message) {
44 public CannotDecompileException(Throwable cause) {
50 public abstract CharSequence getText(@NotNull VirtualFile file) throws CannotDecompileException;
54 * <p>"Full" decompilers are designed to provide extended support for languages significantly different from Java.
55 * Extensions of this type should take care of building file stubs and properly indexing them -
56 * in return they have an ability to represent decompiled file in a way natural for original language.</p>
58 public abstract static class Full implements Decompiler {
60 public abstract ClsStubBuilder getStubBuilder();
63 * <h5>Notes for implementers</h5>
65 * <p>1. Return a correct language from {@link FileViewProvider#getBaseLanguage()}.</p>
67 * <p>2. This method is called for both PSI file construction and obtaining document text.
68 * In the latter case the PsiManager is based on default project, and the only method called
69 * on a resulting view provider is {@link FileViewProvider#getContents()}.</p>
71 * <p>3. A language compiler may produce auxiliary .class files which should be handled as part of their parent classes.
72 * A standard practice is to hide such files by returning {@code null} from
73 * {@link FileViewProvider#getPsi(com.intellij.lang.Language)}.</p>
76 public abstract FileViewProvider createFileViewProvider(@NotNull VirtualFile file, @NotNull PsiManager manager, boolean physical);
79 public static ClassFileDecompilers getInstance() {
80 return ApplicationManager.getApplication().getService(ClassFileDecompilers.class);
83 public final ExtensionPointName<Decompiler> EP_NAME = new ExtensionPointName<>("com.intellij.psi.classFileDecompiler");
85 private ClassFileDecompilers() {
86 Application app = ApplicationManager.getApplication();
87 if (!app.isHeadlessEnvironment() || app.isUnitTestMode()) {
88 EP_NAME.addChangeListener(() -> BinaryFileTypeDecompilers.getInstance().notifyDecompilerSetChange(), null);
92 @SuppressWarnings("unchecked")
93 public <D extends Decompiler> D find(@NotNull VirtualFile file, @NotNull Class<D> decompilerClass) {
94 return (D)EP_NAME.findFirstSafe(d -> decompilerClass.isInstance(d) && d.accepts(file));