如何找到具有已知注释的JavaBean属性
我有一系列不相关的课程。每个类都有一个属性和@PrimaryKey(带getter和setter),可以是任何类型。如何使用反射来查找任何类的实例的哪个属性具有@PrimaryKey注释,以便以字符串的形式获取其值如何找到具有已知注释的JavaBean属性,java,reflection,annotations,Java,Reflection,Annotations,我有一系列不相关的课程。每个类都有一个属性和@PrimaryKey(带getter和setter),可以是任何类型。如何使用反射来查找任何类的实例的哪个属性具有@PrimaryKey注释,以便以字符串的形式获取其值 代码不知道传递的是哪种类型的类-它只是“Object”类型。您可以这样做: for (Field field : YourClass.class.getDeclaredFields()) { try { Annotation annotation = fiel
代码不知道传递的是哪种类型的类-它只是“Object”类型。您可以这样做:
for (Field field : YourClass.class.getDeclaredFields()) {
try {
Annotation annotation = field.getAnnotation(PrimaryKey.class);
// what you want to do with the field
} catch (NoSuchFieldException e) {
// ...
}
}
instance.getClass().getDeclaredFields()
如果您使用的是类的实例,则可以执行此操作以获取其类
对象:
Class<?> clazz = instance.getClass();
如果你遇到麻烦,你可以随时向官员询问。我相信这很好。你可以这样做:
for (Field field : YourClass.class.getDeclaredFields()) {
try {
Annotation annotation = field.getAnnotation(PrimaryKey.class);
// what you want to do with the field
} catch (NoSuchFieldException e) {
// ...
}
}
instance.getClass().getDeclaredFields()
如果您使用的是类的实例,则可以执行此操作以获取其类
对象:
Class<?> clazz = instance.getClass();
如果你遇到麻烦,你可以随时向官员询问。我相信这很好。您可以获取一个类的所有字段,然后迭代并找到哪个字段有您的注释:
Field[] fields = YourClass.class.getDeclaredFields();
for (Field field : fields) {
Annotation annot = field.getAnnotation(PrimaryKey.class);
if (annot != null) {
System.out.println("Found! " + field);
}
}
您可以获取一个类的所有字段,然后迭代并找到哪个字段有您的注释:
Field[] fields = YourClass.class.getDeclaredFields();
for (Field field : fields) {
Annotation annot = field.getAnnotation(PrimaryKey.class);
if (annot != null) {
System.out.println("Found! " + field);
}
}
首先,您需要找到可能在其成员中包含注释的所有类。这可以使用Spring框架
ClassUtils
实现:
public static void traverse(String classSearchPattern, TypeFilter typeFilter) {
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(classLoader);
Resource[] resources = null;
try {
resources = resourceResolver.getResources(classSearchPattern);
} catch (IOException e) {
throw new FindException(
"An I/O problem occurs when trying to resolve resources matching the pattern: "
+ classSearchPattern, e);
}
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
for (Resource resource : resources) {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
if (typeFilter.match(metadataReader, metadataReaderFactory)) {
String className = metadataReader.getClassMetadata().getClassName();
Class<?> annotatedClass = classLoader.loadClass(className);
// CHECK IF THE CLASS HAS PROPERLY ANNOTATED FIELDS AND
// DO SOMETHING WITH THE CLASS FOUND... E.G., PUT IT IN SOME REGISTRY
}
} catch (Exception e) {
throw new FindException("Failed to analyze annotation for resource: " + resource, e);
}
}
}
publicstaticvoid遍历(stringclasssearchpattern,TypeFilter-TypeFilter){
ClassLoader ClassLoader=ClassUtils.getDefaultClassLoader();
ResourcePatternResolver resourceResolver=新路径匹配源模式解析程序(classLoader);
Resource[]resources=null;
试一试{
resources=resourceResolver.getResources(classSearchPattern);
}捕获(IOE异常){
抛出新的FindException(
“尝试解析与模式匹配的资源时发生I/O问题:”
+类搜索模式(e);
}
MetadataReaderFactory MetadataReaderFactory=新的SimpleMetadataReaderFactory();
for(资源:资源){
试一试{
MetadataReader MetadataReader=metadataReaderFactory.getMetadataReader(资源);
if(typeFilter.match(metadataReader,metadataReaderFactory)){
字符串className=metadataReader.getClassMetadata().getClassName();
Class annotatedClass=classLoader.loadClass(className);
//检查类是否具有正确注释的字段和
//对找到的类执行某些操作…例如,将其放在某个注册表中
}
}捕获(例外e){
抛出新的FindException(“未能分析资源的注释:“+resource,e”);
}
}
}
首先,您需要找到可能在其成员中包含注释的所有类。这可以使用Spring框架ClassUtils
实现:
public static void traverse(String classSearchPattern, TypeFilter typeFilter) {
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(classLoader);
Resource[] resources = null;
try {
resources = resourceResolver.getResources(classSearchPattern);
} catch (IOException e) {
throw new FindException(
"An I/O problem occurs when trying to resolve resources matching the pattern: "
+ classSearchPattern, e);
}
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
for (Resource resource : resources) {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
if (typeFilter.match(metadataReader, metadataReaderFactory)) {
String className = metadataReader.getClassMetadata().getClassName();
Class<?> annotatedClass = classLoader.loadClass(className);
// CHECK IF THE CLASS HAS PROPERLY ANNOTATED FIELDS AND
// DO SOMETHING WITH THE CLASS FOUND... E.G., PUT IT IN SOME REGISTRY
}
} catch (Exception e) {
throw new FindException("Failed to analyze annotation for resource: " + resource, e);
}
}
}
publicstaticvoid遍历(stringclasssearchpattern,TypeFilter-TypeFilter){
ClassLoader ClassLoader=ClassUtils.getDefaultClassLoader();
ResourcePatternResolver resourceResolver=新路径匹配源模式解析程序(classLoader);
Resource[]resources=null;
试一试{
resources=resourceResolver.getResources(classSearchPattern);
}捕获(IOE异常){
抛出新的FindException(
“尝试解析与模式匹配的资源时发生I/O问题:”
+类搜索模式(e);
}
MetadataReaderFactory MetadataReaderFactory=新的SimpleMetadataReaderFactory();
for(资源:资源){
试一试{
MetadataReader MetadataReader=metadataReaderFactory.getMetadataReader(资源);
if(typeFilter.match(metadataReader,metadataReaderFactory)){
字符串className=metadataReader.getClassMetadata().getClassName();
Class annotatedClass=classLoader.loadClass(className);
//检查类是否具有正确注释的字段和
//对找到的类执行某些操作…例如,将其放在某个注册表中
}
}捕获(例外e){
抛出新的FindException(“未能分析资源的注释:“+resource,e”);
}
}
}
谢谢-这看起来接近我需要的,但我需要从类的实例(即对象)开始工作-代码不知道它是哪个类(类型)。谢谢-这看起来接近我需要的,但我需要从类的实例(即对象)开始工作-代码不知道它是哪个类(类型)这看起来很接近我需要的,但我需要从类的实例(即对象)开始工作,代码不知道它是哪个类(类型)的对象。但是你可以通过instance.getClass()来询问它。谢谢-这看起来很接近我需要的,但我需要从类的实例(即对象)开始工作-并且代码不知道它是哪个类(类型)的对象。但是您可以通过instance.getClass()来询问它