0
点赞
收藏
分享

微信扫一扫

Java输入字段名称,取出实体类中字段对应的值

首选我们创建一个FileUtils,代码如下,如何使用请看代码中的main方法

package com.bmw.cieaf.utils;

import com.bmw.cieaf.app.entity.PreonboardDataSourcesEntity;
import com.bmw.cieaf.app.utils.PinyinUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.List;

public class FieldUtil {
    public static Object getFieldValueByObject(Object object, String targetFieldName) throws Exception {

        // 获取该对象的Class
        Class objClass = object.getClass();

        // 初始化返回值
        Object result = null;

        // 获取所有的属性数组
        Field[] fields = objClass.getDeclaredFields();
        for (Field field : fields) {
            // 属性名称
            String currentFieldName = "";
            // 获取属性上面的注解 import com.fasterxml.jackson.annotation.JsonProperty;
            /**
             * 举例: @JsonProperty("roleIds")
             * private String roleIds;
             */

            try {
                boolean has_JsonProperty = field.isAnnotationPresent(JsonProperty.class);

                if (has_JsonProperty) {
                    currentFieldName = field.getAnnotation(JsonProperty.class).value();
                } else {
                    currentFieldName = field.getName();
                }

                if (currentFieldName.equals(targetFieldName)) {
                    field.setAccessible(true);
                    result = field.get(object);

                    return result; // 通过反射拿到该属性在此对象中的值(也可能是个对象)
                }

            } catch (SecurityException e) {
                // 安全性异常
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // 非法参数
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // 无访问权限
                e.printStackTrace();
            }
        }

        return result;
    }

    public static String getStringAbel(PreonboardDataSourcesEntity preonboardDataSourcesEntity, Class<?> c, String field) throws BadHanyuPinyinOutputFormatCombination {
        StringBuffer result = new StringBuffer();
        if (StringUtils.isNoneBlank(field)) {
            Field[] fields = c.getDeclaredFields();
            int pos;
            for (pos = 0; pos < fields.length; pos++) {
                if (field.equals(fields[pos].getName())) {
                    break;
                }
            }

                try {
                    fields[pos].setAccessible(true);
                    result.append(fields[pos].get(preonboardDataSourcesEntity) + ",");
                } catch (Exception e) {
                    System.out.println("error--------" + "Reason is:" + e.getMessage());
                }

        }
        if (result.toString().equals("null,")||result.length()==0){
            result.delete(0,result.length());
            result.append(" ,");
            return result.deleteCharAt(result.length()-1).toString();
        }
        return result.deleteCharAt(result.length() - 1).toString();
    }
	//如何使用,看main方法
    public static void main(String[] args) throws Exception {
        PreonboardDataSourcesEntity preonboardDataSourcesEntity = new PreonboardDataSourcesEntity();
        preonboardDataSourcesEntity.setBirthName("张三");
        System.out.println(getStringAbel(preonboardDataSourcesEntity,PreonboardDataSourcesEntity.class, "birthName"));
    }
}

举报

相关推荐

0 条评论