Skip to content

Instantly share code, notes, and snippets.

@ytoshima
Created April 23, 2014 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytoshima/11207832 to your computer and use it in GitHub Desktop.
Save ytoshima/11207832 to your computer and use it in GitHub Desktop.
JDK8 Repeating Annotations sample
import java.lang.annotation.Target;
import java.lang.annotation.Repeatable;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
@Target(METHOD)
@Retention(RUNTIME)
@Repeatable(Schedules.class)
public @interface Schedule {
String second() default "0";
String minute() default "0";
String hour() default "0";
String dayOfMonth() default "*";
String month() default "*";
String dayOfWeek() default "*";
String year() default "*";
String timezone() default "";
String info() default "";
boolean persistent() default true;
}
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
@Target(METHOD)
@Retention(RUNTIME)
public @interface Schedules {
Schedule[] value();
}
::::::::::::::
TaskEx.java
::::::::::::::
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TaskEx {
public static void main(String[] args) {
showDoitAnno();
}
static void showDoitAnno() {
try {
Method m = TaskEx.class.getMethod("doit");
Annotation[] annos = m.getAnnotationsByType(Schedule.class);
I("getAnnotationsByType");
for (Annotation an : annos) {
P("annos: " + an);
}
I("getAnnotation");
Annotation a = m.getAnnotation(Schedule.class);
P("anno: " + a);
} catch (Exception e) {
e.printStackTrace();
}
}
@Schedule(dayOfMonth="Last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doit() {
}
static void P(Object o) { System.out.println(o); }
static void I(Object o) { System.out.println("I: " + o); }
static void W(Object o) { System.out.println("I: " + o); }
static void E(Object o) { System.out.println("I: " + o); }
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TaskEx {
public static void main(String[] args) {
showDoitAnno();
}
static void showDoitAnno() {
try {
Method m = TaskEx.class.getMethod("doit");
Annotation[] annos = m.getAnnotationsByType(Schedule.class);
I("getAnnotationsByType");
for (Annotation an : annos) {
P("annos: " + an);
}
I("getAnnotation");
Annotation a = m.getAnnotation(Schedule.class);
P("anno: " + a);
} catch (Exception e) {
e.printStackTrace();
}
}
@Schedule(dayOfMonth="Last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doit() {
}
static void P(Object o) { System.out.println(o); }
static void I(Object o) { System.out.println("I: " + o); }
static void W(Object o) { System.out.println("I: " + o); }
static void E(Object o) { System.out.println("I: " + o); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment