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();