Check PsiMethod for compile/syntax errors
Hi,
I'm creating a plugin, and i need to check whether there are errors in a specific java class method - before the compilation. How can i access the syntax errors that appear at runtime when using Java Editor?
Btw there's no need for me to know the specific error, i just need to know whether the method is valid or not (so in more simple words, whether it contains any red parts when edited via Java Editor).
Thanks in advance,
Tomas
Please sign in to leave a comment.
Hello Tomas,
Syntax errors are represented as PsiErrorElement instances in the tree. You
can go through the method with a PsiRecursiveElementVisitor and check for
the existing of error elements.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
And how are compiler problems presented? For example if i have this text in java class method:
public void updateOutput(String output) {
this.asjdgglsdk;
}
And in my class there's no field "asjdgglsdk". This wouldn't present itself as a PsiErrorElement. How can i get these errors?
Thanks,
Tomas
Hello Tomas,
It would be easier for us to suggest the optimum solution if we knew exactly
why you need this functionality.
One possibility is the following:
DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myProject);
List infos = codeAnalyzer.runMainPasses(psiFile, document,
progress);
This runs the analysis on the entire file; you'll need then to got through
the list of returned infos and filter the ones that are in the text range
of your PsiMethod.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Great! thanks a lot, it works!
Just a strange thing, sometimes i get this exception:
But other than that it works perfectly fine.
Thanks again,
Tomas
Hello Tomas,
You should pass an EmptyProgressIndicator as the progress indicator parameter.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hi Dmitry,
Everything works fine, i used the EmptyProgressIndicator as you suggested but now i get the following exception:
Any ideas?
Thanks in advance,
Tomas
Either don't catch that exception, or catch and rethrow it.