[英]Java: How do I invoke methods that are annotated and declared within an interface
File A.java:文件 A.java:
public interface A {
@Property(propertyName = "empoloyee.name")
String getEmployeeName();
@Property(propertyName = "employee.age")
String getEmployeeAge();
}
File B.java:文件 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());
}
}
}
Property.class:属性类:
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 "";
}
As shown in the above code snippet, my requirement is to invoke all the methods declared in A.class, in a loop.如上面的代码片段所示,我的要求是循环调用 A.class 中声明的所有方法。
Since these methods are annotated, just invoking them will return the property value.由于这些方法是带注释的,因此只需调用它们就会返回属性值。 method.invoke() is giving me error. method.invoke() 给了我错误。 How do I resolve this issue?我该如何解决这个问题?
method.invoke needs object to pass for invoking the method so you will have pass an object of any class implementing interface A, like below - method.invoke 需要传递对象来调用方法,因此您将传递任何实现接口 A 的类的对象,如下所示 -
public class C implements A{
@Override
public String getEmployeeName() {
return "xyz";
}
@Override
public String getEmployeeAge() {
return "22";
}
}
Then your main method will be like this -那么你的主要方法将是这样的 -
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));
}
}
I did some modifications to your code and I am hoping it helps.我对您的代码做了一些修改,希望对您有所帮助。 first of all, A is not a class it is just an interface and has methods with nobody, so in my case, I made an Employee class that implements the A interface:首先, 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;
}
}
then inside Class BI called the Employee class然后在 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));
}
}
}
Property Annotation:属性注释:
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 interface:一个接口:
// 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();
}
I hope this answer your question.我希望这能回答你的问题。 thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.