Skip to content

Instantly share code, notes, and snippets.

@xudifsd
Created December 27, 2013 06:09
Show Gist options
  • Save xudifsd/8143210 to your computer and use it in GitHub Desktop.
Save xudifsd/8143210 to your computer and use it in GitHub Desktop.
annotation的使用
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.NoSuchMethodException;
public class T {
public static void main(String[] args) throws Exception {
executeWithAnnotationSupport(T.class, "doSomething");
}
@InProgress
public static void doSomething() {
}
public static void executeWithAnnotationSupport(Class<?> clazz, String methodName) throws Exception {
Method[] ms = clazz.getMethods();
for (Method m : ms) {
if (m.getName().equals(methodName)) {
InProgress inProgress = m.getAnnotation(InProgress.class);
if (inProgress != null) {
System.out.format("method '%s' for class '%s' is still work in progress\n", methodName, clazz.getName());
}
}
}
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface InProgress {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment