jagdpanzer
jagdpanzer

Reputation: 713

Deprecating a variable in an external library @interface

Let's say I have an external library implemented into my project, and an annotation interface I'd like to use in it, namely, FindBy:

@Target({ElementType.FIELD, ElementType.TYPE})
public @interface FindBy {
  String id() default "";

  String name() default "";
}

And let's say I want to @Deprecate the String name() variable.

As this is an external library, I can not go into the interface and simply deprecate it. Extending it, implementing it, or trying to override it hasn't proven fruitful either.

What is the best practice for deprecating this one variable in the external interface?

Upvotes: 1

Views: 409

Answers (1)

LppEdd
LppEdd

Reputation: 21144

There is no Java-way of doing this. At least, not if you want to avoid re-packaging the library.
You can, however, leverage the IDE. For example, using IntelliJ IDEA, you can use External annotations.

What that means is an annotation.xml file is maintained, per package, to contain all the wanted annotations on classes, methods, fields, etc.

After enabling the External annotation feature, navigate to your FindBy annotation, then, over the target method, open the quick action popup

enter image description here

And choose "Annotate method ...". Another popup will appear

enter image description here

Choose java.lang.Deprecated.
At this point, an annotation.xml file has been created, with a content similar to

<root>
    <item name='com.fasterxml.jackson.databind.annotation.JsonSerialize java.lang.Class&lt;? extends com.fasterxml.jackson.databind.JsonSerializer&gt; using()'>
        <annotation name='java.lang.Deprecated' />
    </item>
</root>

My example is done on the Jackson JsonSerialize#using annotation's field.
On the usages, you'll now see it as striked/deprecated

enter image description here

You can now git add and git push these files, and everyone on your team will see the field's deprecation.


Eclipse has a similar feature, you might be able to leverage it.
See here. I never tried as I don't use Eclipse anymore.

Upvotes: 3

Related Questions