Example #1
0
  @Nullable
  public static PsiJavaFileStub buildFileStub(@NotNull VirtualFile file, @NotNull byte[] bytes)
      throws ClsFormatException {
    if (ClassFileViewProvider.isInnerClass(file)) {
      return null;
    }

    try {
      PsiJavaFileStubImpl stub = new PsiJavaFileStubImpl("do.not.know.yet", true);
      String className = file.getNameWithoutExtension();
      StubBuildingVisitor<VirtualFile> visitor =
          new StubBuildingVisitor<VirtualFile>(file, STRATEGY, stub, 0, className);
      try {
        new ClassReader(bytes).accept(visitor, ClassReader.SKIP_FRAMES);
      } catch (OutOfOrderInnerClassException e) {
        return null;
      }

      PsiClassStub<?> result = visitor.getResult();
      if (result == null) return null;

      stub.setPackageName(getPackageName(result));
      return stub;
    } catch (Exception e) {
      throw new ClsFormatException(file.getPath() + ": " + e.getMessage(), e);
    }
  }
  @Nullable
  private static ClsClassImpl createClsJavaClassFromVirtualFile(
      @NotNull final JetFile decompiledKotlinFile,
      @NotNull VirtualFile virtualFile,
      @Nullable final JetClassOrObject decompiledClassOrObject) {
    final PsiJavaFileStubImpl javaFileStub = getOrCreateJavaFileStub(virtualFile);
    if (javaFileStub == null) {
      return null;
    }
    PsiManager manager = PsiManager.getInstance(decompiledKotlinFile.getProject());
    ClsFileImpl fakeFile =
        new ClsFileImpl((PsiManagerImpl) manager, new ClassFileViewProvider(manager, virtualFile)) {
          @NotNull
          @Override
          public PsiElement getNavigationElement() {
            if (decompiledClassOrObject != null) {
              return decompiledClassOrObject.getNavigationElement().getContainingFile();
            }
            return super.getNavigationElement();
          }

          @NotNull
          @Override
          public PsiClassHolderFileStub getStub() {
            return javaFileStub;
          }

          @Override
          public PsiElement getMirror() {
            return decompiledKotlinFile;
          }
        };
    fakeFile.setPhysical(false);
    javaFileStub.setPsi(fakeFile);
    return (ClsClassImpl) single(fakeFile.getClasses());
  }