![](/img/trans.png)
[英]How do I invoke testng annotated methods using reflection api in java and also obtain the testng report for that?
[英]Java: How do I invoke methods that are annotated and declared within an interface
文件 A.java:
public interface A {
@Property(propertyName = "empoloyee.name")
String getEmployeeName();
@Property(propertyName = "employee.age")
String getEmployeeAge();
}
文件 B.java:
public class B {
public static void main(String[] args) {
for (Method method: A.class.getMethods()) {
Property property = method.getAnnotation(Property.class);
System.out.println("Property Name: " + property.propertyName());
System.out.println("Property Value: " + method.invoke());
}
}
}
属性类:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Property {
String propertyName() default "";
String defaultValue() default "";
}
如上面的代码片段所示,我的要求是循环调用 A.class 中声明的所有方法。
由于这些方法是带注释的,因此只需调用它们就会返回属性值。 method.invoke() 给了我错误。 我该如何解决这个问题?
method.invoke 需要传递对象来调用方法,因此您将传递任何实现接口 A 的类的对象,如下所示 -
public class C implements A{
@Override
public String getEmployeeName() {
return "xyz";
}
@Override
public String getEmployeeAge() {
return "22";
}
}
那么你的主要方法将是这样的 -
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
C c = new C();
for (Method method: A.class.getMethods()) {
Property property = method.getAnnotation(Property.class);
System.out.println("Property Name: " + property.propertyName());
System.out.println("Property Value: " + method.invoke(c));
}
}
我对您的代码做了一些修改,希望对您有所帮助。 首先, A 不是一个类,它只是一个接口,并且没有任何人的方法,所以在我的例子中,我创建了一个实现 A 接口的Employee 类:
public class Employee implements A {
private String employeeName;
private String employeeAge;
public Employee(String name, String age) {
this.employeeName = name;
this.employeeAge = age;
}
public String getEmployeeName() {
// TODO Auto-generated method stub
return employeeName;
}
public String getEmployeeAge() {
// TODO Auto-generated method stub
return employeeAge;
}
}
然后在 BI 类内部称为 Employee 类
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class B {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for (Method method: A.class.getMethods()) {
Employee em = new Employee("Roy", "20");
System.out.println("Property Name: " + em.getEmployeeName());
System.out.println("Property Value: " + method.invoke(em));
}
}
}
属性注释:
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
// this annotation target only methods
@Target(ElementType.METHOD)
public @interface Property {
String propertyName() default "hi";
}
一个接口:
// A is interface not a class
public interface A {
@Property(propertyName = "employee.name")
// this method is abstract: has no body
String getEmployeeName();
@Property(propertyName ="employee.age" )
// for good practice this should be integer instead of string
String getEmployeeAge();
}
我希望这能回答你的问题。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.