annotations : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

annotations
Used in the form @interface or @Retention. These are annotations, a full-fledge feature of  Java version 1.6 or later, though they were partly implemented in Java version 1.5 (supporting standard annotations without custom annotations). You can use them to add metadata to your programs useful to various utilities that process the text, e.g. for persistent object store implementations. They can also be used for inserting boiler plate in programs. You use them as you would the attribute static, declaring a class, variable or method to be marked with a given annotation.
The Basics Checker-Framework
@SuppressWarnings Learning More
@Override Links

The Basics

These @annotations not Javadoc comment tags! They are part of the program proper.

@Retention(RetentionPolicy.RUNTIME) means the following annotation should be retained in the class files.

@Target(ElementType.METHOD) means the following annotation only applies to methods in the class, not the whole class.

@Documents means the following annotation should show up in the Javadoc.

Now that you have defined it, you can annotate any class with it, which embeds the copyright notice in the class file, with otherwise changing the class file. It does not add any methods or fields to your class, though it is sometimes possible to get at the information through the java.lang.annotation package methods. Here is how you use it:

@SuppressWarnings

In Java version 1.5, Oracle introduced an annotation called @SuppressWarnings. Unfortunately, it is ignored prior to JDK (Java Development Kit) 1.5.0_06. You just insert the annotation prior to a class or method declaration telling which sort of warning you want suppressed. Further, you are not permitted to use them if you have -target 1.4 or earlier, not even @SuppressWarnings. You use them like this:

As well as classes and methods, @SuppressWarnings also applicable to enum, interface, constructor, field, parameter and local variable declarations.

Annotations do not inherit.

Here are the sorts of warning you can suppress:

@SuppressWarnings Options
Option Notes
all @SuppressWarnings( all ) means suppress all warnings
allDeprecation deprecation even inside deprecated code
allJavadoc invalid or missing Javadoc
assertIdentifier occurrence of assert used as identifier
boxing autoboxing conversion
cast cast operations
charConcat when a char array is used in a string concatenation without being converted explicitly to a string
conditionAssign possible accidental Boolean assignment
constructorName method with constructor name
dep-ann missing @Deprecated annotation
deprecation usage of deprecated type or member outside deprecated code
emptyBlock undocumented empty block
enumSwitch incomplete enum switch
incomplete-switch incomplete switch
fallthrough Mark that fallthrough in switch cases was deliberate, not just a missing break.
fieldHiding field hiding another variable
finalBound type parameter with final bound
finally finally block not completing normally
hiding shorthand for fieldHiding, localHiding, typeHiding and maskedCatchBlock
indirectStatic indirect reference to static member
intfAnnotation annotation type used as super interface
intfNonInherited interface non-inherited method compatibility
Javadoc invalid Javadoc
localHiding local variable hiding another variable
maskedCatchBlocks hidden catch block
nls non-nls string literals
noEffectAssign for assignment with no effect
null missing or redundant null check
over-ann missing @Override annotation
pkgDefaultMethod attempt to override package-default method
rawtypes un-specific types when using generics on class params
restriction usage of discouraged or forbidden references
serial missing serialVersionUID
semicolon unnecessary semicolon or empty statement
specialParamHiding constructor or setter parameter hiding another field
static-access shorthand for indirectStatic and staticReceiver
staticReceiver if a non static receiver is used to get a static field or call a static method
suppress enable @SuppressWarnings
syntheticAccess when performing synthetic access for innerclass
synthetic-access when performing synthetic access for innerclass
tasks enable support for tasks tags in source code
typeHiding type parameter hiding another type
unchecked unchecked type operation
unnecessaryElse unnecessary else clause
unqualified-field-access unqualified reference to field
unqualifiedField unqualified reference to field
unused shorthand for unusedArgument, unusedImport, unusedLocal, unusedPrivate and unusedThrown
unusedArgument unused method argument
unusedImport unused import reference
unusedLocal unused local variable
unusedPrivate unused private member declaration
unusedThrown unused declared thrown exception
uselessTypeCheck unnecessary cast/instanceof operation
varargsCast varargs argument need explicit cast
warningToken unhandled warning token in @SuppressWarnings

Some of the options above are for Eclipse only, however, Javac will quietly ignore them.

@Override

There is no override keyword to warn the compiler your method is intended to override a method in the base class. However, since Java version 1.5 or later, there is an @Override annotation. @Override serves as documentation for the reader and a double check in the compiler. The compiler will warn you if your method is not overriding properly, usually because the two method names are not spelled exactly the same way. Note it is @Overridewith capital O and double r, not @overide. Don’t confuse the @Override annotation with the unofficial @override Javadoc command. Here is how you would use the @Override annotation to indicate that a method is overriding another in the base class. Note it is not inside the Javadoc comment!

/**
 * Get String presentation for this Thing object.
 * @return human readable summary of fields in Thing object.
 */
@Override
public String toString()
   {
   return desc + " " + quantity + " " + losses;
   }

Checker-Framework

The university of Washington has put out a huge library of annotations called Checker-Framework. It is not built into Javac as I was first lead to believe. You must install it first in a rather daunting procedure. It works with ANT, Maven, IntelliJ, Eclipse and others, but not NetBeans.

It supports fake enums, using fenums, enums checked only at compile time, implemented with ordinary ints and hence backward compatible with code written before the invention of enums using @Fenum.

It checks your regex and format string syntax at compile time with @Regex and @Format.

It handles consistency of units of measure, but not automatic conversion of units, with annotations such as: @Area, @Current, @Length (@m, @mm, @km ), @Luminance, @Mass, @Speed, @Substance, @Temperature and @Time. You could spend weeks exploring the smorgasbord of tools included.

Learning More

Oracle’s Javadoc on Class.isAnnotationPresent : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/annotations.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\annotations.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.239.59.193]
You are visitor number