Class file from PsiJavaFIle

Hi

Is it possible to create or get the corresponding, compiled class file out of an existing PsiJavaFile from the project view?
I need it for a Java patch plugin in IntelliJ, where we directly patch classes.

 

Regards

Stefan

0
2 comments
Avatar
Permanently deleted user

Hi Stefan, 

BytecodeViewer plugin does similar thing. Please check ByteCodeViewerManager.getByteCode(PsiElement element)

 

@Nullable
public static String getByteCode(@NotNull PsiElement psiElement) {
PsiClass containingClass = getContainingClass(psiElement);
if (containingClass == null) return null;
CompilerPathsEx.ClassFileDescriptor file = CompilerPathsEx.findClassFileInOutput(containingClass);
if (file != null) {
  try {
    return processClassFile(file.loadFileBytes());
  }
  catch (IOException e) {
      LOG.error(e);
   }
 }
return null;
}

0
Avatar
Permanently deleted user

This perfectly works, thank you!

0

Please sign in to leave a comment.