如何在java中获取Field对象的原始属性?

如何在java中获取Field对象的原始属性?,java,resources,field,Java,Resources,Field,我正在迭代一个类的字段,我需要返回它的字段的原始类 我正在迭代的类中充满了我正在迭代的“资源”对象。我需要返回“Resource”对象,而不是“Field”对象。我怎么做 private Resource getPredicate(String header) { for(Field field : VCARD4.class.getDeclaredFields()){ String name = field.getName(); if(header.eq

我正在迭代一个类的字段,我需要返回它的字段的原始类

我正在迭代的类中充满了我正在迭代的“资源”对象。我需要返回“Resource”对象,而不是“Field”对象。我怎么做

 private Resource getPredicate(String header) {
    for(Field field : VCARD4.class.getDeclaredFields()){
        String name = field.getName();
        if(header.equalsIgnoreCase(name)){

        }
    }

我想你需要这样的东西:

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

class VCARD4 {
    Resource resource_a = new FileSystemResource("/dev/null");
    Resource resource_b = new FileSystemResource("/dev/null");
    Class<?> class_a = Object.class;

    public Resource getResource_a() {
        return resource_a;
    }

    public Resource getResource_b() {
        return resource_b;
    }

    public Class<?> getClass_a() {
        return class_a;
    }
}

class Scratch {
    public static void main(String[] args) {
        VCARD4 vcard4 = new VCARD4();

        Object fieldValue = getPredicate(vcard4, "resource_a");

        System.out.println("Field value: " + fieldValue);
        System.out.println("Field class name: " + fieldValue.getClass().getName());
        System.out.println("Field superclass name: " + fieldValue.getClass().getGenericSuperclass().getTypeName());
    }

    private static Object getPredicate(VCARD4 object, String fieldName) {
        AtomicReference<Object> targetField = new AtomicReference<>();

        Arrays.stream(PropertyUtils.getPropertyDescriptors(VCARD4.class))
                .filter(property -> property.getReadMethod().getName().equalsIgnoreCase("get" + fieldName))
                .findFirst()
                .ifPresent(foundField -> {
                    Method method = foundField.getReadMethod();
                    method.setAccessible(true);
                    targetField.set(ReflectionUtils.invokeMethod(method, object));
                });

        return targetField.get();
    }
}
import org.springframework.core.io.FileSystemResource;
导入org.springframework.core.io.Resource;
导入org.springframework.util.ReflectionUtils;
导入java.lang.reflect.Method;
导入java.util.array;
导入java.util.concurrent.AtomicReference;
VCARD4类{
Resource Resource_a=新文件系统资源(“/dev/null”);
Resource Resource_b=新文件系统资源(“/dev/null”);
Class_a=Object.Class;
公共资源getResource_a(){
返回资源_a;
}
公共资源getResource_b(){
返回资源;
}
公共类getClass_a(){
返回类_a;
}
}
课堂擦伤{
公共静态void main(字符串[]args){
VCARD4 VCARD4=新VCARD4();
objectfieldvalue=getPredicate(vcard4,“资源_a”);
System.out.println(“字段值:”+fieldValue);
System.out.println(“字段类名:+fieldValue.getClass().getName());
System.out.println(“字段超类名称:+fieldValue.getClass().getGenericSuperclass().getTypeName());
}
私有静态对象getPredicate(VCARD4对象,字符串字段名){
AtomicReference targetField=新的AtomicReference();
流(PropertyUtils.getPropertyDescriptors(VCARD4.class))
.filter(property->property.getReadMethod().getName().equalsIgnoreCase(“get”+fieldName))
.findFirst()
.ifPresent(foundField->{
Method=foundField.getReadMethod();
方法setAccessible(true);
set(ReflectionUtils.invokeMethod(方法,对象));
});
返回targetField.get();
}
}
这将基本上为您提供一个类VCARD4指定字段的值。
希望这能有所帮助。

?@AndyTurner这是用来获取声明的类的,我需要相关字段的类。我看不出有什么办法,你所说的领域类别是什么意思。你是说那种类型吗?在这种情况下,
getType()
。当我尝试使用.getType()强制转换它时,我得到:java.lang.ClassCastException:class java.lang.class不能强制转换为类org.apache.jena.rdf.model.Resource(java.lang.class在加载程序“bootstrap”的模块java.base中;org.apache.jena.rdf.model.Resource在加载程序“app”的未命名模块中)这是因为它返回的是
,而不是
资源
。你的意思是你想要这个字段的值吗?