使用反射访问 Java 数组中的注释

huangapple go评论68阅读模式
英文:

Accessing java comment in array using reflection

问题

私有静态常量字符串数组 STRINGS 如下所示:

private static final String[] STRINGS = {
    // 在这里放置要测试的日志行
    "a",
    // Jira票据1
    "b",
    // Jira票据2
    "c"
};

基本上,我正在构建的代码将访问这个类并复制 STRINGS 数组。是否可能使用反射来获取这个包含嵌套注释的 STRINGS 数组?我不认为这是可能的,但我想问一下是否有不同的方向我可能可以采取。这些注释对于跟踪更改非常有用,我正试图将其用作保留信息的一种方式。

英文:

Imagine there is an array variable like the following:

private static final String[] STRINGS = {
        // Put log lines to test here
        "a",
        // Jira ticket 1
        "b",
        // Jira ticket 2
        "c"	
};

Essentially I am building code that would go to this class and copy the STRINGS array. Would it possible to use reflection to get this STRINGS array with the nested comments? I don't think this is possible but I wanted to ask if there are different directions I could possibly take. The comments are useful to tracking changes and I am trying to use this as a way to keep that information.

答案1

得分: 1

某些内容在编译后会消失。某些泛型和所有注释都不会出现在类文件中,在运行时源文件也不会存在,所以答案很简单:不可能

有一些替代方法。例如,您可以在部署时将源文件打包(打包到JAR中),然后解析这些源文件,但解析Java代码显然是相当复杂的,所以我真的不推荐这种方法。

您还可以将这些注释放入简单的文本文件中,然后打包到项目中,这是一个更好的方案;您可以使用MyClassName.class.getResourceAsStream("jira-ticket-assignments.txt")来获取包含此数据的输入流,只要名为jira-ticket-assignments.txt的文件与MyClassName.class在完全相同的位置上(即使在JAR文件中也没有问题)。

除此之外,您还可以将这些注释转换为字符串文字:

private static final Map<String, String> STRINGS = Map.of(
    "a", "Jira ticket 1",
    "b", "Jira ticket 2",
    "c", "");
英文:

Certain things just disappear once compiled. Some generics, and all comments, just are not in that class file, and the source file is not there at runtime, so, the answer is simple: impossible.

There are some alternatives. For example, you could ship the source file (pack into the jar as you deploy it), and then parse the sources, but parsing java code is decidedly non-trivial, so I really don't recommend this route.

You can also put these comments in simple text files, and pack those in, that's a much better plan; you can use MyClassName.class.getResourceAsStream(&quot;jira-ticket-assignments.txt&quot;) to get an inputstream with this data, and that works as long as a file named jira-ticket-assignments.txt is in the exact same place MyClassName.class is (even in a jar file, not a problem).

It's that, or turn those comments into string literals instead:

private static final Map&lt;String, String&gt; STRINGS = Map.of(
    &quot;a&quot;, &quot;Jira ticket 1&quot;,
    &quot;b&quot;, &quot;Jira ticket 2&quot;,
    &quot;c&quot;, &quot;&quot;);

答案2

得分: 0

正如 @rzwitserloot 在上面所说,某些东西在编译后就会消失。

如果您需要在运行时保存一些元信息,您可以使用注解:

@Comment({
    "在此处放置测试用的日志行",
    "Jira 工单 1",
    "Jira 工单 2"})
private static final String[] STRINGS = {
    "a",
    "b",
    "c"
};

为了满足这个需求,需要创建一个注解:

@Retention(RetentionPolicy.RUNTIME)
public @interface Comment {
    String[] value();
}

并在运行时使用:

String[] comments = SomeClass.class.getDeclaredField("STRINGS").getAnnotation(Comment.class).value();
String firstElementComment = comments[0];
String secondElementComment = comments[1];
// ...
英文:

As @rzwitserloot said above, certain things just disappear once compiled.
If you need to save some meta information in runtime, you can use annotations

@Comment({
    &quot;Put log lines to test here&quot;,
    &quot;Jira ticket 1&quot;,
    &quot;Jira ticket 2&quot;})
private static final String[] STRINGS = {
    &quot;a&quot;,
    &quot;b&quot;,
    &quot;c&quot;
};

For this need to create annotation:

@Retention(RetentionPolicy.RUNTIME)
public @interface Comment {
    String[] value();
}

And use in runtime:

String[] comments = SomeClass.class.getDeclaredField(&quot;STRINGS&quot;).getAnnotation(Comment.class).value();
String firstElementComment = comments[0];
String secondElementComment = comments[1];
...

huangapple
  • 本文由 发表于 2020年10月2日 06:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64163809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定