运行动态class加载时的时间注释扫描
run time annotation scanning when Dynamic class loading
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
String str();
int val();
}
public class PackageDemo {
// set values for the annotation
@Demo(str = "Demo Annotation", val = 100)
// a method to call in the main
public static void example() {
PackageDemo ob = new PackageDemo();
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
// print the annotation
System.out.println(annotation.str() + " " + annotation.val());
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
public static void main(String args[]) {
example();
}
}
我的objective是检查几个方法上的注解,如果注解上存在,我需要获取注解。
Demo annotation = m.getAnnotation(Demo.class);
在上面的例子中,注释是在同一个文件中声明的。如果注释在不同的包中,我可以做类似
import com.this.class.DemoClass
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
但是如果我想像
那样动态加载 DemoClass/AnnotationClass
Class<?> Demo = Class.forName("com.this.class.DemoClass")
如何获取方法上的注释。我认为下面的行在这种情况下不起作用
Demo annotation = m.getAnnotation(Demo.class);
当注释动态加载到变量 Demo
时,然后使用该变量获取注释:
Class<?> Demo = Class.forName("com.this.class.DemoClass");
Demo annotation = m.getAnnotation(Demo);
这种方法对我有用。希望这对某人有帮助。
DemoClass= (Class<Annotation>) Class.forName("com.this.class.DemoClass");
if (method.isAnnotationPresent(DemoClass)) {
for (Annotation annotation : method.getAnnotations()) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.getName() == "com.this.class.DemoClass") {
for (Method annotationMethod : annotationType.getDeclaredMethods()) {
value= annotationMethod.invoke(annotation, (Object[]) null);
}
}
}
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
String str();
int val();
}
public class PackageDemo {
// set values for the annotation
@Demo(str = "Demo Annotation", val = 100)
// a method to call in the main
public static void example() {
PackageDemo ob = new PackageDemo();
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
// print the annotation
System.out.println(annotation.str() + " " + annotation.val());
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
public static void main(String args[]) {
example();
}
}
我的objective是检查几个方法上的注解,如果注解上存在,我需要获取注解。
Demo annotation = m.getAnnotation(Demo.class);
在上面的例子中,注释是在同一个文件中声明的。如果注释在不同的包中,我可以做类似
import com.this.class.DemoClass
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotation for class Demo
Demo annotation = m.getAnnotation(Demo.class);
但是如果我想像
那样动态加载 DemoClass/AnnotationClassClass<?> Demo = Class.forName("com.this.class.DemoClass")
如何获取方法上的注释。我认为下面的行在这种情况下不起作用
Demo annotation = m.getAnnotation(Demo.class);
当注释动态加载到变量 Demo
时,然后使用该变量获取注释:
Class<?> Demo = Class.forName("com.this.class.DemoClass");
Demo annotation = m.getAnnotation(Demo);
这种方法对我有用。希望这对某人有帮助。
DemoClass= (Class<Annotation>) Class.forName("com.this.class.DemoClass");
if (method.isAnnotationPresent(DemoClass)) {
for (Annotation annotation : method.getAnnotations()) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.getName() == "com.this.class.DemoClass") {
for (Method annotationMethod : annotationType.getDeclaredMethods()) {
value= annotationMethod.invoke(annotation, (Object[]) null);
}
}
}