org.jiemamy.utils.reflect.ReflectionUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.jiemamy.utils.reflect.ReflectionUtil.java

Source

/*
 * Copyright 2007-2012 Jiemamy Project and the Others.
 * Created on 2009/01/29
 *
 * This file is part of Jiemamy.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.jiemamy.utils.reflect;

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

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.Validate;

import org.jiemamy.utils.JmStringUtil;

/**
 * ?
 * 
 * @version $Id$
 * @author daisuke
 */
public final class ReflectionUtil {

    /** setter?? */
    public static final String SET = "set";

    /** getter?? */
    public static final String GET = "get";

    /** boolean getter?? */
    public static final String IS = "is";

    /**
     * ?????
     * 
     * @param method 
     * @return ??
     * @throws IllegalArgumentException ??????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertAccessorToFieldName(Method method) {
        Validate.notNull(method);
        if (isAccessor(method) == false) {
            throw new IllegalArgumentException();
        }
        Class<?> returnType = method.getReturnType();
        String name = method.getName();
        if ((returnType == boolean.class || returnType == Boolean.class) && name.startsWith(IS)) {
            return JmStringUtil.decapitalize(name.substring(2));
        }
        return JmStringUtil.decapitalize(name.substring(3));
    }

    /**
     * ??????
     * 
     * @param fieldName ??
     * @param prefix ("set", "get", "is" )
     * @return ??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertFieldNameToAccessorName(String fieldName, String prefix) {
        Validate.notNull(fieldName);
        Validate.notNull(prefix);
        return prefix + JmStringUtil.capitalize(fieldName);
    }

    /**
     * ???setter???
     * 
     * @param fieldName ??
     * @return setter??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertFieldNameToSetterName(String fieldName) {
        Validate.notNull(fieldName);
        return convertFieldNameToAccessorName(fieldName, SET);
    }

    /**
     * ????
     * 
     * @param field 
     * @param prefix ("set", "get", "is" )
     * @return ??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertFieldToAccessorName(Field field, String prefix) {
        Validate.notNull(field);
        Validate.notNull(prefix);
        return convertFieldNameToAccessorName(field.getName(), prefix);
    }

    /**
     * ?getter???
     * 
     * @param field 
     * @return getter??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertFieldToGetterName(Field field) {
        Validate.notNull(field);
        if (field.getType() == boolean.class || field.getType() == Boolean.class) {
            return convertFieldToAccessorName(field, IS);
        }
        return convertFieldToAccessorName(field, GET);
    }

    /**
     * ?setter???
     * 
     * @param field 
     * @return setter??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String convertFieldToSetterName(Field field) {
        Validate.notNull(field);
        return convertFieldToAccessorName(field, SET);
    }

    /**
     * ????????????{@link Class}??
     * 
     * @param <T> {@link Class}??
     * @param className ?????
     * @return ????????{@link Class}
     * @throws ClassNotFoundException ????????
     * @see Class#forName(String)
     */
    public static <T> Class<T> forName(String className) throws ClassNotFoundException {
        return forName(className, Thread.currentThread().getContextClassLoader());
    }

    /**
     * ???????????{@link Class}??
     * 
     * @param <T> {@link Class}??
     * @param className ?????
     * @param loader ??????
     * @return ????????{@link Class}
     * @throws ClassNotFoundException ????????
     * @see Class#forName(String, boolean, ClassLoader)
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> forName(String className, ClassLoader loader) throws ClassNotFoundException {
        return (Class<T>) Class.forName(className, true, loader);
    }

    /**
     * ?????? ???????{@link Class}??
     * 
     * @param <T> {@link Class}??
     * @param className ?????
     * @return ????????{@link Class}.  ?????????{@code null}
     * @see Class#forName(String)
     */
    public static <T> Class<T> forNameNoException(String className) {
        return forNameNoException(className, Thread.currentThread().getContextClassLoader());
    }

    /**
     * ????? ???????{@link Class}??
     * 
     * @param <T> {@link Class}??
     * @param className ?????
     * @param loader ??????
     * @return ????????{@link Class}.  ?????????{@code null}
     * @see Class#forName(String)
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> forNameNoException(String className, ClassLoader loader) {
        try {
            return (Class<T>) Class.forName(className, true, loader);
        } catch (Throwable ignore) {
            return null;
        }
    }

    /**
     * {@link Class}?????????
     * {@link Field}????????
     * 
     * @param clazz ?{@link Class}
     * @param name ??
     * @return {@code name}??????{@link Field}. ?????{@code null}
     * @see Class#getDeclaredField(String)
     */
    public static Field getDeclaredFieldNoException(Class<?> clazz, String name) {
        try {
            return clazz.getDeclaredField(name);
        } catch (SecurityException e) {
            return null;
        } catch (NoSuchFieldException e) {
            return null;
        }
    }

