Java annotation: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 1:
In the [[Java (programming language)|Java computer programming language]], an '''annotation''' is a form of syntactic [[metadata]] that can be added to Java [[source code]].<ref>{{cite web|url = http://download.oracle.com/javase/1,5.0/docs/guide/language/annotations.html|title = Annotations|accessdate = 2011-09-30|publisher = [[Sun Microsystems]]}}.</ref> [[Class (computer programming)|Classes]], [[Method (computer programming)|methods]], [[Variable (computer science)|variables]], [[Parameter (computer programming)|parameters]] and [[Java package]]s may be annotated. Like [[Javadoc]] tags, Java annotations can be read from source files. Unlike [[Javadoc]] tags, Java annotations can also be embedded in and read from [[Java class (file format)|class files]]s generated by the [[Java compiler]]. This allows annotations to be retained by the [[Java virtual machine]] at [[Run time (program lifecycle phase)|run-time]] and read via [[reflection (computer science)|reflection]].<ref>{{Cite book|title = Java(TM) Language Specification|edition = 3rd|publisher = [[Prentice Hall]]|year = 2005|isbn = 0-321-24678-0|url = http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html|author = [[Sun Microsystems]]|postscript = <!--None-->}}.</ref> It is possible to create meta-annotations out of the existing ones in Java.<ref>{{cite web
|url=http://www.25hoursaday.com/
|title=A COMPARISON OF MICROSOFT'S C# PROGRAMMING LANGUAGE TO SUN MICROSYSTEMS' JAVA PROGRAMMING LANGUAGE: Metadata Annotations
Line 24:
 
== Built-in annotations ==
Java defines a set of annotations that are built into the language. Of the seven standard annotations, three are part of [[java.lang]], and the remaining four are imported from java.lang.annotation.<ref>{{cite web
| url = https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html
| title = Predefined Annotation Types
Line 36:
 
'''Annotations applied to Java code:'''
* <code>@Override</code> - Checks that the method is an [[Method overriding|override]]. Causes a compile[[compilation error]] if the method is not found in one of the [[parent classesclass]]es or implemented [[Interface (Java)|interfaces]].
* <code>@Deprecated</code> - Marks the method as obsolete. Causes a compile warning if the method is used.
* <code>@SuppressWarnings</code> - Instructs the compiler to suppress the [[compile time]] warnings specified in the annotation parameters.
Line 42:
'''Annotations applied to other annotations (also known as "Meta Annotations"):'''
 
* <code>@Retention</code> - Specifies how the marked annotation is stored—Whetherstored, whether in code only, compiled into the class, or available at runtime through reflection.
* <code>@Documented</code> - Marks another annotation for inclusion in the documentation.
* <code>@Target</code> - Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
Line 49:
Since Java 7, three additional annotations have been added to the language.
 
* <code>@SafeVarargs</code> - Suppress warnings for all callers of a method or constructor with a [[Generics in Java|generics]] [[Variadic_function|varargs]] parameter, since Java 7.
* <code>@FunctionalInterface </code> - Specifies that the [[Declaration (computer programming)|type declaration]] is intended to be a [[Anonymous function|functional interface]], since Java 8.
* <code>@Repeatable</code> - Specifies that the annotation can be applied more than once to the same declaration, since Java 8.
 
Line 83:
=== Custom annotations ===
 
Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface [[Reserved word|keyword]]. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to [[Primitive data type|primitives]], [[String (computer science)|String]], Class, [[Enumerated type|enums]], annotations, and [[Array data type|arrays]] of the preceding types. Methods can have [[Default (computer science)|default values]].
 
<source lang=Java>