Java tutorial
/* * Copyright (C) 2008 feilong (venusdrogon@163.com) * * 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.commons.core.lang; import java.lang.reflect.Modifier; import java.util.LinkedHashMap; import java.util.Map; /** * {@link java.lang.Class} . * * @author <a href="mailto:venusdrogon@163.com"></a> * @version 1.0 2012-6-1 ?7:19:47 * @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 log. * * @param klass * the clz * @return the map for log */ public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) { Map<String, Object> map = new LinkedHashMap<String, Object>(); //"clz.getCanonicalName()": "com.feilong.commons.core.date.DatePattern", //"clz.getName()": "com.feilong.commons.core.date.DatePattern", //"clz.getSimpleName()": "DatePattern", // getCanonicalName ( Java Language Specification ??) && getName //??class?array? // getName[[Ljava.lang.String?getCanonicalName? map.put("clz.getCanonicalName()", klass.getCanonicalName()); map.put("clz.getName()", klass.getName()); map.put("clz.getSimpleName()", klass.getSimpleName()); 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; // Class<?> klass = this.getClass(); // // TypeVariable<?>[] typeParameters = klass.getTypeParameters(); // TypeVariable<?> typeVariable = typeParameters[0]; // // Type[] bounds = typeVariable.getBounds(); // // Type bound = bounds[0]; // // if (log.isDebugEnabled()){ // log.debug("" + (bound instanceof ParameterizedType)); // } // // Class<T> modelClass = ReflectUtil.getGenericModelClass(klass); // return modelClass; } /** * . * * <ol> * <li>Class cl=o.getClass();<br> * o?(:????)OClass.<br> * ?O??.</li> * <li>Class cl=A.class;<br> * JVMA,A(???:A),?A??.<br> * AClass.</li> * <li>Class cl=Class.forName("??");<br> * ?.</li> * <li>Class cl=ClassLoader.loadClass("??");<br> * ???.</li> * </ol> * * @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); } }