All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.feizns.reflect.ClassUtils Maven / Gradle / Ivy

There is a newer version: 5.5-RELEASE
Show newest version
package com.gitee.feizns.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;
import java.util.function.Function;

/**
 * @author feizns
 * @since 2019/5/15
 */
public abstract class ClassUtils {

    /**
     * 获取类型数组
     * @param params
     * @return
     */
    public static final Class[] getClasses(Object... params) {
        return Arrays.stream(params).map(Object::getClass).toArray(Class[]::new);
    }

    /**
     * 获取一个类的继承体系,从子类开始到父类
     * @return
     */
    public static final Deque> getClasses(Class type) {
        return getClasses(type, item -> item);
    }

    /**
     * 收集你所想拥有的。
     * 获取一个类的继承体系,从子类开始到父类
     * @return
     */
    public static final  Deque getClasses(Class type, Function, T> func) {
        Deque deque = new LinkedList<>();
        while ( type != null ) {
            deque.offer(func.apply(type));
            type = type.getSuperclass();
        }
        return deque;
    }

    /**
     * 获取一个类的接口体系,向上延伸
     * @param type
     * @return
     */
    public static final Deque> getInterfaces(Class type) {
        Deque> result = new LinkedList<>();

        Deque> interfaces = new LinkedList<>();
        interfaces.offer(type);
        while ( interfaces.isEmpty() == false ) {
            Class remove = interfaces.remove();
            result.offer(remove);
            interfaces.addAll(Arrays.asList(remove.getInterfaces()));
        }

        return result;
    }

    /**
     * 获取一个类的继承体系和和接口体系 当前类 => 父类,当前类接口,父类接口
     * @param type
     * @return
     */
    public static final Queue> getClassesAndInterfaces(Class type) {
        Deque> result = new LinkedList<>();
        result.addAll(getClasses(type));
        Arrays.stream(type.getInterfaces()).forEach(item -> result.addAll(getInterfaces(item)));
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy