如何使用JDK8和JDK11阅读的Java注释的价值?
import io.cucumber.java.en.When;
public class Sof {
private static final Logger log = LoggerFactory.getLogger(Sof.class);
@When(value = "I update text {string} with {string}(\\?)")
public static void main(String[] args) {
Class c = Sof.class;
Method[] methods = c.getMethods();
Method method = null;
for (Method m : methods) {
if (m.getName().equals("main")) {
method = m;
}
}
Annotation stepAnnotation = method.getAnnotation(When.class);
Object as[] = { "a", "b" };
Matcher matcher = Pattern.compile("value=(.*)\\)").matcher(stepAnnotation.toString());
if (matcher.find()) {
log.info("---> " + stepAnnotation.annotationType().getSimpleName() + " " + String.format(matcher.group(1).replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), as));
} else {
System.err.println("error");
}
}
}
/!\在现实中,我不知道注释的类型@When
。这可以是任何的接口在io.cucumber.java包
导致JDK8:
---> When I update text {a} with {b}
导致JDK11(额外报价)(stepAnnotation.toString()
是不同的!)
---> When "I update text {a} with {b}"
编辑 openjdk11
和oraclejdk11
不尊重的javadoc:
/**
* Returns a string representation of this annotation. The details
* of the representation are implementation-dependent, but the following
* may be regarded as typical:
* <pre>
* @com.acme.util.Name(first=Alfred, middle=E., last=Neuman)
* </pre>
*
* @return a string representation of this annotation
*/
String toString();
你不应该依赖于toString()
它通常是用于调试/只记录执行。
查看是否有可能读取Java中的注释的价值?关于如何阅读批注的价值更多的细节。
更新:
要通过反射做的一切,你可以使这样的事情:
import org.springframework.transaction.annotation.Transactional;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class AnnotationTest {
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
Method[] methods = AnnotationTest.class.getMethods();
System.out.println("methods = " + Arrays.toString(methods));
for (Method method : methods) {
System.out.println("method = " + method);
Annotation[] annotations = method.getAnnotations();
System.out.println("annotations = " + Arrays.toString(annotations));
for (Annotation annotation : annotations) {
System.out.println("annotation = " + annotation);
Class<? extends Annotation> annotationClass = annotation.annotationType();
System.out.println("annotationClass = " + annotationClass);
Method[] annotationMethods = annotationClass.getMethods();
System.out.println("annotation methods = " + Arrays.toString(annotationMethods));
for (Method annotationMethod : annotationMethods) {
if (Modifier.isPublic(annotationMethod.getModifiers())) {
String name = annotationMethod.getName();
Object o = annotationMethod.invoke(annotation);
System.out.println(name + ": " + o);
}
}
}
}
}
@Transactional("bla")
public void test() {
}
}
(我这里使用的Spring的注解之一,因为这是我碰巧有我的类路径)
UPDATE(具有溶液端):
@When(value = "I update text {string} with {string}(\\?)")
public static void main(String[] args) {
Object as[] = { "a", "b" };
Class c = Sof.class;
Method[] methods = c.getMethods();
Method method = null;
for (Method m : methods) {
if (m.getName().equals("main")) {
method = m;
}
}
Annotation stepAnnotation = method.getAnnotation(When.class);
Class<? extends Annotation> annotationClass = stepAnnotation.annotationType();
try {
Method valueMethods = annotationClass.getDeclaredMethod("value");
if (Modifier.isPublic(valueMethods.getModifiers())) {
log.info("---> {} " + String.format(valueMethods.invoke(stepAnnotation).toString().replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), as),
stepAnnotation.annotationType().getSimpleName());
}
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
e1.printStackTrace();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句