Inspection called too many times

hi,

I have created an inspection that overrides the buildVisitor method and then again overrides the visitNewExpression method. That works nicely and my code is being called correctly. Only problem is that its called six times! That means that my warning text is shown six times.

The test files I'm using has one line inside it with only one new operator. Whats wrong here?

regards

Anders

8 comments

Hello Anders,

AB> I have created an inspection that overrides the buildVisitor method
AB> and then again overrides the visitNewExpression method. That works
AB> nicely and my code is being called correctly. Only problem is that
AB> its called six times! That means that my warning text is shown six
AB> times.
AB>
AB> The test files I'm using has one line inside it with only one new
AB> operator. Whats wrong here?

This was discussed in this forum just yesterday: the visitor you return from
buildVisitor() must not be a PsiRecursiveElementVisitor.

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Thanks a lot for the quick response. I actually did search for an answer but apparantly missed it anyhow...

regards

Anders

0

Hi again,

I've now looked at it. And as the plugin I'm creating should also work under version 5.1 what other choices do I have ?

regards

Anders

0

Hello Anders,

AB> I've now looked at it. And as the plugin I'm creating should also
AB> work under version 5.1 what other choices do I have ?

Override checkFile().

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Hi,

I was too fast wih my answer. Should'nt the following work:

public ProblemDescriptor[] checkFile(PsiFile psiFile, InspectionManager inspectionManager, boolean b) {
return analyzeCode(psiFile, inspectionManager);
}

private ProblemDescriptor[] analyzeCode(PsiElement where, InspectionManager manager) {
where.accept(new PsiElementVisitor() {
public void visitReferenceExpression(PsiReferenceExpression psiReferenceExpression) {}

public void visitNewExpression(PsiNewExpression psiNewExpression) {
super.visitNewExpression(psiNewExpression);
PsiFile containingFile = psiNewExpression.getContainingFile();
PsiFile[] files = containingFile.getContainingDirectory().getFiles();
for (int i = 0; i < files.length; i++) {
PsiFile file = files+;
System.out.println("file = " + file.getName());
}
}
});
return new ProblemDescriptor[0];
}

Nothing is printed out but if I change it to override the PsiRecursiveElementVisitor instead it prints put the lines. I'm using the exact same test project in both cases. Any ideas ?

regards

Anders

0

Hello Anders,

With checkFile(), the visitor you use does need to be recursive. On the other
hand, if you override buildVisitor(), the recursion is handled by IDEA itself.

AB> I was too fast wih my answer. Should'nt the following work:
AB>
AB> public ProblemDescriptor[] checkFile(PsiFile psiFile,
AB> InspectionManager inspectionManager, boolean b) {
AB> return analyzeCode(psiFile, inspectionManager);
AB> }
AB> private ProblemDescriptor[] analyzeCode(PsiElement where,
AB> InspectionManager manager) {
AB> where.accept(new PsiElementVisitor() {
AB> public void
AB> visitReferenceExpression(PsiReferenceExpression
AB> psiReferenceExpression) {}
AB> public void visitNewExpression(PsiNewExpression
AB> psiNewExpression) {
AB> super.visitNewExpression(psiNewExpression);
AB> PsiFile containingFile =
AB> psiNewExpression.getContainingFile();
AB> PsiFile[] files =
AB> containingFile.getContainingDirectory().getFiles();
AB> for (int i = 0; i < files.length; i++) {
AB> PsiFile file = files+;
AB> System.out.println("file = " + file.getName());
AB> }
AB> }
AB> });
AB> return new ProblemDescriptor[0];
AB> }
AB> Nothing is printed out but if I change it to override the
AB> PsiRecursiveElementVisitor instead it prints put the lines. I'm
AB> using the exact same test project in both cases. Any ideas ?

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Ok. So I use PsiRecursiveElementVisitor when using checkFile (5.1) BUT this gives me to hits. Ie my code gets called more than once.

0

Please sign in to leave a comment.