com.feilong.core.lang.ClassUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.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.feilong.core.lang;

import java.lang.reflect.Modifier;

import org.apache.commons.lang3.Validate;

import com.feilong.core.lang.reflect.ReflectException;

/**
 * {@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.feilong.core.DatePattern}
 * </p>
 * 
 * <table border="1" cellspacing="0" cellpadding="4" summary="">
 * <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>instanceof?/isAssignableFrom/isInstance(Object obj) </h3>
 * 
 * <blockquote>
 * <table border="1" cellspacing="0" cellpadding="4" summary="">
 * <tr style="background-color:#ccccff">
 * <th align="left"></th>
 * <th align="left"></th>
 * </tr>
 * <tr valign="top">
 * <td>instanceof?</td>
 * <td>,??????<br>
 * ?:oo instanceof TypeName<br>
 * ???,??????<br>
 * instanceofJava?,{@code ==,>,<}?,??,boolean?</td>
 * </tr>
 * <tr valign="top" style="background-color:#eeeeff">
 * <td>isAssignableFrom</td>
 * <td>class,?Class1?Class2????.<br>
 * ?Class1.isAssignableFrom(Class2)<br>
 * ?java.lang.Class.</td>
 * </tr>
 * <tr valign="top">
 * <td>isInstance(Object obj)</td>
 * <td>obj,objclass? ,true.<br>
 * instanceof??</td>
 * </tr>
 * </table>
 * 
 * <p>
 * instanceof :? {@code ----->}  <br>
 * isAssignableFrom : {@code ----->} ?
 * </p>
 * </blockquote>
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see org.apache.commons.lang3.ClassUtils
 * @since 1.0.0
 */
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!");
    }

    /**
     *  <code>obj</code> ?? <code>klass</code> .
     * 
     * <h3>instanceof?/isAssignableFrom/isInstance(Object obj) </h3>
     * 
     * <blockquote>
     * <table border="1" cellspacing="0" cellpadding="4" summary="">
     * <tr style="background-color:#ccccff">
     * <th align="left"></th>
     * <th align="left"></th>
     * </tr>
     * <tr valign="top">
     * <td>instanceof?</td>
     * <td>,??????<br>
     * ?:oo instanceof TypeName<br>
     * ???,??????<br>
     * instanceofJava?,{@code ==,>,<}?,??,boolean?</td>
     * </tr>
     * <tr valign="top" style="background-color:#eeeeff">
     * <td>isAssignableFrom</td>
     * <td>class,?Class1?Class2????.<br>
     * ?Class1.isAssignableFrom(Class2)<br>
     * ?java.lang.Class.</td>
     * </tr>
     * <tr valign="top">
     * <td>isInstance(Object obj)</td>
     * <td>obj,objclass? ,true.<br>
     * instanceof? <span style="color:red">?</span></td>
     * </tr>
     * </table>
     * 
     * <p>
     * instanceof :? {@code ----->}  <br>
     * isAssignableFrom : {@code ----->} ?
     * </p>
     * </blockquote>
     * 
     * @param obj
     *            
     * @param klass
     *            
     * @return  obj , true;<br>
     *          <code>null == klass</code>  false
     * @see java.lang.Class#isInstance(Object)
     */
    public static boolean isInstance(Object obj, Class<?> klass) {
        return null == klass ? false : klass.isInstance(obj);
    }

    /**
     *  <code>obj</code> ?isInstance ? <code>klasses</code>.
     *
     * @param obj
     *            the obj
     * @param klasses
     *            the klasses
     * @return  <code>null == klasses</code> , false<br>
     *         true, if checks if is instance;
     * @since 1.5.6
     */
    public static boolean isInstanceAnyClass(Object obj, Class<?>... klasses) {
        if (null == klasses) {
            return false;
        }

        for (Class<?> klass : klasses) {
            if (isInstance(obj, klass)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks if is assignable from.
     * 
     * <p>
     * instanceof :? {@code ----->}  <br>
     * isAssignableFrom : {@code ----->} ?
     * </p>
     *
     * @param klass
     *            the klass
     * @param cls
     *            the cls
     * @return  <code>klass</code> null,false<br>
     *          <code>cls</code> null,false
     * @see java.lang.Class#isAssignableFrom(Class)
     * @see org.apache.commons.lang3.ClassUtils#isAssignable(Class, Class)
     * @since 1.4.0
     */
    public static boolean isAssignableFrom(Class<?> klass, Class<?> cls) {
        return (null == klass || null == cls) ? false : klass.isAssignableFrom(cls);
    }

    /**
     *  <code>ownerClass</code> ??.
     * 
     * @param ownerClass
     *            class
     * @return true<br>
     *          <code>ownerClass</code> null,false
     * @see java.lang.Class#getModifiers()
     * @see java.lang.reflect.Modifier#isInterface(int)
     */
    public static boolean isInterface(Class<?> ownerClass) {
        return null == ownerClass ? false : Modifier.isInterface(ownerClass.getModifiers());// ??
    }

    /**
     * ??,?.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * assertArrayEquals(new Class[] { String.class, String.class }, ClassUtil.toClass("a", "a"));
     * assertArrayEquals(new Class[] { Integer.class, Boolean.class }, ClassUtil.toClass(1, true));
     * </pre>
     * 
     * </blockquote>
     * 
     * @param paramValues
     *            ?
     * @return  <code>paramValues</code> null, null<br>
     * @see org.apache.commons.lang3.ClassUtils#toClass(Object...)
     * @see org.apache.commons.lang3.ClassUtils#convertClassNamesToClasses(java.util.List)
     * @since 1.1.1
     */
    public static Class<?>[] toClass(Object... paramValues) {
        return null == paramValues ? null : org.apache.commons.lang3.ClassUtils.toClass(paramValues);
    }

    /**
     * JVM.
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * 
     * FeiLongVersion feiLongVersion = ClassUtil.loadClass("com.feilong.core.FeiLongVersion");
     * </pre>
     * 
     * </blockquote>
     * 
     * <h3> {@link Class#forName(String)}:</h3>
     * <blockquote>
     * <ol>
     * 
     * <li> = {@link Class#forName(String)}</li>
     * 
     * <li>Returns the class represented by {@code className} using the {@code classLoader}. <br>
     * ? " {@code java.util.Map.Entry[]}", "{@code java.util.Map$Entry[]}", "{@code [Ljava.util.Map.Entry;}", and
     * "{@code [Ljava.util.Map$Entry;}".</li>
     * 
     * <li>
     * <p>
     * ???class:
     * </p>
     * 
     * <ul>
     * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}</li>
     * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}</li>
     * </ul>
     * 
     * </li>
     * </ol>
     * </blockquote>
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <table border="1" cellspacing="0" cellpadding="4" summary="">
     * <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?<b></b>(:????)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><b>?</b>.</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>
     * 
     * @param className
     *            ??+??, "com.feilong.core.FeiLongVersion"
     * @return  <code>className</code> null, {@link NullPointerException}<br>
     *          <code>className</code> blank, {@link IllegalArgumentException}<br>
     *          <code>className</code> ?, {@link ReflectException}
     * @see java.lang.ClassLoader#loadClass(String)
     * @see java.lang.Class#forName(String)
     * @see java.lang.Class#forName(String, boolean, ClassLoader)
     * @see org.apache.commons.lang3.ClassUtils#getClass(String)
     * @see org.apache.commons.lang3.ClassUtils#getClass(ClassLoader, String, boolean)
     * @see "org.springframework.util.ClassUtils#forName(String, ClassLoader)"
     * @since 1.6.2
     */
    public static Class<?> getClass(String className) {
        Validate.notBlank(className, "className can't be blank!");
        try {
            return org.apache.commons.lang3.ClassUtils.getClass(className);
        } catch (ClassNotFoundException e) {
            throw new ReflectException(e);
        }
    }

}