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

Java tutorial

Introduction

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

Source

/*
 * Copyright 2007-2012 Jiemamy Project and the Others.
 *
 * 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.util.HashMap;
import java.util.Map;

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

/**
 * {@link Class}???
 * 
 * @version $Id$
 * @author j5ik2o
 */
public final class ClassUtil {

    private static Map<Class<?>, Class<?>> wrapperToPrimitiveMap = new HashMap<Class<?>, Class<?>>();

    private static Map<Class<?>, Class<?>> primitiveToWrapperMap = new HashMap<Class<?>, Class<?>>();

    private static Map<String, Class<?>> primitiveClsssNameMap = new HashMap<String, Class<?>>();

    static {
        wrapperToPrimitiveMap.put(Character.class, Character.TYPE);
        wrapperToPrimitiveMap.put(Byte.class, Byte.TYPE);
        wrapperToPrimitiveMap.put(Short.class, Short.TYPE);
        wrapperToPrimitiveMap.put(Integer.class, Integer.TYPE);
        wrapperToPrimitiveMap.put(Long.class, Long.TYPE);
        wrapperToPrimitiveMap.put(Double.class, Double.TYPE);
        wrapperToPrimitiveMap.put(Float.class, Float.TYPE);
        wrapperToPrimitiveMap.put(Boolean.class, Boolean.TYPE);

        primitiveToWrapperMap.put(Character.TYPE, Character.class);
        primitiveToWrapperMap.put(Byte.TYPE, Byte.class);
        primitiveToWrapperMap.put(Short.TYPE, Short.class);
        primitiveToWrapperMap.put(Integer.TYPE, Integer.class);
        primitiveToWrapperMap.put(Long.TYPE, Long.class);
        primitiveToWrapperMap.put(Double.TYPE, Double.class);
        primitiveToWrapperMap.put(Float.TYPE, Float.class);
        primitiveToWrapperMap.put(Boolean.TYPE, Boolean.class);

        primitiveClsssNameMap.put(Character.TYPE.getName(), Character.TYPE);
        primitiveClsssNameMap.put(Byte.TYPE.getName(), Byte.TYPE);
        primitiveClsssNameMap.put(Short.TYPE.getName(), Short.TYPE);
        primitiveClsssNameMap.put(Integer.TYPE.getName(), Integer.TYPE);
        primitiveClsssNameMap.put(Long.TYPE.getName(), Long.TYPE);
        primitiveClsssNameMap.put(Double.TYPE.getName(), Double.TYPE);
        primitiveClsssNameMap.put(Float.TYPE.getName(), Float.TYPE);
        primitiveClsssNameMap.put(Boolean.TYPE.getName(), Boolean.TYPE);
    }

    /**
     * ??2?????????????????????
     * 
     * <p>Java??????????</p>
     * 
     * @param s1 ??
     * @param s2 ????????
     * @return ???????
     */
    public static String concatName(String s1, String s2) {
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)) {
            return s1;
        }
        if (StringUtils.isEmpty(s1) == false && StringUtils.isEmpty(s2)) {
            return s1;
        }
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2) == false) {
            return s2;
        }
        return s1 + '.' + s2;
    }

    /**
     * ???????
     * 
     * <p>???????? context {@link ClassLoader}?? {@link Class#forName(String, boolean, ClassLoader)}????</p>
     * 
     * @param className ??
     * @return {@link Class}
     * @throws ClassNotFoundException ????????
     * @see #forName(String)
     */
    public static Class<?> convertClass(String className) throws ClassNotFoundException {
        Class<?> clazz = primitiveClsssNameMap.get(className);
        if (clazz != null) {
            return clazz;
        }
        return forName(className);
    }

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

    /**
     * {@link Field}??????????{@code null}?
     * 
     * @param clazz 
     * @param fieldName ??
     * @return {@link Field}
     * @see Class#getField(String)
     * @throws IllegalArgumentException {@code clazz}?{@code null}???
     */
    public static Field getFieldNoException(Class<?> clazz, String fieldName) {
        Validate.notNull(clazz);
        try {
            return clazz.getField(fieldName);
        } catch (SecurityException e) {
            return null;
        } catch (NoSuchFieldException e) {
            return null;
        }
    }

    /**
     * ????
     * 
     * @param clazz 
     * @return ??.  ???{@code null}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getPackageName(Class<?> clazz) {
        Validate.notNull(clazz);
        String fqcn = clazz.getName();
        int pos = fqcn.lastIndexOf('.');
        if (pos > 0) {
            return fqcn.substring(0, pos);
        }
        return null;
    }

    /**
     * ?????
     * 
     * @param clazz 
     * @return 
     * @see #getResourcePath(String)
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getResourcePath(Class<?> clazz) {
        Validate.notNull(clazz);
        return getResourcePath(clazz.getName());
    }

    /**
     * ?????
     * 
     * @param className ??
     * @return 
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getResourcePath(String className) {
        Validate.notNull(className);
        return StringUtils.replace(className, ".", "/") + ".class";
    }

    /**
     * FQCN??????????
     * 
     * @param clazz 
     * @return FQCN????????
     * @see #getShortClassName(String)
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getShortClassName(Class<?> clazz) {
        Validate.notNull(clazz);
        return getShortClassName(clazz.getName());
    }

    /**
     * FQCN??????????
     * 
     * @param className ??
     * @return FQCN????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getShortClassName(String className) {
        Validate.notNull(className);
        int i = className.lastIndexOf('.');
        if (i > 0) {
            return className.substring(i + 1);
        }
        return className;
    }

    /**
     * ?????????????????????
     * 
     * @param clazz 
     * @return ??
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String getSimpleClassName(Class<?> clazz) {
        Validate.notNull(clazz);
        if (clazz.isArray()) {
            return getSimpleClassName(clazz.getComponentType()) + "[]";
        }
        return clazz.getName();
    }

    /**
     * ???????
     * 
     * @param toClass ?
     * @param fromClass ?
     * @return ?????
     * @see Class#isAssignableFrom(Class)
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static boolean isAssignableFrom(Class<?> toClass, Class<?> fromClass) {
        Validate.notNull(toClass);
        Validate.notNull(fromClass);
        if (toClass == Object.class && fromClass.isPrimitive() == false) {
            return true;
        }
        if (toClass.isPrimitive()) {
            fromClass = toPrimitiveClassIfWrapper(fromClass);
        }
        return toClass.isAssignableFrom(fromClass);
    }

    /**
     * ????
     * 
     * @param className ??
     * @return ??
     * @throws InstantiationException ?? Class ? abstract ?????
     * ??? void ???? null ?????????????????????
     * @throws IllegalAccessException ???????
     * @throws ClassNotFoundException ????????
     */
    public static Object newInstance(String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return forName(className).newInstance();
    }

    /**
     * FQCN???FQCN??????????
     * 
     * @param className ??
     * @return ???FQCN????????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static String[] splitPackageAndShortClassName(String className) {
        Validate.notNull(className);
        String[] ret = new String[2];
        int i = className.lastIndexOf('.');
        if (i > 0) {
            ret[0] = className.substring(0, i);
            ret[1] = className.substring(i + 1);
        } else {
            ret[1] = className;
        }
        return ret;
    }

    /**
     * ???
     * 
     * @param clazz 
     * @return .  {@code clazz}???????{@code null}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> toPrimitiveClass(Class<?> clazz) {
        Validate.notNull(clazz);
        return wrapperToPrimitiveMap.get(clazz);
    }

    /**
     * ??? ??????????????
     * 
     * @param clazz 
     * @return {@link Class}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> toPrimitiveClassIfWrapper(Class<?> clazz) {
        Validate.notNull(clazz);
        Class<?> ret = toPrimitiveClass(clazz);
        if (ret != null) {
            return ret;
        }
        return clazz;
    }

    /**
     * ???
     * 
     * @param clazz 
     * @return {@link Class}.  {@code clazz}???????{@code null}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> toWrapperClass(Class<?> clazz) {
        Validate.notNull(clazz);
        return primitiveToWrapperMap.get(clazz);
    }

    /**
     * ????????????????
     * 
     * @param clazz 
     * @return {@link Class}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static Class<?> toWrapperClassIfPrimitive(Class<?> clazz) {
        Validate.notNull(clazz);
        Class<?> ret = toWrapperClass(clazz);
        if (ret != null) {
            return ret;
        }
        return clazz;
    }

    private ClassUtil() {
    }
}