📄 objecttype.java
字号:
/** * Tests if a class is a class of or a sub-class of the parent * @param objectClass Class to test * @param parentClass Class to test against * @return */ public static boolean isOrSubOf(Class objectClass, Class parentClass) { while (objectClass != null) { if (objectClass == parentClass) return true; objectClass = objectClass.getSuperclass(); } return false; } /** * Tests if a class is a class of a sub-class of or properly implements an interface * @param objectClass Class to test * @param typeObject Object to test against * @return */ public static boolean instanceOf(Class objectClass, Object typeObject) { Class typeClass = typeObject.getClass(); return instanceOf(objectClass, typeClass); } /** * Tests if a class is a class of a sub-class of or properly implements an interface * @param objectClass Class to test * @param typeName name to test against * @return */ public static boolean instanceOf(Class objectClass, String typeName) { return instanceOf(objectClass, typeName, null); } /** * Tests if an object is an instance of a sub-class of or properly implements an interface * @param obj Object to test * @param typeObject Object to test against * @return */ public static boolean instanceOf(Object obj, Object typeObject) { Class typeClass = typeObject.getClass(); return instanceOf(obj, typeClass); } /** * Tests if an object is an instance of a sub-class of or properly implements an interface * @param obj Object to test * @param typeName name to test against * @return */ public static boolean instanceOf(Object obj, String typeName) { return instanceOf(obj, typeName, null); } /** * Tests if a class is a class of a sub-class of or properly implements an interface * @param objectClass Class to test * @param typeName Object to test against * @param loader * @return */ public static boolean instanceOf(Class objectClass, String typeName, ClassLoader loader) { Class infoClass = loadInfoClass(typeName, loader); if (infoClass == null) throw new IllegalArgumentException("Illegal type found in info map (could not load class for specified type)"); return instanceOf(objectClass, infoClass); } /** * Tests if an object is an instance of a sub-class of or properly implements an interface * @param obj Object to test * @param typeName Object to test against * @param loader * @return */ public static boolean instanceOf(Object obj, String typeName, ClassLoader loader) { Class infoClass = loadInfoClass(typeName, loader); if (infoClass == null) throw new IllegalArgumentException("Illegal type found in info map (could not load class for specified type)"); return instanceOf(obj, infoClass); } public static Class loadInfoClass(String typeName, ClassLoader loader) { //Class infoClass = null; try { return ObjectType.loadClass(typeName, loader); } catch (SecurityException se1) { throw new IllegalArgumentException("Problems with classloader: security exception (" + se1.getMessage() + ")"); } catch (ClassNotFoundException e1) { try { return ObjectType.loadClass(LANG_PACKAGE + typeName, loader); } catch (SecurityException se2) { throw new IllegalArgumentException("Problems with classloader: security exception (" + se2.getMessage() + ")"); } catch (ClassNotFoundException e2) { try { return ObjectType.loadClass(SQL_PACKAGE + typeName, loader); } catch (SecurityException se3) { throw new IllegalArgumentException("Problems with classloader: security exception (" + se3.getMessage() + ")"); } catch (ClassNotFoundException e3) { throw new IllegalArgumentException("Cannot find and load the class of type: " + typeName + " or of type: " + LANG_PACKAGE + typeName + " or of type: " + SQL_PACKAGE + typeName + ": (" + e3.getMessage() + ")"); } } } } /** * Tests if an object is an instance of a sub-class of or properly implements an interface * @param obj Object to test * @param typeClass Class to test against * @return */ public static boolean instanceOf(Object obj, Class typeClass) { if (obj == null) return true; Class objectClass = obj.getClass(); return instanceOf(objectClass, typeClass); } /** * Tests if a class is a class of a sub-class of or properly implements an interface * @param objectClass Class to test * @param typeClass Class to test against * @return */ public static boolean instanceOf(Class objectClass, Class typeClass) { if (typeClass.isInterface()) { return interfaceOf(objectClass, typeClass); } else { return isOrSubOf(objectClass, typeClass); } } /** * Converts the passed object to the named simple type; supported types * include: String, Boolean, Double, Float, Long, Integer, Date (java.sql.Date), * Time, Timestamp; * @param obj Object to convert * @param type Name of type to convert to * @param format Optional (can be null) format string for Date, Time, Timestamp * @param locale Optional (can be null) Locale for formatting and parsing Double, Float, Long, Integer * @param noTypeFail Fail (Exception) when no type conversion is available, false will return the primary object * @return * @throws GeneralException */ public static Object simpleTypeConvert(Object obj, String type, String format, Locale locale, boolean noTypeFail) throws GeneralException { if (obj == null) { return null; } if (obj.getClass().getName().equals(type)) { return obj; } if ("PlainString".equals(type)) { return obj.toString(); } if ("Object".equals(type) || "java.lang.Object".equals(type)) { return obj; } String fromType = null; if (obj instanceof java.lang.String) { fromType = "String"; String str = (String) obj; if ("String".equals(type) || "java.lang.String".equals(type)) { return obj; } if (str.length() == 0) { return null; } if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { Boolean value = null; if (str.equalsIgnoreCase("TRUE")) value = new Boolean(true); else value = new Boolean(false); return value; } else if ("Locale".equals(type) || "java.util.Locale".equals(type)) { Locale loc = UtilMisc.parseLocale(str); if (loc != null) { return loc; } else { throw new GeneralException("Could not convert " + str + " to " + type + ": "); } } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { try { NumberFormat nf = null; if (locale == null) { nf = NumberFormat.getNumberInstance(); } else { nf = NumberFormat.getNumberInstance(locale); } Number tempNum = nf.parse(str); return new BigDecimal(tempNum.toString()); } catch (ParseException e) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { try { NumberFormat nf = null; if (locale == null) { nf = NumberFormat.getNumberInstance(); } else { nf = NumberFormat.getNumberInstance(locale); } Number tempNum = nf.parse(str); return new Double(tempNum.doubleValue()); } catch (ParseException e) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { try { NumberFormat nf = null; if (locale == null) { nf = NumberFormat.getNumberInstance(); } else { nf = NumberFormat.getNumberInstance(locale); } Number tempNum = nf.parse(str); return new Float(tempNum.floatValue()); } catch (ParseException e) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { try { NumberFormat nf = null; if (locale == null) { nf = NumberFormat.getNumberInstance(); } else { nf = NumberFormat.getNumberInstance(locale); } nf.setMaximumFractionDigits(0); Number tempNum = nf.parse(str); return new Long(tempNum.longValue()); } catch (ParseException e) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { try { NumberFormat nf = null; if (locale == null) { nf = NumberFormat.getNumberInstance(); } else { nf = NumberFormat.getNumberInstance(locale); } nf.setMaximumFractionDigits(0); Number tempNum = nf.parse(str); return new Integer(tempNum.intValue()); } catch (ParseException e) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { if (format == null || format.length() == 0) { try { return java.sql.Date.valueOf(str); } catch (Exception e) { try { DateFormat df = null; if (locale != null) { df = DateFormat.getDateInstance(DateFormat.SHORT, locale); } else { df = DateFormat.getDateInstance(DateFormat.SHORT); } Date fieldDate = df.parse(str); return new java.sql.Date(fieldDate.getTime()); } catch (ParseException e1) { throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); } } } else { try { SimpleDateFormat sdf = new SimpleDateFormat(format); java.util.Date fieldDate = sdf.parse(str); return new java.sql.Date(fieldDate.getTime()); } catch (ParseException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -