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

com.github.quartzwebui.utils.ClassUtils Maven / Gradle / Ivy

The newest version!
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.github.quartzwebui.utils;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author quxiucheng [[email protected]]
 */
public class ClassUtils {

    /**
     * Suffix for array class names: "[]"
     */
    public static final String ARRAY_SUFFIX = "[]";

    /**
     * Prefix for internal array class names: "["
     */
    private static final String INTERNAL_ARRAY_PREFIX = "[";

    /**
     * Prefix for internal non-primitive array class names: "[L"
     */
    private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";

    /**
     * The package separator character '.'
     */
    private static final char PACKAGE_SEPARATOR = '.';


    /**
     * The inner class separator character '$'
     */
    private static final char INNER_CLASS_SEPARATOR = '$';


    /**
     * Map with primitive wrapper type as key and corresponding primitive
     * type as value, for example: Integer.class -> int.class.
     */
    private static final Map, Class> primitiveWrapperTypeMap = new HashMap, Class>(8);

    /**
     * Map with primitive type as key and corresponding wrapper
     * type as value, for example: int.class -> Integer.class.
     */
    private static final Map, Class> primitiveTypeToWrapperMap = new HashMap, Class>(8);

    private static final Map, Class> primitiveTypeMap = new HashMap, Class>(8);

    /**
     * Map with primitive type name as key and corresponding primitive
     * type as value, for example: "int" -> "int.class".
     */
    private static final Map> primitiveTypeNameMap = new HashMap>(32);

    /**
     * Map with common "java.lang" class name as key and corresponding Class as value.
     * Primarily for efficient deserialization of remote invocations.
     */
    private static final Map> commonClassCache = new HashMap>(32);


    static {
        primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
        primitiveWrapperTypeMap.put(Byte.class, byte.class);
        primitiveWrapperTypeMap.put(Character.class, char.class);
        primitiveWrapperTypeMap.put(Double.class, double.class);
        primitiveWrapperTypeMap.put(Float.class, float.class);
        primitiveWrapperTypeMap.put(Integer.class, int.class);
        primitiveWrapperTypeMap.put(Long.class, long.class);
        primitiveWrapperTypeMap.put(Short.class, short.class);

        for (Map.Entry, Class> entry : primitiveWrapperTypeMap.entrySet()) {
            primitiveTypeToWrapperMap.put(entry.getValue(), entry.getKey());
            registerCommonClasses(entry.getKey());
        }

        Set> primitiveTypes = new HashSet>(32);
        primitiveTypes.addAll(primitiveWrapperTypeMap.values());
        primitiveTypes.addAll(Arrays.asList(new Class[]{
                boolean[].class, byte[].class, char[].class, double[].class,
                float[].class, int[].class, long[].class, short[].class}));
        primitiveTypes.add(void.class);
        for (Class primitiveType : primitiveTypes) {
            primitiveTypeNameMap.put(primitiveType.getName(), primitiveType);
            primitiveTypeMap.put(primitiveType, primitiveType);
        }

        registerCommonClasses(Boolean[].class, Byte[].class, Character[].class, Double[].class,
                Float[].class, Integer[].class, Long[].class, Short[].class);
        registerCommonClasses(Number.class, Number[].class, String.class, String[].class,
                Object.class, Object[].class, Class.class, Class[].class);
        registerCommonClasses(Throwable.class, Exception.class, RuntimeException.class,
                Error.class, StackTraceElement.class, StackTraceElement[].class);
    }

    public static boolean isAssignableFrom(Object source, Object target) {
        if (source == null || target == null) {
            return false;
        }
        return source.getClass().isAssignableFrom(target.getClass());
    }


    /**
     * Register the given common classes with the ClassUtils cache.
     */
    private static void registerCommonClasses(Class... commonClasses) {
        for (Class clazz : commonClasses) {
            commonClassCache.put(clazz.getName(), clazz);
        }
    }

    /**
     * Return the default ClassLoader to use: typically the thread context
     * ClassLoader, if available; the ClassLoader that loaded the ClassUtils
     * class will be used as fallback.
     * 

Call this method if you intend to use the thread context ClassLoader * in a scenario where you clearly prefer a non-null ClassLoader reference: * for example, for class path resource loading (but not necessarily for * {@code Class.forName}, which accepts a {@code null} ClassLoader * reference as well). * * @return the default ClassLoader (only {@code null} if even the system * ClassLoader isn't accessible) * @see Thread#getContextClassLoader() * @see ClassLoader#getSystemClassLoader() */ public static ClassLoader getDefaultClassLoader() { ClassLoader cl = null; try { cl = Thread.currentThread().getContextClassLoader(); } catch (Throwable ex) { // Cannot access thread context ClassLoader - falling back... } if (cl == null) { // No thread context class loader -> use class loader of this class. cl = ClassUtils.class.getClassLoader(); if (cl == null) { // getClassLoader() returning null indicates the bootstrap ClassLoader try { cl = ClassLoader.getSystemClassLoader(); } catch (Throwable ex) { // Cannot access system ClassLoader - oh well, maybe the caller can live with null... } } } return cl; } /** * Replacement for {@code Class.forName()} that also returns Class instances * for primitives (e.g. "int") and array class names (e.g. "String[]"). * Furthermore, it is also capable of resolving inner class names in Java source * style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State"). * * @param name the name of the Class * @param classLoader the class loader to use * (may be {@code null}, which indicates the default class loader) * @return Class instance for the supplied name * @throws ClassNotFoundException if the class was not found * @throws LinkageError if the class file could not be loaded * @see Class#forName(String, boolean, ClassLoader) */ public static Class forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError { Assert.notNull(name, "Name must not be null"); Class clazz = resolvePrimitiveClassName(name); if (clazz == null) { clazz = commonClassCache.get(name); } if (clazz != null) { return clazz; } // "java.lang.String[]" style arrays if (name.endsWith(ARRAY_SUFFIX)) { String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length()); Class elementClass = forName(elementClassName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } // "[Ljava.lang.String;" style arrays if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) { String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1); Class elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } // "[[I" or "[[Ljava.lang.String;" style arrays if (name.startsWith(INTERNAL_ARRAY_PREFIX)) { String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length()); Class elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } ClassLoader clToUse = classLoader; if (clToUse == null) { clToUse = getDefaultClassLoader(); } try { return (clToUse != null ? clToUse.loadClass(name) : Class.forName(name)); } catch (ClassNotFoundException ex) { int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR); if (lastDotIndex != -1) { String innerClassName = name.substring(0, lastDotIndex) + INNER_CLASS_SEPARATOR + name.substring(lastDotIndex + 1); try { return (clToUse != null ? clToUse.loadClass(innerClassName) : Class.forName(innerClassName)); } catch (ClassNotFoundException ex2) { // Swallow - let original exception get through } } throw ex; } } /** * Resolve the given class name as primitive class, if appropriate, * according to the JVM's naming rules for primitive classes. *

Also supports the JVM's internal class names for primitive arrays. * Does not support the "[]" suffix notation for primitive arrays; * this is only supported by {@link #forName(String, ClassLoader)}. * * @param name the name of the potentially primitive class * @return the primitive class, or {@code null} if the name does not denote * a primitive class or primitive array class */ public static Class resolvePrimitiveClassName(String name) { Class result = null; // Most class names will be quite long, considering that they // SHOULD sit in a package, so a length check is worthwhile. if (name != null && name.length() <= 8) { // Could be a primitive - likely. result = primitiveTypeNameMap.get(name); } return result; } /** * Check if the right-hand side type may be assigned to the left-hand side * type, assuming setting by reflection. Considers primitive wrapper * classes as assignable to the corresponding primitive types. * * @param lhsType the target type * @param rhsType the value type that should be assigned to the target type * @return if the target type is assignable from the value type */ public static boolean isAssignable(Class lhsType, Class rhsType) { Assert.notNull(lhsType, "Left-hand side type must not be null"); Assert.notNull(rhsType, "Right-hand side type must not be null"); if (lhsType.isAssignableFrom(rhsType)) { return true; } if (lhsType.isPrimitive()) { Class resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType); if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) { return true; } } else { Class resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType); if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) { return true; } } return false; } /** * Determine if the given type is assignable from the given value, * assuming setting by reflection. Considers primitive wrapper classes * as assignable to the corresponding primitive types. * * @param type the target type * @param value the value that should be assigned to the type * @return if the type is assignable from the value */ public static boolean isAssignableValue(Class type, Object value) { Assert.notNull(type, "Type must not be null"); // 判断class时候为基本类型 return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive()); } /** * Return a public static method of a class. * @param methodName the static method name * @param clazz the class which defines the method * @param args the parameter types to the method * @return the static method, or {@code null} if no static method was found * @throws IllegalArgumentException if the method name is blank or the clazz is null */ public static Method getStaticMethod(Class clazz, String methodName, Class... args) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(methodName, "Method name must not be null"); try { Method method = clazz.getMethod(methodName, args); return Modifier.isStatic(method.getModifiers()) ? method : null; } catch (NoSuchMethodException ex) { return null; } } /** * Check if the given class represents a primitive wrapper, * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double. * @param clazz the class to check * @return whether the given class is a primitive wrapper class */ public static boolean isPrimitiveWrapper(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return primitiveWrapperTypeMap.containsKey(clazz); } public static boolean isPrimitive(Class clazz) { return primitiveTypeMap.containsKey(clazz); } /** * Check if the given class represents a primitive (i.e. boolean, byte, * char, short, int, long, float, or double) or a primitive wrapper * (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double). * @param clazz the class to check * @return whether the given class is a primitive or primitive wrapper class */ public static boolean isPrimitiveOrWrapper(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isPrimitive() || isPrimitiveWrapper(clazz)); } /** * Check if the given class represents an array of primitives, * i.e. boolean, byte, char, short, int, long, float, or double. * @param clazz the class to check * @return whether the given class is a primitive array class */ public static boolean isPrimitiveArray(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isArray() && clazz.getComponentType().isPrimitive()); } /** * Check if the given class represents an array of primitive wrappers, * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double. * @param clazz the class to check * @return whether the given class is a primitive wrapper array class */ public static boolean isPrimitiveWrapperArray(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType())); } /** * Resolve the given class if it is a primitive class, * returning the corresponding primitive wrapper type instead. * @param clazz the class to check * @return the original class, or a primitive wrapper for the original primitive type */ public static Class resolvePrimitiveIfNecessary(Class clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz); } /** * 获取类型不匹配的权重 * 权重越大类型匹配度越低 * Algorithm that judges the match between the declared parameter types of a candidate method * and a specific list of arguments that this method is supposed to be invoked with. *

Determines a weight that represents the class hierarchy difference between types and * arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase * the result - all direct matches means weight 0. A match between type Object and arg of * class Integer would increase the weight by 2, due to the superclass 2 steps up in the * hierarchy (i.e. Object) being the last one that still matches the required type Object. * Type Number and class Integer would increase the weight by 1 accordingly, due to the * superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number. * Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a * constructor (Number) which would in turn be preferred to a constructor (Object). * All argument weights get accumulated. *

Note: This is the algorithm used by MethodInvoker itself and also the algorithm * used for constructor and factory method selection in Spring's bean container (in case * of lenient constructor resolution which is the default for regular bean definitions). * * @param paramTypes the parameter types to match * @param args the arguments to match * @return the accumulated weight for all arguments */ public static int getTypeDifferenceWeight(Class[] paramTypes, Object[] args) { int result = 0; for (int i = 0; i < paramTypes.length; i++) { // 类型不匹配,优先级最后 if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) { return Integer.MAX_VALUE; } if (args[i] != null) { Class paramType = paramTypes[i]; // 获取父类型 Class superClass = args[i].getClass().getSuperclass(); while (superClass != null) { // 相同权重 +2 if (paramType.equals(superClass)) { result = result + 2; superClass = null; } // 继承关系权重 +2 else if (ClassUtils.isAssignable(paramType, superClass)) { result = result + 2; // 继续判断父类,假如相同 +2 superClass = superClass.getSuperclass(); } else { superClass = null; } } // 接口权重 +1 if (paramType.isInterface()) { result = result + 1; } } } // 返回权重 return result; } /** * 是否有无参构造函数 * * @param clazz * @return */ public static boolean hasNonParameterConstructor(Class clazz) { Constructor[] constructors = clazz.getConstructors(); // 获取最小参数构造器 for (Constructor constructor : constructors) { int length = constructor.getParameterTypes().length; // 无参构造函数返回 if (length == 0) { return true; } } return false; } /** * 获取最小参数的构造函数 * @param clazz * @return */ public static Constructor getMinParameterTypeConstructor(Class clazz){ Constructor[] constructors = clazz.getConstructors(); int lengthMin = Integer.MAX_VALUE; Constructor minConstructor = null; for (Constructor constructor : constructors) { Class[] parameterTypes = constructor.getParameterTypes(); int length = parameterTypes.length; if (lengthMin >= length) { lengthMin = length; minConstructor = constructor; } } return minConstructor; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy