private JetDecompiledData build() { myBuilder.append(PsiBundle.message("psi.decompiled.text.header")); myBuilder.append("\n\n"); String packageName = myClsFile.getPackageName(); if (packageName.length() > 0) { myBuilder.append("package ").append(packageName).append("\n\n"); } PsiClass psiClass = myClsFile.getClasses()[0]; if (isKotlinNamespaceClass(psiClass)) { NamespaceDescriptor nd = myJavaDescriptorResolver.resolveNamespace( new FqName(packageName), DescriptorSearchRule.INCLUDE_KOTLIN); if (nd != null) { for (DeclarationDescriptor member : sortDeclarations(nd.getMemberScope().getAllDescriptors())) { if (member instanceof ClassDescriptor || member instanceof NamespaceDescriptor) { continue; } appendDescriptor(member, ""); myBuilder.append("\n"); } } } else { ClassDescriptor cd = myJavaDescriptorResolver.resolveClass(psiClass, DescriptorSearchRule.INCLUDE_KOTLIN); if (cd != null) { appendDescriptor(cd, ""); } } JetFile jetFile = JetDummyClassFileViewProvider.createJetFile( myClsFile.getManager(), myClsFile.getVirtualFile(), myBuilder.toString()); for (Map.Entry<PsiElement, TextRange> clsMemberToRange : myClsMembersToRanges.entrySet()) { PsiElement clsMember = clsMemberToRange.getKey(); assert clsMember instanceof ClsElementImpl; TextRange range = clsMemberToRange.getValue(); JetDeclaration jetDeclaration = PsiTreeUtil.findElementOfClassAtRange( jetFile, range.getStartOffset(), range.getEndOffset(), JetDeclaration.class); assert jetDeclaration != null; myClsElementsToJetElements.put((ClsElementImpl) clsMember, jetDeclaration); } return new JetDecompiledData(jetFile, myClsElementsToJetElements); }
private DecompiledDataFactory(ClsFileImpl clsFile) { myClsFile = clsFile; Project project = myClsFile.getProject(); JavaSemanticServices jss = new JavaSemanticServices(project, new BindingTraceContext()); myBindingContext = jss.getTrace().getBindingContext(); myJavaDescriptorResolver = jss.getDescriptorResolver(); }
@Nullable private static PsiJavaFileStub createStub(@NotNull VirtualFile file) { if (file.getFileType() != JavaClassFileType.INSTANCE) return null; try { return ClsFileImpl.buildFileStub(file, file.contentsToByteArray()); } catch (ClsFormatException e) { LOG.debug(e); } catch (IOException e) { LOG.debug(e); } LOG.error("Failed to build java cls class for " + file.getCanonicalPath()); return null; }
@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()); }