简体   繁体   中英

Show warning for usage of methods/constructor annotated with custom annotation?

I've got an application that uses an ORM solution that requires the default constructor to be left in the class. To prevent my accidental usage of this default constructor, I would like to highlight any usages of it with warning.

I'm currently annotating it with a custom annotation like "@DoNotUse" but I can't figure out how to get Intellij to mark its usages with a warning.

I don't believe structural search inspections can solve this problem.

Is my only hope a plugin now?

EDIT: Since someone obviously didn't read the question, I am not looking for a reason why @Deprecated is causing a statement in my code to be marked with a warning, I'm looking for a way to replicate this effect with other custom annotations.

Actually, this is possible using Structural Search. However it requires knowledge of the internals of IntelliJ IDEA, so it's hard to figure out.

Use the following pattern:

new $Constructor$();

Click Edit Variables... and set the Script Text of Constructor to:

import com.intellij.codeInsight.AnnotationUtil
def method = __context__.parent.resolveMethod()
AnnotationUtil.findAnnotation(method, "fully.qualified.annotation.DoNotUse") != null

This should find all invocations of default constructors where the constructor is annotated with fully.qualified.annotation.DoNotUse . After you get it working, create a Structural Search inspection with the pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM