Java - Annotation Member Class Type

Introduction

The following code shows how to use Class as annotation element value.

Demo

import java.io.IOException;

@interface TestCase {
  Class<? extends Throwable> willThrow() default Exception.class;
}

public class Main {
  // Must throw IOExceptionn
  @TestCase(willThrow = IOException.class)
  public static void testCase1() {
    // Code goes here
  }//from ww w . java  2  s  .c  o m

  // We are not expecting any exception
  @TestCase()
  public static void testCase2() {
    // Code goes here
  }
}