`

记录--ReflectionUtil

 
阅读更多

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * <p>Utility class that uses {@code java.lang.reflect} standard library.
 * It provides easy access to the standard reflect methods that are
 * needed usually when dealing with generic object types.</p>  
 */
public class ReflectionUtil {
   
    /**
     * When {@code Type} initialized with a value of an object, its fully qualified class name
     * will be prefixed with this.
     *
     * @see {@link ReflectionUtil#getClassName(Type)}
     */
    private static final String TYPE_CLASS_NAME_PREFIX = "class ";
    private static final String TYPE_INTERFACE_NAME_PREFIX = "interface ";
   
    /*
     *  Utility class with static access methods, no need for constructor.
     */
    private ReflectionUtil() {}
   
    /**
     * {@link Type#toString()} value is the fully qualified class name prefixed
     * with {@link ReflectionUtil#TYPE_NAME_PREFIX}. This method will substring it, for it to be eligible
     * for {@link Class#forName(String)}.
     *
     * @param type the {@code Type} value whose class name is needed. 
     * @return {@code String} class name of the invoked {@code type}.
     *
     * @see {@link ReflectionUtil#getClass()}
     */
    public static String getClassName(Type type) {
        if (type==null) {
            return "";
        }
        String className = type.toString();
        if (className.startsWith(TYPE_CLASS_NAME_PREFIX)) {
            className = className.substring(TYPE_CLASS_NAME_PREFIX.length());
        } else if (className.startsWith(TYPE_INTERFACE_NAME_PREFIX)) {
            className = className.substring(TYPE_INTERFACE_NAME_PREFIX.length());
        }
        return className;
    }
   
    /**
     * Returns the {@code Class} object associated with the given {@link Type}
     * depending on its fully qualified name.
     *
     * @param type the {@code Type} whose {@code Class} is needed.
     * @return the {@code Class} object for the class with the specified name.
     *
     * @throws ClassNotFoundException if the class cannot be located.
     *
     * @see {@link ReflectionUtil#getClassName(Type)}
     */
    public static Class<?> getClass(Type type)
            throws ClassNotFoundException {
        String className = getClassName(type);
        if (className==null || className.isEmpty()) {
            return null;
        }
        return Class.forName(className);
    }
   
    /**
     * Creates a new instance of the class represented by this {@code Type} object.
     *
     * @param type the {@code Type} object whose its representing {@code Class} object
     *         will be instantiated. 
     * @return a newly allocated instance of the class represented by
     *         the invoked {@code Type} object.
     *
     * @throws ClassNotFoundException if the class represented by this {@code Type} object
     *             cannot be located.
     * @throws InstantiationException if this {@code Type} represents an abstract class,
     *             an interface, an array class, a primitive type, or void;
     *             or if the class has no nullary constructor;
     *             or if the instantiation fails for some other reason.
     * @throws IllegalAccessException if the class or its nullary constructor is not accessible.
     *
     * @see {@link Class#newInstance()}
     */
    public static Object newInstance(Type type)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> clazz = getClass(type);
        if (clazz==null) {
            return null;
        }
        return clazz.newInstance();
    }
   
    /**
     * Returns an array of {@code Type} objects representing the actual type
     * arguments to this object.
     * If the returned value is null, then this object represents a non-parameterized
     * object.
     *
     * @param object the {@code object} whose type arguments are needed.
     * @return an array of {@code Type} objects representing the actual type
     *         arguments to this object.
     *
     * @see {@link Class#getGenericSuperclass()}
     * @see {@link ParameterizedType#getActualTypeArguments()}
     */
    public static Type[] getParameterizedTypes(Object object) {
        Type superclassType = object.getClass().getGenericSuperclass();
        if (!ParameterizedType.class.isAssignableFrom(superclassType.getClass())) {
            return null;
        }
       
        return ((ParameterizedType)superclassType).getActualTypeArguments();
    }
   
    /**
     * Checks whether a {@code Constructor} object with no parameter types is specified
     * by the invoked {@code Class} object or not.
     *
     * @param clazz the {@code Class} object whose constructors are checked.
     * @return {@code true} if a {@code Constructor} object with no parameter types is specified.
     * @throws SecurityException If a security manager, <i>s</i> is present and any of the
     *         following conditions is met:
     *            <ul>
     *             <li> invocation of
     *             {@link SecurityManager#checkMemberAccess
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
     *             access to the constructor
     *
     *             <li> the caller's class loader is not the same as or an
     *             ancestor of the class loader for the current class and
     *             invocation of {@link SecurityManager#checkPackageAccess
     *             s.checkPackageAccess()} denies access to the package
     *             of this class
     *         </ul>
     *        
     * @see {@link Class#getConstructor(Class...)}
     */
    public static boolean hasDefaultConstructor(Class<?> clazz) throws SecurityException {
        Class<?>[] empty = {};
        try {
            clazz.getConstructor(empty);
        } catch (NoSuchMethodException e) {
            return false;
        }
        return true;
    }
   
    /**
     * Returns a {@code Class} object that identifies the
     * declared class for the field represented by the given {@code String name} parameter inside
     * the invoked {@code Class<?> clazz} parameter.
     *
     * @param clazz the {@code Class} object whose declared fields to be
     *         checked for a certain field.
     * @param name the field name as {@code String} to be
     *         compared with {@link Field#getName()}
     * @return the {@code Class} object representing the type of given field name.
     *
     * @see {@link Class#getDeclaredFields()}
     * @see {@link Field#getType()}
     */
    public static Class<?> getFieldClass(Class<?> clazz, String name) {
        if (clazz==null || name==null || name.isEmpty()) {
            return null;
        }
       
        Class<?> propertyClass = null;
       
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            if (field.getName().equalsIgnoreCase(name)) {
                propertyClass = field.getType();
                break;
            }
        }
       
        return propertyClass;
    }
   
    /**
     * Returns a {@code Class} object that identifies the
     * declared class as a return type for the method represented by the given
     * {@code String name} parameter inside the invoked {@code Class<?> clazz} parameter.
     *
     * @param clazz the {@code Class} object whose declared methods to be
     *         checked for the wanted method name.
     * @param name the method name as {@code String} to be
     *         compared with {@link Method#getName()}
     * @return the {@code Class} object representing the return type of the given method name.
     *
     * @see {@link Class#getDeclaredMethods()}
     * @see {@link Method#getReturnType()}
     */
    public static Class<?> getMethodReturnType(Class<?> clazz, String name) {
        if (clazz==null || name==null || name.isEmpty()) {
            return null;
        }
       
        name = name.toLowerCase();
        Class<?> returnType = null;
       
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(name)) {
                returnType = method.getReturnType();
                break;
            }
        }
       
        return returnType;
    }
   
    /**
     * Extracts the enum constant of the specified enum class with the
     * specified name. The name must match exactly an identifier used
     * to declare an enum constant in the given class.
     *
     * @param clazz the {@code Class} object of the enum type from which
     *         to return a constant.
     * @param name the name of the constant to return.
     * @return the enum constant of the specified enum type with the
     *      specified name.
     *     
     * @throws IllegalArgumentException if the specified enum type has
     *         no constant with the specified name, or the specified
     *         class object does not represent an enum type.
     *        
     * @see {@link Enum#valueOf(Class, String)}
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Object getEnumConstant(Class<?> clazz, String name) {
        if (clazz==null || name==null || name.isEmpty()) {
            return null;
        }
        return Enum.valueOf((Class<Enum>)clazz, name);
    }
}

分享到:
评论

相关推荐

    ReflectionUtil:反射工具包

    ReflectionUtil 反射工具包,利用反射的API直接生成Java字节码,提高执行效率。 ###普通方法调用 所有的命令最终生成到Invoker对象的invoke方法中 public Object invoke(Object[] args); 具体使用如下: ...

    ReflectionUtil.java

    Java开发中中经常使用的Java工具类分享,工作中用得上,直接拿来使用,不用重复造轮子。

    aspnet公共类cs文件.rar

    ----------Database-------------- ...(ReflectionUtil.cs) 注册表操作辅助类(RegistryHelper.cs) 用于验证码图片识别的类(UnCodebase.cs) 将原始字串转换为unicode,格式为\u.\u.( UnicodeHelper.cs)

    WHC第三方控件

    ----------Database-------------- ...(ReflectionUtil.cs) 13. 注册表操作辅助类(RegistryHelper.cs) 14. 用于验证码图片识别的类(UnCodebase.cs) 15. 将原始字串转换为unicode,格式为\u.\u.( UnicodeHelper.cs)

    DotNet公用类(超多附文档)

    ----------Database-------------- ...(ReflectionUtil.cs) 13.注册表操作辅助类(RegistryHelper.cs) 14.用于验证码图片识别的类(UnCodebase.cs) 15.将原始字串转换为unicode,格式为\u.\u.( UnicodeHelper.cs)

    reflection-util:Java反射实用程序

    Reflection-util:Java反射实用程序请参阅。编辑您的构建文件您可以从获得Reflection-util库。 在gradle构建文件中,编写dependencies { implementation 'org.plumelib:reflection-util:1.0.3'}其他构建系统为 。

    android-utils:petitviolet的Android Util库

    记录仪记录更多详细的类,方法和行号键盘实用程序显示/隐藏键盘ReflectionUtil 通过反射获得/吸收ToastUtil 显示吐司的捷径AddOnScrollListenerUtil 未set但add AbsListView.OnScrollListener MainThreadCallback ...

    反射处理java泛型

    这是本文将要介绍的ReflectionUtil是为了解决这类问题的辅助工具类,为java.lang.reflect标准库的工具类。它提供了便捷的访问泛型对象类型(java.reflect.Type)的反射方法。  本文假设你已经了解java反射知识,并能...

    JAVA 工具类 toolkit

    JsonResult、PageBean、exception、excel、FtpHelper、HttpHelper、AESHelper、DESHelper、RSAHelper、ChineseUtil、ClassUtil、...PropertiesLoader、ReflectionUtil、StringUtil、ValidatorUtil、ZipUtil

Global site tag (gtag.js) - Google Analytics