Pedro Estevão
Pedro Estevão

Reputation: 1074

My own deprecated annotation

It's possible to create an annotation like @deprecated, I mean, with deprecated code-style?

I'd like to create an annotation to indicates that some messages were removed from the communication protocol, so I'd not want to use the deprecated annotation because I want 1) give a better and more suggestive name for my annotation, like "message removed" 2) give parameters for my annotation, e.g. (protocol = 5) -removed since protocol 5.

But, I want to keep this strikethrough code for others know that this message has been removed just by looking to the code.

Upvotes: 8

Views: 3693

Answers (5)

user2842803
user2842803

Reputation: 1

Mayur Gupta,

I created a annotation MessageRemoved:

public @interface MessageRemoved {
    Protocol protocol();
}

And a Enum:

public enum Protocol {
    P01, P02, P03, P04, P05
}

Using a Annotation:

@MessageRemoved(protocol = Protocol.P05)
public class OldMessage extends Message{
}

This facilitates the traceability of messages removed. Using only annotation @Deprecated this is not possible.

Upvotes: 0

Mayur Gupta
Mayur Gupta

Reputation: 780

There are two things you can do:

Add the @Deprecated annotation to the method, and Add a @deprecated tag to the javadoc of the method

You should do both!

Quoting the java documentation on this subject:

Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead.

Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used. The compiler suppresses deprecation warnings if a deprecated compilation unit uses a deprecated class, method, or field. This enables you to build legacy APIs without generating warnings.

You are strongly recommended to use the Javadoc @deprecated tag with appropriate comments explaining how to use the new API. This ensures developers will have a workable migration path from the old API to the new API

Upvotes: 2

Algiz
Algiz

Reputation: 1288

+1 for oleg.lukyrych's answer BUT you can still do something.

Create your annotation (with all the parameters you want), then add a static code analysis to your build procedure. i.e. PMD with a custom rule. PMD is well know and well integrated in various IDE and continuous build environment like hudson/jenkins. The analysis will produce you a nice report of the (mis)use of your deprecated message.

It is not as nice as having it into your editor but it make the job.

Hope this helps.

Upvotes: 0

user2813148
user2813148

Reputation:

@Deprecated annotation is designed for the development environment (generally but not only). It does nothing itself. But for IDE it is the some kind of marker and when it "see" that annotation - it performs some logic (for example "strikethrough" the code). The goal of my post is to tell that it is not enough to develop and use the annotation : it must me supported by environment.

Upvotes: 5

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

When looking at the source code of the Deprecated annotation, you will see that there is nothing specific that makes it appear with the strikethrough. It is a feature of the IDE to mark @Deprecated code with a strikethrough.

Here it is:

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}

Upvotes: 1

Related Questions