3

I want to do something like this:

@Testing

private void methodThatGivesWarningIfUserTriesToUseThis() {

}

where @Testing is a custom annotation/decorator in Java. I'd like the behaviour of this @Testing annotation to act ALMOST EXACTLY like the @Deprecated annotation - if i or someone else accidentally tries to use this in other parts of the code, the IDE will give a warning (and the method name also has a strike-through across the font). So for ex

how do i do this?

**note: i do not want to use @Deprecated because the method is not deprecated, i just want only to use it for testing purposes

**this marker should also be checked at compile time, not runTime.

David T.
  • 22,301
  • 23
  • 71
  • 123

3 Answers3

2

The problem is that annotations are, as their name states, only for annotating :) Creating such annotation is pretty easy, you need to do something like this:

public @interface Testing

In order for it to be used by your IDE at compile time you have to write a plug-in.

Here is a good start I found in another SO question for starting with the plugin development.

Avi
  • 21,182
  • 26
  • 82
  • 121
2

It sounds like you're describing something like @VisibleForTesting, which is part of the Guava libraries (formerly known as "Google Collections"). If you do create one of your own, you'll probably want it to be @Documented and @Retention(SOURCE).

I'm not aware of any hook or feature that would cause non-@Deprecated members to trigger warnings or IDE flags as if they were @Deprecated. Because the Mirror API and Reflection API don't track individual expressions and statements (as documented in this SO answer), you probably won't have much luck detecting it yourself without a full Java parser.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
2

You might consider writing a custom Lint rule to solve this problem. When your @Testing annotation is detected, the IDE would show a Lint warning (e.g., a yellow underline in the case of Eclipse+ADT).

A detailed guide to scanning Java source files for specific issues with Lint can be found in the Android Tools docs here.

acj
  • 4,821
  • 4
  • 34
  • 49