📄 classes.java
字号:
} //try any is if ((flags&B_SET) == 0) try { return getAnyMethod(cls, isMethodName, argTypes); } catch (NoSuchMethodException ex) { //ignore } //try any same try { return getAnyMethod(cls, name, argTypes); } catch (NoSuchMethodException ex) { if ((flags & B_METHOD_ONLY) != 0) throw ex; } } if (argTypes != null && argTypes.length > 1) throw new NoSuchMethodException(cls+": "+name+" "+Objects.toString(argTypes)); try { //try public field try { return cls.getField(name); } catch (NoSuchFieldException ex) { //ignore if ((flags & B_PUBLIC_ONLY) != 0) throw ex; } //try any field return getAnyField(cls, name); } catch (NoSuchFieldException ex) { //ignore throw new NoSuchMethodException(cls+": name="+name+" args="+Objects.toString(argTypes)); } } /** The infomation of the access object. */ private static class AOInfo { private Class cls; private String name; private Class[] argTypes; private int flags; private AOInfo(Class cls, String name, Class[] argTypes, int flags) { this.cls = cls; this.name = name; this.argTypes = argTypes; this.flags = flags; } public int hashCode() { return cls.hashCode() + name.hashCode() + flags; } public boolean equals(Object o) { if (!(o instanceof AOInfo)) return false; AOInfo aoi = (AOInfo)o; int len = argTypes != null ? argTypes.length: 0; int len2 = aoi.argTypes != null ? aoi.argTypes.length: 0; if (len != len2) return false; if (flags != aoi.flags || !cls.equals(aoi.cls) || !name.equals(aoi.name)) return false; for (int j = 0; j < len; ++j) if (!Objects.equals(argTypes[j], aoi.argTypes[j])) return false; return true; } } /** * Gets the specified method by searching all methods including * <i>any</i> access control and any base class. * Note: you rarely need to call this metod. In most cases, * Class.getMethod, {@link #getCloseMethod}, and * {@link #getMethodInPublic} are what you need. * * <p>The search sequence is: this class's methods, and then * the superclass's methods. * * <p>Note: public methods don't be treated different. If the caller * wants to search public methods first, it has to call Class.getMethod * first. * * @param cls the class to search * @param name the method name * @param argTypes the parameter array of types * @return the Method object * @exception NoSuchMethodException if a matching method is not found. * @exception SecurityException if access to the information is denied. * @see #getAccessibleObject(Class, String, Class[], int) * @see #getAnyField(Class, String) */ public static final Method getAnyMethod(Class cls, String name, Class[] argTypes) throws NoSuchMethodException { try { return cls.getDeclaredMethod(name, argTypes); } catch (NoSuchMethodException ex) { final Class[] clses = cls.getInterfaces(); for (int j = 0; j< clses.length; ++j) try { return getAnyMethod(clses[j], name, argTypes); } catch (NoSuchMethodException e2) { //ignore it } cls = cls.getSuperclass(); if (cls == null) throw ex; return getAnyMethod(cls, name, argTypes); } } /** * Gets the specified field by searching all fields including * any access control and any base class. * The search sequence is: this class's fields, and then * the superclass's fields. * * <p>Note: public fields don't be treated different. If the caller * wants to search public fields first, it has to call Class.getField * first. * * @param cls the class to search * @param name the field name * @return the Field object * @exception NoSuchFieldException if a matching field is not found. * @exception SecurityException if access to the information is denied. * @see #getAccessibleObject(Class, String, Class[], int) * @see #getAnyMethod(Class, String, Class[]) */ public static final Field getAnyField(Class cls, String name) throws NoSuchFieldException { for (;;) { try { return cls.getDeclaredField(name); } catch (NoSuchFieldException ex) { cls = cls.getSuperclass(); if (cls == null) throw ex; } } } /** * Searches thru each element of the specified array of classes, and * returns classes that are super-classes (or equal) of * the sepcified class. * * @param cls the specified class; null is not allowed * @param clsToCheck the class array to check; null is acceptable * @return a subset of clsToCheck that are super-class of cls; * null if no one qualified */ public static final Class[] getSuperClasses(Class cls, Class[] clsToCheck) { if (clsToCheck!=null) { int[] hits = new int[clsToCheck.length]; int no = 0; for (int j=0; j<clsToCheck.length; ++j) if (clsToCheck[j].isAssignableFrom(cls)) hits[no++] = j; if (no != clsToCheck.length) { if (no == 0) return null; Class[] exc = new Class[no]; for (int j=0; j<no; ++j) exc[j] = clsToCheck[hits[j]]; return exc; } } return clsToCheck; } /** * Check whether the specified class is a primitive or a primitive wrapper. */ public static final boolean isPrimitiveWrapper(Class cls) { return Objects.equals(cls.getPackage(), Boolean.class.getPackage()) && (cls.equals(Boolean.class) || cls.equals(Byte.class) || cls.equals(Character.class) || cls.equals(Double.class) || cls.equals(Float.class) || cls.equals(Integer.class) || cls.equals(Long.class) || cls.equals(Short.class)); } /** Checks whether the specified class is a numeric class. * * @param extend whether to consider Date, char, boolean, Character * and Boolean as a numeric object. */ public static final boolean isNumeric(Class cls, boolean extend) { if (cls.isPrimitive()) return extend || (!cls.equals(char.class) && !cls.equals(boolean.class)); if (Number.class.isAssignableFrom(cls)) return true; return extend && (cls.equals(Date.class) || cls.equals(Boolean.class) || cls.equals(Character.class)); } /** Converts an object to the specified class. * It is the same as coerce(cls, val, true). * * <p>Future: use org.apache.commons.el.Coercions * * @param val the value. * @exception ClassCastException if failed to convert * @see #coerce(Class, Object, boolean) */ public static Object coerce(Class cls, Object val) throws ClassCastException { if (cls.isInstance(val)) return val; if (String.class == cls) { return Objects.toString(val); } else if (BigDecimal.class == cls) { if (val == null) { return null; } else if (val instanceof Double) { return new BigDecimal(((Double)val).doubleValue()); } else if (val instanceof Float) { return new BigDecimal(((Float)val).doubleValue()); } else if (val instanceof BigInteger) { return new BigDecimal((BigInteger)val); } else if (val instanceof Number) { return BigDecimals.toBigDecimal(((Number)val).intValue()); } else if (val instanceof String) { return new BigDecimal((String)val); } else if (val instanceof Date) { return new BigDecimal(((Date)val).getTime()); } } else if (Integer.class == cls || int.class == cls) { if (val == null) { return Integer.class == cls ? null: Objects.ZERO_INTEGER; } else if (val instanceof Integer) { //int.class return val; } else if (val instanceof Number) { return new Integer(((Number)val).intValue()); } else if (val instanceof String) { return new Integer((String)val); } } else if (Boolean.class == cls || boolean.class == cls) { if (val == null) { return Boolean.class == cls ? null: Boolean.FALSE; } else if (val instanceof Boolean) { //boolean.class return val; } else if (val instanceof String) { return Boolean.valueOf((String)val); } else if (val instanceof BigDecimal) { return Boolean.valueOf(((BigDecimal)val).signum() != 0); } else if (val instanceof BigInteger) { return Boolean.valueOf(((BigInteger)val).signum() != 0); } else if (val instanceof Number) { return Boolean.valueOf(((Number)val).intValue() != 0); } else { return Boolean.TRUE; //non-null is true } } else if (Short.class == cls || short.class == cls) { if (val == null) { return Short.class == cls ? null: Objects.ZERO_SHORT; } else if (val instanceof Short) { //short.class return val; } else if (val instanceof Number) { return new Short(((Number)val).shortValue()); } else if (val instanceof String) { return new Short((String)val); } } else if (Long.class == cls || long.class == cls) { if (val == null) { return Long.class == cls ? null: Objects.ZERO_LONG; } else if (val instanceof Long) { //long.class return val; } else if (val instanceof Number) { return new Long(((Number)val).longValue()); } else if (val instanceof String) { return new Long((String)val); } else if (val instanceof Date) { return new Long(((Date)val).getTime()); } } else if (Double.class == cls || double.class == cls) { if (val == null) { return Double.class == cls ? null: Objects.ZERO_DOUBLE; } else if (val instanceof Double) { //double.class return val; } else if (val instanceof Number) { return new Double(((Number)val).doubleValue()); } else if (val instanceof String) { return new Double((String)val); } else if (val instanceof Date) { return new Double(((Date)val).getTime()); } } else if (BigInteger.class == cls) { if (val == null) { return null; } else if (val instanceof Integer) { return BigIntegers.toBigInteger((Integer)val); } else if (val instanceof Short) { return BigIntegers.toBigInteger((Short)val); } else if (val instanceof Byte) { return BigIntegers.toBigInteger((Byte)val); } else if (val instanceof Number) { return BigIntegers.toBigInteger(((Number)val).longValue()); } else if (val instanceof String) { return new BigInteger((String)val); } else if (val instanceof Date) { return BigIntegers.toBigInteger(((Date)val).getTime()); } } else if (Float.class == cls || float.class == cls) { if (val == null) { return Float.class == cls ? null: Objects.ZERO_FLOAT; } else if (val instanceof Float) { //float.class return val; } else if (val instanceof Number) { return new Float(((Number)val).floatValue()); } else if (val instanceof String) { return new Float((String)val); } else if (val instanceof Date) { return new Float(((Date)val).getTime()); } } else if (Byte.class == cls || byte.class == cls) { if (val == null) { return Byte.class == cls ? null: Objects.ZERO_BYTE; } else if (val instanceof Byte) { //byte.class return val; } else if (val instanceof Number) { return new Byte(((Number)val).byteValue()); } else if (val instanceof String) { return new Byte((String)val); } } else if (Character.class == cls || char.class == cls) { if (val == null) { return Character.class == cls ? null: Objects.NULL_CHARACTER; } else if (val instanceof Character) { //character.class return val; } else if (val instanceof Number) { return new Character((char)((Number)val).shortValue()); } else if (val instanceof String) { final String s = (String)val; return s.length() > 0 ? new Character(s.charAt(0)): Objects.NULL_CHARACTER; } } else if (Date.class == cls) { if (val == null) { return null; } else if (val instanceof Number) { return new Date(((Number)val).longValue()); } } else if (Number.class == cls) { if (val == null) { return null; } else if (val instanceof String) { return new BigDecimal((String)val); } else if (val instanceof Date) { return new BigDecimal(((Date)val).getTime()); } } else { if (val == null) { return null; } else { try { return cls.newInstance(); } catch (Exception ex) { final ClassCastException t = new ClassCastException( Messages.get(MCommon.CLASS_NOT_COMPATIABLE, new Object[] {val.getClass(), cls})); t.initCause(ex); throw t; } } } throw new ClassCastException( Messages.get(MCommon.CLASS_NOT_COMPATIABLE, new Object[] { val != null ? val+"("+val.getClass().getName()+")": "null", cls})); } /** Converts to the specified type. * * @param nullable whether the result could be null. * If false, 0 is used for number, the default constructor is used * for others. {@link #coerce(Class, Object)} is a special case that * is equivalent to nullable=true. * @exception ClassCastException if failed to convert */ public static Object coerce(Class cls, Object val, boolean nullable) throws ClassCastException { if (nullable || val != null) return coerce(cls, val); if (BigDecimal.class == cls) { return Objects.ZERO_BIG_DECIMAL; } else if (Integer.class == cls || int.class == cls) { return Objects.ZERO_INTEGER; } else if (Boolean.class == cls || boolean.class == cls) { return Boolean.FALSE; } else if (Short.class == cls || short.class == cls) { return Objects.ZERO_SHORT; } else if (Long.class == cls || long.class == cls) { return Objects.ZERO_LONG; } else if (Double.class == cls || double.class == cls) { return Objects.ZERO_DOUBLE; } else if (Byte.class == cls || byte.class == cls) { return Objects.ZERO_BYTE; } else if (BigInteger.class == cls) { return Objects.ZERO_BIG_INTEGER; } else if (Float.class == cls || float.class == cls) { return Objects.ZERO_FLOAT; } else if (Character.class == cls || char.class == cls) { return Objects.NULL_CHARACTER; } else { try { return cls.newInstance(); } catch (Exception ex) { final ClassCastException t = new ClassCastException( Messages.get(MCommon.CLASS_NOT_COMPATIABLE, new Object[] {"null", cls})); t.initCause(ex); throw t; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -