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

1
7 comments

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.

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).


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Avatar
Permanently deleted user

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

0

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.

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?


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Avatar
Permanently deleted user

Great! thanks a lot, it works!
Just a strange thing, sometimes i get this exception:

com.intellij.openapi.progress.ProcessCanceledException
     at com.intellij.openapi.progress.util.ProgressIndicatorBase.checkCanceled(ProgressIndicatorBase.java:217)
     at com.intellij.openapi.progress.impl.ProgressManagerImpl.doCheckCanceled(ProgressManagerImpl.java:77)
     at com.intellij.openapi.progress.ProgressManager.checkCanceled(ProgressManager.java:45)
     at com.intellij.psi.impl.source.tree.CompositeElement.getPsi(CompositeElement.java:674)
     at com.intellij.psi.impl.source.SourceTreeToPsiMap.treeElementToPsi(SourceTreeToPsiMap.java:31)
     at com.intellij.psi.impl.source.tree.SharedImplUtil.getParent(SharedImplUtil.java:43)
     at com.intellij.psi.impl.source.tree.LeafPsiElement.getParent(LeafPsiElement.java:71)


But other than that it works perfectly fine.
Thanks again,
Tomas
0

Hello Tomas,

You should pass an EmptyProgressIndicator as the progress indicator parameter.

Great! thanks a lot, it works!

Just a strange thing, sometimes i get this exception:

com.intellij.openapi.progress.ProcessCanceledExceptio
> n
>
>      at
> com.intellij.openapi.progress.util.ProgressIndicatorBase.checkCanceled
> (ProgressIndicatorBase.java:217)
>
>      at
> com.intellij.openapi.progress.impl.ProgressManagerImpl.doCheckCanceled
> (ProgressManagerImpl.java:77)
>
>      at
> com.intellij.openapi.progress.ProgressManager.checkCanceled(ProgressMa
> nager.java:45)
>
>      at
> com.intellij.psi.impl.source.tree.CompositeElement.getPsi(CompositeEle
> ment.java:674)
>
>      at
> com.intellij.psi.impl.source.SourceTreeToPsiMap.treeElementToPsi(Sourc
> eTreeToPsiMap.java:31)
>
>      at
> com.intellij.psi.impl.source.tree.SharedImplUtil.getParent(SharedImplU
> til.java:43)
>
>      at
> com.intellij.psi.impl.source.tree.LeafPsiElement.getParent(LeafPsiElem
> ent.java:71)


But other than that it works perfectly fine.
Thanks again,
Tomas
---
Original message URL:
http://devnet.jetbrains.net/message/5321601#5321601


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Avatar
Permanently deleted user

Hi Dmitry,

Everything works fine, i used the EmptyProgressIndicator as you suggested but now i get the following exception:

com.intellij.openapi.progress.ProcessCanceledException
     at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.a(LocalInspectionsPass.java:271)
     at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.a(LocalInspectionsPass.java:225)
     at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.collectInformationWithProgress(LocalInspectionsPass.java:131)
     at com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.doCollectInformation(ProgressableTextEditorHighlightingPass.java:57)
     at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl.runMainPasses(DaemonCodeAnalyzerImpl.java:166)



Any ideas?

Thanks in advance,
Tomas
0

Either don't catch that exception, or catch and rethrow it.

0

Please sign in to leave a comment.