com.discovery.darchrow.lang.ClassUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.lang.ClassUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * 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 com.discovery.darchrow.lang;

import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.Map;

import com.discovery.darchrow.util.Validator;

/**
 * {@link java.lang.Class} .
 * 
 * <h3> Class {@link java.lang.Class#getCanonicalName() getCanonicalName()} VS {@link java.lang.Class#getName() getName()} VS
 * {@link java.lang.Class#getSimpleName() getSimpleName()}</h3>
 * 
 * <blockquote>
 * <p>
 * ?class  {@link com.baozun.nebulaplus.date.DatePattern}
 * </p>
 * 
 * <table border="1" cellspacing="0" cellpadding="4">
 * <tr style="background-color:#ccccff">
 * <th align="left"></th>
 * <th align="left"></th>
 * </tr>
 * <tr valign="top">
 * <td>{@link java.lang.Class#getCanonicalName() getCanonicalName()}<br>
 *  Java Language Specification ??</td>
 * <td>"com.feilong.core.date.DatePattern"</td>
 * </tr>
 * <tr valign="top" style="background-color:#eeeeff">
 * <td>{@link java.lang.Class#getName() getName()}</td>
 * <td>"com.feilong.core.date.DatePattern"</td>
 * </tr>
 * <tr valign="top">
 * <td>{@link java.lang.Class#getSimpleName() getSimpleName()}</td>
 * <td>"DatePattern"</td>
 * </tr>
 * </table>
 * 
 * <p>
 * {@link java.lang.Class#getCanonicalName() getCanonicalName()} && {@link java.lang.Class#getName() getName()} ,class?,??
 * <br>
 * array?
 * </p>
 * <ul>
 * <li>{@link java.lang.Class#getName() getName()} <b>[[Ljava.lang.String</b>?</li>
 * <li> {@link java.lang.Class#getCanonicalName() getCanonicalName()} ?</li>
 * </ul>
 * 
 * </blockquote>
 * 
 * <h3> {@link #loadClass(String)}:</h3>
 * 
 * <blockquote>
 * 
 * <table border="1" cellspacing="0" cellpadding="4">
 * <tr style="background-color:#ccccff">
 * <th align="left"></th>
 * <th align="left"></th>
 * </tr>
 * <tr valign="top">
 * <td><code>Class klass=o.{@link java.lang.Object#getClass() getClass()};</code></td>
 * <td>o?(:????)OClass.<br>
 * ?O??.</td>
 * </tr>
 * <tr valign="top" style="background-color:#eeeeff">
 * <td><code>Class klass=A.class;</code></td>
 * <td>JVMA,A(???:A),?A??.<br>
 * AClass.</td>
 * </tr>
 * <tr valign="top">
 * <td><code>Class klass={@link java.lang.Class#forName(String) Class.forName}("??");</code></td>
 * <td>?.</td>
 * </tr>
 * <tr valign="top" style="background-color:#eeeeff">
 * <td><code>Class klass={@link java.lang.ClassLoader#loadClass(String) ClassLoader.loadClass}("??");</code></td>
 * <td>???.</td>
 * </tr>
 * </table>
 * </blockquote>
 * 
 * @author feilong
 * @version 1.0.0 2012-6-1 ?7:19:47
 * @version 1.2.1 2015-6-21 20:50 update javadoc
 * @since 1.0.0
 * @see java.lang.Class
 * @see org.apache.commons.lang3.ClassUtils
 */
public final class ClassUtil {

    /** Don't let anyone instantiate this class. */
    private ClassUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     *  class info map for LOGGER.
     *
     * @param klass
     *            the clz
     * @return the map for log
     */
    public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
        if (Validator.isNullOrEmpty(klass)) {
            return null;
        }

        Map<String, Object> map = new LinkedHashMap<String, Object>();

        map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern"
        map.put("clz.getName()", klass.getName());//"com.feilong.core.date.DatePattern"
        map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

        map.put("clz.getComponentType()", klass.getComponentType());
        // ?? voidboolean?byte?char?short?int?long?float  double?
        map.put("clz.isPrimitive()", klass.isPrimitive());

        // ??,
        map.put("clz.isLocalClass()", klass.isLocalClass());
        // ????,??????
        map.put("clz.isMemberClass()", klass.isMemberClass());

        //isSynthetic()?Class????java??false?trueJVM???java??????
        map.put("clz.isSynthetic()", klass.isSynthetic());
        map.put("clz.isArray()", klass.isArray());
        map.put("clz.isAnnotation()", klass.isAnnotation());

        //??true
        map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
        map.put("clz.isEnum()", klass.isEnum());

        return map;
    }

    /**
     * .
     * 
     * <blockquote>
     * 
     * <table border="1" cellspacing="0" cellpadding="4">
     * <tr style="background-color:#ccccff">
     * <th align="left"></th>
     * <th align="left"></th>
     * </tr>
     * <tr valign="top">
     * <td><code>Class klass=o.getClass();</code></td>
     * <td>o?(:????)OClass.<br>
     * ?O??.</td>
     * </tr>
     * <tr valign="top" style="background-color:#eeeeff">
     * <td><code>Class klass=A.class;</code></td>
     * <td>JVMA,A(???:A),?A??.<br>
     * AClass.</td>
     * </tr>
     * <tr valign="top">
     * <td><code>Class klass=Class.forName("??");</code></td>
     * <td>?.</td>
     * </tr>
     * <tr valign="top" style="background-color:#eeeeff">
     * <td><code>Class klass=ClassLoader.loadClass("??");</code></td>
     * <td>???.</td>
     * </tr>
     * </table>
     * </blockquote>
     * 
     * @param className
     *            ??+?? "org.jfree.chart.ChartFactory"
     * @return the class
     * @throws ClassNotFoundException
     *             the class not found exception
     * @since 1.0.7
     */
    public static Class<?> loadClass(String className) throws ClassNotFoundException {
        return Class.forName(className);// JVM
    }

    /**
     * ??.
     * 
     * @param obj
     *            
     * @param klass
     *            
     * @return  obj  true
     * @see java.lang.Class#isInstance(Object)
     */
    public static boolean isInstance(Object obj, Class<?> klass) {
        return klass.isInstance(obj);
    }

    /**
     * ??.
     * 
     * @param ownerClass
     *            class
     * @return true
     * @see java.lang.Class#getModifiers()
     * @see java.lang.reflect.Modifier#isInterface(int)
     */
    public static boolean isInterface(Class<?> ownerClass) {
        // ?? Java 
        int flag = ownerClass.getModifiers();
        // ??
        return Modifier.isInterface(flag);
    }

    /**
     * ??,?,? paramValues null  null.
     * 
     * @param paramValues
     *            ?
     * @return ? paramValues null  null
     * @see org.apache.commons.lang3.ClassUtils#toClass(Object...)
     * @since 1.1.1
     */
    public static Class<?>[] toClass(Object... paramValues) {
        return org.apache.commons.lang3.ClassUtils.toClass(paramValues);
    }
}