📄 objecttype.java
字号:
/* * $Id: ObjectType.java 5570 2005-08-22 05:33:45Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.base.util;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.math.BigDecimal;import java.text.DateFormat;import java.text.NumberFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Map;import javolution.util.FastMap;/** * Utilities for analyzing and converting Object types in Java * Takes advantage of reflection * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:gielen@aixcept.de">Rene Gielen</a> * @version $Rev: 5570 $ * @since 2.0 */public class ObjectType { public static final String module = ObjectType.class.getName(); public static final Object NULL = new NullObject(); protected static Map classCache = new FastMap(); public static final String LANG_PACKAGE = "java.lang."; // We will test both the raw value and this + raw value public static final String SQL_PACKAGE = "java.sql."; // We will test both the raw value and this + raw value /** * Loads a class with the current thread's context classloader * @param className The name of the class to load * @return The requested class * @throws ClassNotFoundException */ public static Class loadClass(String className) throws ClassNotFoundException { // small block to speed things up by putting using preloaded classes for common objects, this turns out to help quite a bit... Class theClass = (Class) CachedClassLoader.globalClassNameClassMap.get(className); if (theClass != null) return theClass; return loadClass(className, null); } /** * Loads a class with the current thread's context classloader * @param className The name of the class to load * @param loader The ClassLoader to su * @return The requested class * @throws ClassNotFoundException */ public static Class loadClass(String className, ClassLoader loader) throws ClassNotFoundException { // small block to speed things up by putting using preloaded classes for common objects, this turns out to help quite a bit... Class theClass = (Class) CachedClassLoader.globalClassNameClassMap.get(className); if (theClass != null) return theClass; if (loader == null) loader = Thread.currentThread().getContextClassLoader(); try { theClass = loader.loadClass(className); } catch (Exception e) { theClass = (Class) classCache.get(className); if (theClass == null) { synchronized (ObjectType.class) { theClass = (Class) classCache.get(className); if (theClass == null) { theClass = Class.forName(className); if (theClass != null) { if (Debug.verboseOn()) Debug.logVerbose("Loaded Class: " + theClass.getName(), module); classCache.put(className, theClass); } } } } } return theClass; } /** * Returns an instance of the specified class * @param className Name of the class to instantiate * @return An instance of the named class * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException */ public static Object getInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class c = loadClass(className); Object o = c.newInstance(); if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module); return o; } /** * Tests if a class properly implements the specified interface * @param objectClass Class to test * @param interfaceName Name of the interface to test against * @return boolean indicating whether interfaceName is an interface of the obj * @throws ClassNotFoundException */ public static boolean interfaceOf(Class objectClass, String interfaceName) throws ClassNotFoundException { Class interfaceClass = loadClass(interfaceName); return interfaceOf(objectClass, interfaceClass); } /** * Tests if a class properly implements the specified interface * @param objectClass Class to test * @param interfaceObject to test against * @return boolean indicating whether interfaceObject is an interface of the obj */ public static boolean interfaceOf(Class objectClass, Object interfaceObject) { Class interfaceClass = interfaceObject.getClass(); return interfaceOf(objectClass, interfaceClass); } /** * Returns an instance of the specified class using the constructor matching the specified parameters * @param className Name of the class to instantiate * @param parameters Parameters passed to the constructor * @return An instance of the named class * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException */ public static Object getInstance(String className, Object[] parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] sig = new Class[parameters.length]; for (int i = 0; i < sig.length; i++) { sig[i] = parameters[i].getClass(); } Class c = loadClass(className); Constructor con = c.getConstructor(sig); Object o = con.newInstance(parameters); if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module); return o; } /** * Tests if an object properly implements the specified interface * @param obj Object to test * @param interfaceName Name of the interface to test against * @return boolean indicating whether interfaceName is an interface of the obj * @throws ClassNotFoundException */ public static boolean interfaceOf(Object obj, String interfaceName) throws ClassNotFoundException { Class interfaceClass = loadClass(interfaceName); return interfaceOf(obj, interfaceClass); } /** * Tests if an object properly implements the specified interface * @param obj Object to test * @param interfaceObject to test against * @return boolean indicating whether interfaceObject is an interface of the obj */ public static boolean interfaceOf(Object obj, Object interfaceObject) { Class interfaceClass = interfaceObject.getClass(); return interfaceOf(obj, interfaceClass); } /** * Tests if an object properly implements the specified interface * @param obj Object to test * @param interfaceClass Class to test against * @return boolean indicating whether interfaceClass is an interface of the obj */ public static boolean interfaceOf(Object obj, Class interfaceClass) { Class objectClass = obj.getClass(); return interfaceOf(objectClass, interfaceClass); } /** * Tests if a class properly implements the specified interface * @param objectClass Class to test * @param interfaceClass Class to test against * @return boolean indicating whether interfaceClass is an interface of the obj */ public static boolean interfaceOf(Class objectClass, Class interfaceClass) { while (objectClass != null) { Class[] ifaces = objectClass.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { if (ifaces[i] == interfaceClass) return true; } objectClass = objectClass.getSuperclass(); } return false; } /** * Tests if a class is a class of or a sub-class of the parent * @param objectClass Class to test * @param parentName Name of the parent class to test against * @return * @throws ClassNotFoundException */ public static boolean isOrSubOf(Class objectClass, String parentName) throws ClassNotFoundException { Class parentClass = loadClass(parentName); return isOrSubOf(objectClass, parentClass); } /** * Tests if a class is a class of or a sub-class of the parent * @param objectClass Class to test * @param parentObject Object to test against * @return */ public static boolean isOrSubOf(Class objectClass, Object parentObject) { Class parentClass = parentObject.getClass(); return isOrSubOf(objectClass, parentClass); } /** * Tests if an object is an instance of or a sub-class of the parent * @param obj Object to test * @param parentName Name of the parent class to test against * @return * @throws ClassNotFoundException */ public static boolean isOrSubOf(Object obj, String parentName) throws ClassNotFoundException { Class parentClass = loadClass(parentName); return isOrSubOf(obj, parentClass); } /** * Tests if an object is an instance of or a sub-class of the parent * @param obj Object to test * @param parentObject Object to test against * @return */ public static boolean isOrSubOf(Object obj, Object parentObject) { Class parentClass = parentObject.getClass(); return isOrSubOf(obj, parentClass); } /** * Tests if an object is an instance of or a sub-class of the parent * @param obj Object to test * @param parentClass Class to test against * @return */ public static boolean isOrSubOf(Object obj, Class parentClass) { Class objectClass = obj.getClass(); return isOrSubOf(objectClass, parentClass); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -