注释 - 使用反射读取元素值

时间:2018-03-09 21:46:39

标签: java reflection annotations

是否可以使用反射读取注释元素的值?我想使用元素名称的String访问元素的值。这甚至可能吗?

注释:

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target({CONSTRUCTOR, METHOD, TYPE})
public @interface TestCase {
    String brand1() default "";
    String brand2() default "";
    String brand3() default "";
}

使用示例:

@TestCase(brand1 = "1001", brand2 = "1101", brand3 = "1201")
@Test
public void testStep() {
    // run test here
}

我目前使用开关提取元素的值:

TestCase testCase = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(TestCase.class);
switch (Brand brand) { // Brand is an enum value
    case BRAND1:
        testCaseId = testCase.brand1();
        break;
    case BRAND2:
        testCaseId = testCase.brand2();
        break;
    case BRAND3:
        testCaseId = testCase.brand3();
        break;
    default:
        testCaseId = "";
}

我希望做这样的事情:

String sbrand = brand.toString.toLowerCase() // brand is an enum value
String s = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod()
        .getAnnotation(TestCase.class).[iCantFigureThisPartOut](sbrand);

我试过了:

String s = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod()
        .getAnnotation(TestCase.class).getClass().getField("brand1").toString();

我得到NoSuchFieldException:

java.lang.NoSuchFieldException: brand1
    at java.base/java.lang.Class.getField(Class.java:1956)

我试过这些,看看能不能得到任何东西:

Field[] fs = testCase.getClass().getFields();
Field[] dfs = testCase.getClass().getDeclaredFields();
Method[] ms = testCase.getClass().getMethods();
Method[] dms = testCase.getClass().getDeclaredMethods();

但是当我迭代并执行一个System.out.println()时,唯一看起来甚至非常有用的东西是getDeclaredMethods()的输出:

public final boolean com.sun.proxy.$Proxy3.equals(java.lang.Object)
public final java.lang.String com.sun.proxy.$Proxy3.toString()
public final int com.sun.proxy.$Proxy3.hashCode()
public final java.lang.Class com.sun.proxy.$Proxy3.annotationType()
public final java.lang.String com.sun.proxy.$Proxy3.brand1()
public final java.lang.String com.sun.proxy.$Proxy3.brand2()
public final java.lang.String com.sun.proxy.$Proxy3.brand3()

1 个答案:

答案 0 :(得分:1)

是。你几乎拥有它。一旦你到达这个部分:

iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod()
    .getAnnotation(TestCase.class)

它为您提供了一个继承TestCase注释的对象。

TestCase tc = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod()
    .getAnnotation(TestCase.class);
String b1 = tc.brand1(); // returns the value "1001" from your example
String b2 = tc.brand2(); // returns the value "1101" from your example
String b3 = tc.brand3(); // returns the value "1201" from your example

编辑:添加其他手段@SotiriosDelimanolis说。

TestCase tc = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod()
    .getAnnotation(TestCase.class);
Method fm = TestCase.class.getMethod("brand1"); // or brand2 or a variable
String brand = (String) fm.invoke(tc); // returns the value "1001" from your example