create a method and add it to a java class

Answered

Hello,

I am trying to create a  method and add it to a java class. The method is created and I use the right code to add it to the class (c) (see the following code. But, there is no change in the class code:

runnableHelper.runWriteCommand(project,new Runnable() {

@Override
public void run() {


c.add(print);


PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(PsiDocumentManager.getInstance(project).getDocument(c.getContainingFile()));
PsiDocumentManager.getInstance(project).commitDocument(PsiDocumentManager.getInstance(project).getDocument(c.getContainingFile()));
VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);


}
});

 

4 comments
Comment actions Permalink

Please show full context.

This line is most definitely not needed

VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
0
Comment actions Permalink

print is a method created by JavaFacade....getElementFactory.createMethod and c is a variable containing a PsiClass

0
Comment actions Permalink

I need to see the full class and invocation context

0
Comment actions Permalink
public class AbstractClassesDetection extends AnAction{
private JButton next;
RunnableHelper runnableHelper = new RunnableHelper();
JFrame mainWindow;
MainFRame dual;
Utilities utils = new Utilities();
public AbstractClassesDetection(){

}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {

Project project = e.getData(PlatformDataKeys.PROJECT);

PsiFile psifile = PsiManager.getInstance(project).findFile(project.getWorkspaceFile());
Path p = Paths.get(project.getBaseDir().findChild("src").getPath());


List<PsiClass> projectPsiClasses = utils.walkOnPsiClasses(project);
for(int i = 0; i < projectPsiClasses.size(); i++){
if(utils.isAbstract(projectPsiClasses.get(i))) {
System.out.println(((PsiJavaFile) projectPsiClasses.get(i).getContainingFile()).getPackageName() + "." + projectPsiClasses.get(i).getName());

PsiClass c = projectPsiClasses.get(i);
PsiMethod print = utils.createAbstractMethod("print", project);

CommandProcessor cmdprocessor = CommandProcessor.getInstance();

runnableHelper.runWriteCommand(project,new Runnable() {

@Override
public void run() {

c.add(print);


PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(PsiDocumentManager.getInstance(project).getDocument(c.getContainingFile()));
PsiDocumentManager.getInstance(project).commitDocument(PsiDocumentManager.getInstance(project).getDocument(c.getContainingFile()));
VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);


}
});
}
}

mainWindow = new JFrame();
mainWindow.setTitle("Detect Project ABstract Classes");
next = new JButton("Next");


next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
mainWindow.hide();
}
});


mainWindow.getContentPane().add(new DualListBox2(projectPsiClasses,project), BorderLayout.CENTER);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocation(500,500);
mainWindow.setSize(1200,1000);
mainWindow.setVisible(true);


}
}

 

0

Please sign in to leave a comment.