Variable 'variable' might not have been initialized compilation error suppressing by custom annotation

Answered

Hi Dev Team!
I would like to make a little cheat on Java syntax.
There is a built-in compilation error like Variable 'variable' might not have been initialized 
I am working on a framework and there is some recompilation process and works on background but intellij doesn't allow this.

For instance I want this code compile:

public void method() {
@Recall("random value") String variable;
System.out.println(variable);
}

Is this possible via custom IDEA plugin? 

Looking forward to the answer!
Thank you!

0
6 comments

https://plugins.jetbrains.com/docs/intellij/controlling-highlighting.html can be used to suppress this, in fact de.plushnikov.intellij.plugin.extension.LombokHighlightErrorFilter.LombokHighlightFilter#VARIABLE_MIGHT_NOT_BEEN_INITIALIZED in Lombok plugin does that.

0

Yann Cebron That worked for me. Thank you!

0

Yann Cebron
going further a bit. Now how can I check if annotation present on local variable?

accept(...) method provides PsiFile and HighlighterInfo objects.
I supress now such compilation errors by a description. However this is a half way to my goal.


Main goal is to supress it by description AND if variable is marked by annotation.

But I wasn't able to figure out how I can retreive variable annotation to check the presense only given PsiFile and highlighter info?

0

You'll need to locate the related PsiElement reprenting the variable declaration using text offset from HighlightInfo. See https://plugins.jetbrains.com/docs/intellij/navigating-psi.html#bottom-up-navigation for some sample.

Then use com.intellij.codeInsight.AnnotationUtil from Java PSI API to inspect whether annotation is present for found PsiVariable etc.

0

Thank you!
Had to use some recursion instead using .getParent() and .getPrevSibling() method.

0

PsiTreeUtil.getParentOfType() should work

0

Please sign in to leave a comment.