    /**
     * ??????
     * 
     * @param parameterizedCollection ???
     * @return ????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfCollection(Type parameterizedCollection) {
        Validate.notNull(parameterizedCollection);
        return GenericUtil.getRawClass(GenericUtil.getElementTypeOfCollection(parameterizedCollection));
    }

    /**
     * ?????????
     * 
     * @param field 
     * @return ??????? 
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfCollectionFromFieldType(Field field) {
        Validate.notNull(field);
        Type type = field.getGenericType();
        return getElementTypeOfCollection(type);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @param parameterPosition ????????
     * @return ?????????????
     * @throws IllegalArgumentException {@code method}?{@code null}???
     * @throws IndexOutOfBoundsException ???????? parameterPosition?????
     */
    public static Class<?> getElementTypeOfCollectionFromParameterType(Method method, int parameterPosition) {
        Validate.notNull(method);
        Type[] parameterTypes = method.getGenericParameterTypes();
        return getElementTypeOfCollection(parameterTypes[parameterPosition]);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @return ?????????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfCollectionFromReturnType(Method method) {
        Validate.notNull(method);
        return getElementTypeOfCollection(method.getGenericReturnType());
    }

    /**
     * ??????
     * 
     * @param parameterizedList ???
     * @return ????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfList(Type parameterizedList) {
        Validate.notNull(parameterizedList);
        return GenericUtil.getRawClass(GenericUtil.getElementTypeOfList(parameterizedList));
    }

    /**
     * ?????????
     * 
     * @param field 
     * @return ???????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfListFromFieldType(Field field) {
        Validate.notNull(field);
        Type type = field.getGenericType();
        return getElementTypeOfList(type);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @param parameterPosition ????????
     * @return ?????????????
     * @throws IllegalArgumentException ?{@code null}???
     * @throws IndexOutOfBoundsException ???????? parameterPosition?????
     */
    public static Class<?> getElementTypeOfListFromParameterType(Method method, int parameterPosition) {
        Validate.notNull(method);
        Type[] parameterTypes = method.getGenericParameterTypes();
        return getElementTypeOfList(parameterTypes[parameterPosition]);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @return ?????????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfListFromReturnType(Method method) {
        Validate.notNull(method);
        return getElementTypeOfList(method.getGenericReturnType());
    }

    /**
     * ??????
     * 
     * @param parameterizedSet ???
     * @return ????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfSet(Type parameterizedSet) {
        Validate.notNull(parameterizedSet);
        return GenericUtil.getRawClass(GenericUtil.getElementTypeOfSet(parameterizedSet));
    }

    /**
     * ?????????
     * 
     * @param field 
     * @return ???????
     * @throws IndexOutOfBoundsException ???????? parameterPosition?????
     */
    public static Class<?> getElementTypeOfSetFromFieldType(Field field) {
        Validate.notNull(field);
        Type type = field.getGenericType();
        return getElementTypeOfSet(type);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @param parameterPosition ????????
     * @return ?????????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfSetFromParameterType(Method method, int parameterPosition) {
        Validate.notNull(method);
        Type[] parameterTypes = method.getGenericParameterTypes();
        return getElementTypeOfSet(parameterTypes[parameterPosition]);
    }

    /**
     * ???????????????
     * 
     * @param method 
     * @return ?????????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> getElementTypeOfSetFromReturnType(Method method) {
        Validate.notNull(method);
        return getElementTypeOfSet(method.getGenericReturnType());
    }

    /**
     * ??????
     * 
     * @param method 
     * @return ???{@code true}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static boolean isAccessor(Method method) {
        return isGetter(method) || isSetter(method);
    }

    /**
     * ?getter?????
     * 
     * @param method 
     * @return getter???{@code true}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static boolean isGetter(Method method) {
        Validate.notNull(method);
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length != 0) {
            return false;
        }
        Class<?> returnType = method.getReturnType();
        String name = method.getName();
        if (returnType == void.class || returnType == Void.class) {
            return false;
        } else if (returnType == boolean.class || returnType == Boolean.class) {
            boolean result = name.startsWith(IS) && name.length() > 2
                    && CharUtils.isAsciiAlphaUpper(name.toCharArray()[2]);
            if (result == true) {
                return true;
            }
        }
        return name.startsWith(GET) && name.length() > 3 && CharUtils.isAsciiAlphaUpper(name.toCharArray()[3]);
    }

    /**
     * ?setter?????
     * 
     * @param method 
     * @return setter???{@code true}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static boolean isSetter(Method method) {
        Validate.notNull(method);
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length != 1) {
            return false;
        }
        Class<?> returnType = method.getReturnType();
        if (returnType != void.class && returnType != Void.class) {
            return false;
        }
        String name = method.getName();
        return name.startsWith(SET) && name.length() > 3 && CharUtils.isAsciiAlphaUpper(name.toCharArray()[3]);
    }

    private ReflectionUtil() {
    }
}