7

Is there something special to the @Deprecated annotation that I cannot reproduce?

I need to create an annotation similar to @Deprecated to produce warnings in Eclipse and also at build time. When I mark a method as @Deprecated I get nice warnings. For example, if I have an old method (that I may still keep for compatibility reasons):

@Deprecated
public List<Account> getClientAccounts(final int clientId) {
  // Implement search...
}
  • Then, if I try to use it in Eclipse I can see it strikethrough, and a yellow icon in the left bar:

    enter image description here

  • Also when building I can see the:

    [WARNING] app1/src/test/java/com/app1/MyApp.java: app1/src/test/java/com/app1/MyApp.java uses or overrides a deprecated API.

Now, depending on external factors I cannot control (e.g. absence of database indexes) some methods are not optimal, and I would like to clearly mark them as such... with my brand new @NonOptimal annotation. I need to add visibility to the problem. So far I have:

@Retention(RUNTIME)
@Target(METHOD)
// What else here?
public @interface NonOptimal {
}

How can I create this annotation?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • 2
    in order to do something like this you need to create an annotation processor for the compiler, e.g. https://cloudogu.com/en/blog/Java-Annotation-Processors_1-Intro – jtahlborn Aug 19 '20 at 15:19
  • @jtahlborn I'm trying to follow the instructions in your link but the compiler says those clases are not part of the API. I get: `Access restriction: The method 'JavaCompiler.CompilationTask.call()' is not API (restriction on required library '/apps/jdk-11+28/lib/jrt-fs.jar')`. Maybe this doesn't work in Java 11 anymore. – The Impaler Aug 19 '20 at 15:39
  • i believe that is an _eclipse_ restriction. you may need to change some settings in eclipse to allow it to build your project: https://stackoverflow.com/questions/25222811/access-restriction-the-type-application-is-not-api-restriction-on-required-l/28331980 – jtahlborn Aug 19 '20 at 16:13
  • 1
    @jtahlborn Quite good option, actually. I implemented it for Java 11 and it works well during the build. About the other side (Eclipse warning, icons, etc) do you have any pointers? – The Impaler Aug 19 '20 at 18:03
  • For Eclipse, two options are to write an Eclipse plugin, or to run an external build (that is, call javac with your annotation processor). The former can provide a nicer UI experience, but the latter is easier to implement and easier to apply to other IDEs. – mernst Aug 21 '20 at 17:59
  • @mernst I think an Eclipse plugin is kind of overkill. Plus I would need to ensure developers are using it. I was thinking more like using a standard IDE. I'll try running the external build to see how it works. Thanks. – The Impaler Aug 21 '20 at 18:13

2 Answers2

3

@TheImpaler This is actually not a true answer for your problem, but some time ago I came across the Google Annotations Library (a.k.a. gag) while using Zalando's Problem API.

This library provides a great number of custom annotations that, in some cases, can be used to instrument your actual code by using a ASM and a custom java agent.

Maybe it can give you some ideas regarding your actual issue.

The project is no longer maintained but there is a fork in Github.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • Though your answer doesn't resolved the problem, I decided to give you the bounty, so it doesn't go to waste. These annotations, by the way, are quite funny (and some are even useful!). – The Impaler Sep 03 '20 at 16:36
  • Thank you very much!! I did not expect the bounty!! And you are right about the annotations; if possible, I usually use them in my projects. Thank you again!! – jccampanero Sep 03 '20 at 17:41
2

I wish I could extend Deprecated, but no can do.

After reading about this quite a bit I ended up with an ugly workaround. It works, though I don't like it.

I decided to mark the bad methods with both the @Deprecated and @NonOptimal annotations. It's conceptually wrong (the methods are not actually deprecated) but it works well out of the box. No need to develop an overkill Eclipse plugin:

  • The @Deprecated annnotation bugs developers all around the place (in Eclipse and when building), and that's a good thing.

  • The @NonOptimal annotation provides details on why this is a bad method to use.

Ugly but works. As of now Eclipse does not provide any better option.

Note: to make things worse, the NonOptimal annotation does not work well in Maven when using toolchains: warnings go silent, disappear, nada... Therefore, AnnotationProcessors are kind of useless in the end.

The Impaler
  • 45,731
  • 9
  • 39
  • 76