views:

44

answers:

3

java.lang.annotation.ElementType:

A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Target meta-annotation type to specify where it is legal to use an annotation type.

There are the following constants:

  • ANNOTATION_TYPE - Annotation type declaration
  • CONSTRUCTOR - Constructor declaration
  • FIELD - Field declaration (includes enum constants)
  • LOCAL_VARIABLE - Local variable declaration
  • METHOD - Method declaration
  • PACKAGE - Package declaration
  • PARAMETER - Parameter declaration
  • TYPE - Class, interface (including annotation type), or enum declaration

Can someone explain what each of them are (where they'd be annotated in actual code)?

+2  A: 

Let's say the annotation to which you specify the ElementType is called YourAnnotation:

  • ANNOTATION_TYPE - Annotation type declaration. Note: This goes on other annotations

    @YourAnnotation
    public @interface AnotherAnnotation {..}
    
  • CONSTRUCTOR - Constructor declaration

    public class SomeClass {
        @YourAnnotation
        public SomeClass() {..}
    }
    
  • FIELD - Field declaration (includes enum constants)

    @YourAnnotation
    private String someField;
    
  • LOCAL_VARIABLE - Local variable declaration. Note: This can't be read at runtime, so it is used only for compile-time things, like the @SuppressWarnings annotation.

    public void someMethod() {
        @YourAnnotation int a = 0;
    }
    
  • METHOD - Method declaration

    @YourAnnotation
    public void someMethod() {..}
    
  • PACKAGE - Package declaration. Note: This can be used only in package-info.java.

    @YourAnnotation
    package org.yourcompany.somepackage;
    
  • PARAMETER - Parameter declaration

    public void someMethod(@YourAnnotation param) {..}
    
  • TYPE - Class, interface (including annotation type), or enum declaration

    @YourAnnotation
    public class SomeClass {..}
    

You can specify multiple ElementTypes for a given annotation. E.g.:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
Bozho
`@Bozho:` Thanks for the pro tip. I check-marked the thing below the vote counter.
Action Jackson
+1  A: 

TYPE:

Annotation:

@Target({ElementType.TYPE})    // This annotation can only be applied to
public @interface Tweezable {  // class, interface, or enum declarations.
}

and an example usage:

@Tweezable
public class Hair {
    ...
}
Action Jackson
A: 

It seems pretty obvious, but...

@BlahType
public class MyAnnotatedClass {
  @BlahField
  private String foo;

  @BlahConstructor
  public MyAnnotatedClass() {
  }

  @BlahMethod
  public String bar(@BlahParameter String str) {
    @BlahLocalVariable String asdf = "asdf";
    return asdf + str;
  }
}

Then ANNOTATION_TYPE is just on an annotation definition, and package is defined in a package-info.java file in the package, see here.

Javid Jamae
Oops, didn't see the other answers until after I posted.
Javid Jamae