Generete annotation and methods/code in method in already existing java file. Follow
Hi,
How can I (correct way) generate code (annotations, new methods and code inside eg. class constructor) in already existing java file? I will appreciate any kind of help or ideas.
Please sign in to leave a comment.
Do you want to generate source code visible in the file or something like lombok generated members which are in the classfiles but not in the source files?
Anna
Yes, i want generate source code visible in file. Something like that.
Use `com.intellij.psi.PsiJavaParserFacade#createXXXFromText` to create elements from text. (To get the factory, use `com.intellij.psi.JavaPsiFacade#getElementFactory(com.intellij.openapi.project.Project)`)
Ok it works fine, but i have one problem. Im adding it with:
After adding annotation and imports first time it works like i want it to:
but when i add second time my annotation it merges with Element 3 and it looks like this. Why it merge and how can I prevent it?
I'd suggest to `addBefore` with your class as anchor. Otherwise your code would be broken by package statement/import statement, etc.
I changed it like you suggested but it still merge into class when i try to add second annotation and looks like:
It merges inside previous annotation as far as I can see. Please check what element is added and that anchor is still valid after you added the first element.
After adding first annotation: (Element 2 is added)
When I try to add annotation again: (it automatic merge into next element?)
annotations belong to class's modifier list.
I want to add next annotation like this:
before second annotation:
after adding second annotation:
My idea was to replace annotation element with new element (created from text) with new annotation but I guess it is not possible because i will replace class as well. Do you know how can i change it like that?
You want to add next annotation inside added annotation. Thus, you need to keep added annotation and add new element inside, like
```
PsiAnnotation anno = psiClass.add(firstAnnoFromText);
anno.addAfter(secondAnnoFromText, anchor);
```
where anchor need to be found among the `anno` children according to your rules.
How can I generate code inside the method i focus on now ?
If you have a caret inside the method, then AnAction will provide you with `com.intellij.openapi.actionSystem.DataContext` from which you may get psiElement (`com.intellij.openapi.actionSystem.CommonDataKeys#PSI_ELEMENT`). This element will correspond to the leaf element under caret. Then you would need to check the tree above the element and perform your transformations.