簡體   English   中英

Java:如何調用在接口中注釋和聲明的方法

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM