Java 注释-使用反射读取元素值
是否可以使用反射读取注释元素的值?我想使用元素名称的字符串访问元素的值。这可能吗 注释:Java 注释-使用反射读取元素值,java,reflection,annotations,Java,Reflection,Annotations,是否可以使用反射读取注释元素的值?我想使用元素名称的字符串访问元素的值。这可能吗 注释: 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)
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();
我得到一个nosuchfield例外:
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()的输出:
对。你差点就成功了。到达此部分后:
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
无论你想做什么,如果你使用的是反射,你可能是设计过度了。一个更恰当的问题可能是,“如何在没有思考的情况下改变我的解决方案来解决问题X?”然后描述问题X实际上是什么。
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