java - 使用 Apache Bcel 获取注释名称
问题描述
我正在使用此代码来获取 Java 类注释:
JavaClass jclas = new ClassParser("src\\test\\org\\poc\\TestClass.class").parse();
ClassGen cg = new ClassGen(jclas);
AnnotationEntryGen[] attributes = cg.getAnnotationEntries();
for (AnnotationEntryGen attribute : attributes) {
System.out.println("attribute: " + attribute);
if(attribute.getTypeName().contains("Fix")) {
// Do something
}
}
但是当我打印时,attribute.getTypeName()
我得到Lorg/annotations/Fix
. 注释名称是@Fix(..)
Do you know how I can get only the name?
解决方案
像 `"Lorg/annotations/Fix" 这样的字符串是一个字段描述符。Java 有许多不同的类名格式。
您可以使用反射实用程序包的Signatures
类在名称的不同格式之间进行转换。
注释名称是
@Fix(..)
.
该术语不正确;您已经展示了完整的注解是如何用 Java 源代码编写的,但这不是它的名称。 getTypeName()
确实返回了它的名称(以字段描述符格式)。
如果您想要注解的元素(当注解用 Java 源代码编写时出现在括号之间),请使用getValues()
. 您可能还会发现toString()
有用。
推荐阅读
- entity-framework - 从对象的子列表中获取列表
- c# - 我可以依赖整个项目中枚举选项值的唯一性吗?
- java - 构造函数代码“学生班级错误”有问题吗?
- powerbuilder - 如何以正确的方式填充饼图?[电力建设者]
- sql-tuning - 查询调优和重写 - SQL Server
- javascript - 我们不能将状态传递给 reactchart 数据栏:数据集:数据吗?
- mongodb - 如何使大型 MongoDB 请求更快
- s4sdk - VDM 适用于一种 Odata 服务,不适用于另一种
- c# - 如何从 COM 类转换接口
- ios - Xcode 10 无法构建项目,因为无法找到 pod