Java tutorial
/* * 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.sunchenbin.store.feilong.core.lang; import java.lang.reflect.Modifier; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import com.sunchenbin.store.feilong.core.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.sunchenbin.store.feilong.core.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.sunchenbin.store.feilong.core.date.DatePattern"</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link java.lang.Class#getName() getName()}</td> * <td>"com.sunchenbin.store.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> * * <h3>instanceof?/isAssignableFrom/isInstance(Object obj) </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>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 :? -----> <br> * isAssignableFrom : -----> ? * </p> * </blockquote> * * @author feilong * @version 1.0.0 2012-6-1 ?7:19:47 * @version 1.2.1 2015-6-21 20:50 update javadoc * @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!"); } /** * 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 Collections.emptyMap(); } Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.sunchenbin.store.feilong.core.date.DatePattern" map.put("clz.getName()", klass.getName());//"com.sunchenbin.store.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,?true,JVM???,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 } /** * ??. * * <h3>instanceof?/isAssignableFrom/isInstance(Object obj) </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>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 :? -----> <br> * isAssignableFrom : -----> ? * </p> * </blockquote> * * @param obj * * @param klass * * @return obj , true; if <code>null == klass</code> return false * @see java.lang.Class#isInstance(Object) */ public static boolean isInstance(Object obj, Class<?> klass) { if (null == klass) { return false; } return klass.isInstance(obj); } /** * obj ?isInstance ? <code>klasses</code>. * * @param obj * the obj * @param klasses * the klasses * @return true, if checks if is instance; if <code>null == klasses</code> return false * @since 1.4.0 */ public static boolean isInstance(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 :? -----> <br> * isAssignableFrom : -----> ? * </p> * * @param klass * the klass * @param cls * the cls * @return true, if checks if is assignable from * @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 klass.isAssignableFrom(cls); } /** * ??. * * @param ownerClass * class * @return true * @see java.lang.Class#getModifiers() * @see java.lang.reflect.Modifier#isInterface(int) */ public static boolean isInterface(Class<?> ownerClass) { int flag = ownerClass.getModifiers();// ?? Java 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); } }