Created
April 23, 2014 08:58
-
-
Save ytoshima/11207832 to your computer and use it in GitHub Desktop.
JDK8 Repeating Annotations sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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