java反射简单的一些用法

 记录一下反射的用法

 

 用org.springframework.util里面的工具类去反射注解的字段值

Class<?> clz = Class.forName("...");
Map<String, String> map = new ConcurrentHashMap<>();
ReflectionUtils.doWithFields( clz, field -> {
Table annotation = clazz.getAnnotation(Table.class);
String name = annotation.value();
String className = clazz.getSimpleName();
map.put("tableName", name);
map.put("className", className);
});

  

实体对象简单的反射的工具栏
package com.dscomm.datacenter.basicdata.util;



import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description 反射工具类
 */
public class ReflectUtils {

    /**
     * 获取实体类主键
     *
     * @param clazz
     * @return
     */
    public static Field getIdField(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Field item = null;
        for (Field field : fields) {
            Id id = field.getAnnotation(Id.class);
            if (id != null) {
                field.setAccessible(true);
                item = field;
                break;
            }
        }
        if (item == null) {
            Class<?> superclass = clazz.getSuperclass();
            if (superclass != null) {
                item = getIdField(superclass);
            }
        }
        return item;
    }

    /**
     * 根据字段名称获取实体类属性值
     *
     * @param field
     * @return
     */
    public static Map<String, String> getPkValueByName(Field field) {
        Map<String, String> map = new ConcurrentHashMap<>();
        Column declaredAnnotation = field.getDeclaredAnnotation(Column.class);
        String column = declaredAnnotation.value();
        map.put("fieldNames", field.getName());
        map.put("type", field.getType().getName());
        map.put("column", column);
        return map;
    }

    /**
     * 通过反射将 class1不为空的值赋值给class2
     *
     * @param class1
     * @param class2
     * @throws Exception
     */
//    public static void reflectClass1ToClass2(Object class1, Object class2) throws Exception {
//        Field[] field = class1.getClass().getDeclaredFields();
//        for (int i = 0; i < field.length; i++) {
//            String name = field[i].getName();
//            if ("serialVersionUID".equals(name)) {
//                continue;
//            }
//            name = name.substring(0, 1).toUpperCase() + name.substring(1);
//            Method m1 = class1.getClass().getMethod("get" + name);
//            Object value = m1.invoke(class1);
//            if (value != null) {
//                Field f = field[i];
//                f.setAccessible(true);
//                f.set(class2, value);
//            }
//        }
//    }

    /**
     * 获取实体类 @Column 的其中一个属性名称
     *
     * @param clazz
     * @return
     */
    public static List<Map<String, String>> getColumnName(Class<?> clazz) {
        List<Map<String,String>> list = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            Map<String, String> map = new ConcurrentHashMap<>();
            if (field.isAnnotationPresent(Column.class)) {
                /**
                 * 获取字段名
                 */
                Column declaredAnnotation = field.getDeclaredAnnotation(Column.class);
                String column = declaredAnnotation.value();
                map.put("fieldNames", field.getName());
                map.put("type", field.getType().getName());
                map.put("column", column);
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 通过获取类上的@Table注解获取表名称
     *
     * @param clazz
     * @return
     */
    public static Map<String, String> getTableName(Class<?> clazz) {
        Map<String, String> map = new ConcurrentHashMap<>();
        Table annotation = clazz.getAnnotation(Table.class);
        String name = annotation.value();
        String className = clazz.getSimpleName();
        map.put("tableName", name);
        map.put("className", className);
        return map;
    }



}

  

posted @ 2022-01-24 10:01  不是安逸  阅读(49)  评论(0编辑  收藏  举报