Java Annotations

by J. Benton





Example 1: 



/** Deprecating a method using the annotation. @author J. Benton */

public class DeprecateExample {

  // if you use this method, you'll get a warning that it is deprecated.

  @Deprecated

  public void ohNoIAmDeprecated() {

     System.out.println("I am deprecated!");

  }

}





Example 2: 



import java.util.ArrayList;

/** Suppressing warnings. @author J. Benton */

public class UncheckedExample {

  public void uncheckedGenerics() {

    ArrayList blahblah = new ArrayList();

    // warning, warning, warning!!  unchecked call below!

    blahblah.add(2);

  }

}





Example 3:



import java.util.ArrayList;



/** Suppressing warnings. @author J. Benton */

// You can use so-called single-valued annotations

// just like regular annotations if you want (note

// how I am specifying the method name here, though

// it is unnecessary).

@SuppressWarnings(value={"unchecked"})

public class UncheckedExample {

  public void uncheckedGenerics() {

    ArrayList blahblah = new ArrayList();

    // no warning!  I suppressed it!

    blahblah.add(2);

  }

}





Example 4:



package com.ddj;

import java.util.*;



/** Using the @Override annotation. @author J. Benton */

public class OverrideExample implements Collection {

  // ...

  /** This won't work!  I'm not overriding anything here!

   * This is a new method--not one being overridden.

   * @param index The index of the thing to get.

   * @see java.util.List#get(int)

   */

  @Override

  public Object get(int index) {

    Object value = null;

    // code to get stuff.

    return value;

  }

  // ...

}





Listing One



/* ApplicationException.java * Created on Jan 2, 2005  */

package com.ddj.annotations;



import java.lang.annotation.*;



/** This annotation is used by the annotation processing tool (apt)  

* to generate an exception. * @author J. Benton */

@Retention(RetentionPolicy.SOURCE)

// I give an empty array to "@Target" to say that this annotation

// type should be used only as an element of other annotations

// (you cannot use this to annotate anything directly).

@Target({})

public @interface ApplicationException {

  /** What I want to call this exception (sans the word "Exception".*/

  String exceptionName();

  /** I must add extra information to this exception to make it useful. */

  String addedInformationType() default "";

  /** I should call the "extra information" something that is recongizable if 

   * you try to read the generated exception class. */

  String addedInformationVariableName() default "";

}





Listing Two



/* ApplicationExceptions.java * Created on Jan 2, 2005 */

package com.ddj.annotations;



import java.lang.annotation.*;



/** This was made so I can generate a set of exceptions for an application 

 * on the main application class. * @author J. Benton */

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.SOURCE)

@Documented

public @interface ApplicationExceptions {

  /** An array of ApplicationExceptions. I do things this way because we can't 

   * have more than one instance of the same annotation type on any item. */

  ApplicationException[] applicationExceptions();

}





Listing Three



/* ExceptionAnnotationTest.java * Created on Jan 3, 2005 */

package com.ddj;



import com.ddj.annotations.*;



/** Use this to test the annotations. * @author J. Benton  */

@ApplicationExceptions(

  applicationExceptions={@ApplicationException(exceptionName="Test",

      addedInformationType="int",

      addedInformationVariableName="status"), 

   @ApplicationException(exceptionName="App")})

public class ExceptionAnnotationTest {

}









3



