Came across this #Deprecated() today. Curious to know #Deprecated vs #Deprecated() but both are resolving to the same interface Deprecated by the compiler.
Are they both different or are they same? Is there some practice to use one over another?
They both mean the same thing. #Deprecated is simply shorthand for #Deprecated(). See §9.7.2. Marker Annotations of the Java Language Specification:
A marker annotation is a shorthand designed for use with marker annotation types (§9.6.1).
MarkerAnnotation:
# TypeName
It is shorthand for the normal annotation:
#TypeName()
It is legal to use marker annotations for annotation types with elements, so long as all the elements have default values (§9.6.2).
Example 9.7.2-1. Marker Annotations
Here is an example using the Preliminary marker annotation type from §9.6.1:
#Preliminary public class TimeTravel { ... }
As of Java 8 the #Deprecated annotation had no elements, so it could only ever be a marker annotation. Since Java 9, however, it now has two elements: since and forRemoval. But since those elements have default values the annotation can still be used as a marker annotation.
It's not an interface, it is an annotation. #Deprecated and #Deprecated() are the same
Related
Are there such things as Java annotations that aren't tied to any class, method, field, etc.?
Like just writing
#MyAnnotation(someParameter=value, ...)
by itself, and it generates code.
It seems like ExecutableType might define what kinds of "elements" an annotation can annotate, but I'm not sure. If that's true, then ExecutableType derives from TypeMirror, one of whose members are NoType. So maybe it's possible? But I cannot find an example of this.
You cannot have a stand-alone annotation in Java.
Annotations can be applied to different things, for example: types, methods, fields, local variables, packages, method parameters and also on annotation definitions.
One annotation that is meant to be used on annotation definitions (therefore it's called a "meta-annotation") is #Target, which you use to indicate on what things the annotation you are defining is allowed to be used. You do this by specifying one or more element types as an argument to the #Target annotation - see the API docs of java.lang.annotation.ElementType.
The Java Language Specification paragraph 9.6.4.1 explains what annotations can be used on in more detail.
Hi i wonder if i have to annotate a deprecated method at all their occurrences like, Interface, Base (Abstract Class) and all the Implementation Classes or is one annotation in the Interface sufficient?
Actually depends of how deprecated classes are used. Deprecate an interface (methods or attributes) is enough if you use good practices (instantiating the interface to use the implementation).
Anyway,
you cannot ensure / force this practice in other users
not all compilers/parsers will throw a warning or show javadoc #deprecated tag
Using the #Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the #deprecated Javadoc tag, though the Sun compilers currently do so. Other compilers may not issue such warnings. Thus, using the #Deprecated annotation to generate warnings is more portable that relying on the #deprecated Javadoc tag.
Resuming: if you want to deprecate a method implementation you must deprecate both, interface and implementation.
SOURCES 1 2 3
This question already has an answer here:
How is it possible that an annotation can be an annotation to itself?
(1 answer)
Closed 7 years ago.
I stumbled across into the source code for the "Documented" Java Annotation recently. It looks like this:
#Documented
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.ANNOTATION_TYPE)
public #interface Documented {
}
What the purpose of "#Documented" within Documented itself?
From the documentation:
Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.
#Documented is a meta annotation - it annotates another annotation type. When an annotation type is annotated by #Documented, tools like javadoc should include the annotation when it is used somewhere in the code.
For example, the #Deprecated annotation type is annotated with #Documented. It's pretty important to know if something is deprecated, so #Deprecated is considered part of the public API and should be included in the documentation.
On the other hand, #SuppressWarnings is just a hint for the compiler and not important for the API, so it is not annotated by #Documented.
The #Documented annotation type also annotates itself. Basically that just means that you will see any usage of #Documented in the documentation. This is done so that you can see if any annotation type will be documented or not.
I know this #interface element is used to define annotations in Java.
I know annotations were introduced in Java 5.
My questions:
1) how is that element called (formally), and since when
is it recognized by the compiler i.e. when was it introduced?
Is it an annotation or a meta-annotation itself?
2) before Java 8 (e.g. in Java 5 or 6) was there some other element/way
used for defining annotations or had they always been defined that
way ever since their advent in the language?
how is that element called (formally), and since when is it recognized by the compiler i.e. when was it introduced?
An annotation type declaration. This was added in Java 5.
Is it an annotation or a meta-annotation itself?
No, #interface is not an annotation by itself, it's just the keyword interface preceded by an #. It's not a meta-annotation. A meta-annotation is an annotation that can be used on annotation type declarations. The JDK itself has a number of these (for example #RetentionPolicy).
before Java 8 (e.g. in Java 5 or 6) was there some other element/way used for defining annotations
No.
or they had they always been defined that way ever since their advent in the language?
Yes.
Annotations have been added in Java 5 and #interface was always the way to create annotations; there has not been any other mechanism in the past to create annotations.
Why #interface and not a new keyword such as annotation: This was done for backward compatibility. Adding a new keyword means you immediately create a backward compatibility issue, because programs written for the older version might have used the name of the keyword for example as a variable name. Such programs wouldn't compile anymore on the new version. So, when they want to add a new feature to Java, Oracle prefers re-using an existing keyword instead of creating a new one.
That said, new keywords have been added in the course of the evolution of Java, such as enum and assert (which were added in Java 1.4).
While reading about Marker interfaces I stumbled upon the following site : Item 37: Use marker interfaces to define types
Here, according to Joshua Bloch there are two advantages of Marker interfaces over the Marker annotations.
Marker interfaces define a type that is implemented by instances of
the marked class; marker annotations do not. The existence of this
type allows you to catch errors at compile time that you couldn’t
catch until runtime if you used a marker annotation.
Another advantage of marker interfaces over marker annotations is
that they can be targeted more precisely. If an annotation type is
declared with target ElementType.TYPE, it can be applied to any
class or interface. Suppose you have a marker that is applicable
only to implementations of a particular interface. If you define it
as a marker interface, you can have it extend the sole interface to
which it is applicable, guaranteeing that all marked types are also
subtypes of the sole interface to which it is applicable.
OK, First point is understood but I'm not sure if I understand the 2nd point correctly:
If an annotation type is declared with target ElementType.TYPE, it can be applied to any class or interface.
Likewise, if I have a marker interface then that too can be applied to any class or interface. Isn't it saying the same thing about marker annotations and marker interfaces? How can a marker interface be targeted more precisely?
The 2nd point also mentions that:
you can have [the Marker Interface] extend the sole interface to which it is applicable, guaranteeing that all marked types are also subtypes of the sole interface to which it is applicable.
Can't you also achieve this with annotations, by using the #Inherited meta-annotation?
How can a marker interface be targeted more precisely?
You are correct that both could be applied to any type. By "targeted more precisely" the author means that you can add additional restrictions to which specific types a marker interface can be applied to. It is not possible to add the same precise restrictions to annotations: If an annotation is restricted to ElementType.TYPE, then it can always be applied to all types.
The other part of the 2nd point goes into details how you can add those restrictions. If you have a marker interface, you can let it extend another interface (which the author calls the sole interface) like this:
interface Marker extends Foo { }
The marker can now only be applied to types which implement Foo.
Can't you also achieve this with annotations, by using the #Inherited meta-annotation?
No, the #Inherited meta-annotation only means that any subtype of an annotated class will be treated as if it also had the same annotation. It does not impose any restrictions to which types the annotation can be applied to.