rampion
rampion

Reputation: 89053

Java: Common Annotation for "Not Yet Implemented"

Is there a common or standard annotation in Java for methods that, while defined, have yet to be implemented?

So that, for instance, if I were using a pre-alpha version of a library that contained something like

@NotImplementedYet
public void awesomeMethodThatTotallyDoesExactlyWhatYouNeed(){ /* TODO */ }

I'd get a compile-time warning when trying to call awesomeMethodThatTotallyDoesExactlyWhatYouNeed?

Upvotes: 30

Views: 22226

Answers (4)

Jens Bannmann
Jens Bannmann

Reputation: 5105

No, there is no standard annotation specifically for methods that have yet to be implemented.

However, there is a more general annotation in the JDK that marks an API which developers are discouraged from using, and the Java compiler itself can issue warnings when it is used. I am talking about @Deprecated, which many developers only think of as "announcing removal". However, the relevant articles in the JDK docs (e.g. for Java 7 and Java 9) list several example use cases, only one of them being about removal:

  • The API is dangerous (for example, the Thread.stop method).

  • There is a simple rename (for example, AWT Component.show/hide replaced by setVisible).

  • A newer, better API can be used instead.

  • The deprecated API is going to be removed.

I think your case "not implemented yet" certainly goes in the same direction as those. Further, if the method would always throw a NotYetImplementedException, it even fits the example "The API is dangerous".

So all you need to do is the following:

  1. Add @Deprecated to the method
  2. Add @deprecated Javadoc to explain the background
  3. Configure your build to invoke the compiler with -Xlint:deprecation so that it issues warnings.

Upvotes: 3

mhradek
mhradek

Reputation: 1376

You could create your own annotation. With the runtime retention policy you can then configure target builds to know to look for this annotation, if necessary.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({
    ElementType.ANNOTATION_TYPE, 
    ElementType.METHOD, 
    ElementType.CONSTRUCTOR,
    ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Unimplemented {

    boolean value() default true;
}

Upvotes: 2

Andrejs
Andrejs

Reputation: 27677

Google libraries use the @Beta annotations for API that is likely to change but the methods are implemented though

Upvotes: 1

Alexander
Alexander

Reputation: 23537

You might want to use UnsupportedOperationException and detect calls to-yet-to-be-implemented methods when running your tests.

Upvotes: 19

Related Questions