4

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.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
nulldev
  • 555
  • 6
  • 16
  • 2
    Make the constructor private. Most ORMs use reflection to instantiate classes. – dom farr Mar 10 '16 at 21:47
  • Yeah, I'm going to try this now but I'm not sure if it will work. – nulldev Mar 10 '16 at 21:49
  • 1
    Why you don't use @Deprecated annotation? – dehasi Mar 10 '16 at 21:57
  • Actually, yes I was using this in the past but it was sort of confusing so I was hoping if a better solution exists. – nulldev Mar 10 '16 at 21:57
  • @dehasi that is not a good idea because this classes isn't Deprecated. – dom farr Mar 10 '16 at 21:58
  • @domfarr http://stackoverflow.com/questions/1999766/the-constructor-date-is-deprecated-what-does-it-mean-java/1999796#1999796 – dehasi Mar 10 '16 at 22:04
  • 1
    @domfarr You can mark only target constructor – VMAtm Mar 10 '16 at 22:04
  • @dehasi this class isn't being phased out. Deprecate means you should not use this class as it will be **deleted** in future releases. – dom farr Mar 10 '16 at 22:11
  • @VMAtm I'm unsure what your are talking about? Please expand – dom farr Mar 10 '16 at 22:12
  • He means you can mark a single method/constructor as being deprecated with: `@Deprecated public Test() {}` – nulldev Mar 10 '16 at 22:13
  • I get that, sorry, I mean in the context of this question. @nulldev are you planning on deleting this class in the future? If not, Deprecate is the wrong annotation. – dom farr Mar 10 '16 at 22:16
  • No I don't but `@Deprecated` doesn't actually mean a class will be phased out. It only means the class should not be used (Read the official Javadocs for it). **But I still prefer if I can get a custom annotation to do this!** – nulldev Mar 10 '16 at 22:18

1 Answers1

2

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.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
  • **WOAH,** this actually works! Accepting this right away! Thanks so much! – nulldev Mar 14 '16 at 20:18
  • I can't get your example to work in Android Studio 2.3. I'd like to do something similar but allow the annotation to be used on methods, fields or constructors. Additionally I'm trying to allow a message to be specified and shown in the error (not a must have) So are you saying the `new $Constructor$();` part needs to be in the **Search Template** textbox when you add a new Search Template under structural search? Also, what do you mean by "**After you get it working**, create a Structural Search inspection with the pattern." Isn't that what the `new $Constructor$();` is? Thanks – Joshua King Sep 11 '17 at 20:34
  • @JoshuaKing: He means you should take that search configuration and use it in https://www.jetbrains.com/help/idea/creating-custom-inspections.html – SamB Oct 25 '19 at 20:00