简体   繁体   English

Java:如何调用在接口中注释和声明的方法

[英]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.

相关问题 如何在 Java 中使用反射 api 调用 testng 带注释的方法,并为此获取 testng 报告? - How do I invoke testng annotated methods using reflection api in java and also obtain the testng report for that? Spring AOP用于接口和其中的注释方法 - Spring AOP for interface and for annotated methods within it 如何反射性地调用 Java 8 默认方法 - How do I invoke Java 8 default methods reflectively 如何调用IDL接口上未指定的方法? [Corba,JAVA] - How can I invoke methods which are not specified on the IDL interface ? [Corba, JAVA] 如何在Scala中扩展包含泛型方法的Java接口? - How do I extend Java interface containing generic methods in Scala? 如何在运行时在 Scala/Java 中找到带注释的方法 - How can I find Annotated methods in Scala/Java on Runtime 如何使用多个@Transactional 注解的方法? - How do I use multiple @Transactional annotated methods? Java 库如何检测带注释的方法? - How can a Java library detect annotated methods? 如何调用带有@Component 注释的 Spring Java 方法? - How do i call a Spring Java method annotated with @Component? 在Java中,如何为接口数组定义不同的方法? 在没有Java库的情况下使用哈希表 - in Java, how do I define different methods for an interface array? Using hash tables without java library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM