package cn.vicky.utils; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.*; public class PropertiesWrapper { public PropertiesWrapper(Properties properties) { if (properties == null) { throw new NullPointerException("The argument must not be null"); } else { this.properties = properties; return; } } public Properties getProperties() { return properties; } /** * 获得key对应的value * @param name * @return */ public String getProperty(String name) { return properties.getProperty(name); } /** * 获得key对应的value,如果没有,则返回默认值 * @param name * @param defaultValue * @return */ public String getProperty(String name, String defaultValue) { return properties.getProperty(name, defaultValue); } /** * 获得key对应的value,如果没有,则返回默认值,返回Boolean类型 * @param name * @param defaultValue * @return */ public boolean getBooleanProperty(String name, boolean defaultValue) { String value = properties.getProperty(name); return value != null ? Boolean.valueOf(value).booleanValue() : defaultValue; } /** * 获得key对应的value,如果没有,则返回默认值,返回int类型 * @param name * @param defaultValue * @return */ public int getIntProperty(String name, int defaultValue) { String value = properties.getProperty(name); if (value == null) return defaultValue; try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw (NumberFormatException) (new NumberFormatException((new StringBuilder()).append( "The value of the ").append(name).append(" property must be a valid ").append("int: /"") .append(value).append("/"").toString())).initCause(e); } } /** * 获得key对应的value,返回int类型 * @param name * @return */ public int getRequiredIntProperty(String name) { String value = properties.getProperty(name); if (value == null) throw new IllegalArgumentException((new StringBuilder()).append("The ").append(name).append( " property must be specified").toString()); try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw (NumberFormatException) (new NumberFormatException((new StringBuilder()).append( "The value of the ").append(name).append(" property must be a valid ").append("int: /"") .append(value).append("/"").toString())).initCause(e); } } /** * 获得key对应的value,如果没有,则返回默认值,返回int类型,并且设置返回值的最大范围与最小范围 * @param name * @param defaultValue * @param min * @param max * @return */ public int getIntProperty(String name, int defaultValue, int min, int max) { if (min > max) throw new IllegalArgumentException("The min must not be greater than the max"); if (min > defaultValue || defaultValue > max) throw new IllegalArgumentException("The default value must be between the min and the max"); int result = getIntProperty(name, defaultValue); if (min > result) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be less ").append("than ").append(min).append(": ").append( result).toString()); if (result > max) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be greater ").append("than ").append(max).append(": ") .append(result).toString()); else return result; } /** * 获得key对应的value,返回int类型,并且设置返回值的最大范围与最小范围 * @param name * @param min * @param max * @return */ public int getRequiredIntProperty(String name, int min, int max) { if (min > max) throw new IllegalArgumentException("The min must not be greater than the max"); int result = getRequiredIntProperty(name); if (min > result) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be less ").append("than ").append(min).append(": ").append( result).toString()); if (result > max) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be greater ").append("than ").append(max).append(": ") .append(result).toString()); else return result; } /** * 获得key对应的value,返回Long类型 * @param name * @param defaultValue * @return */ public long getLongProperty(String name, long defaultValue) { String value = properties.getProperty(name); if (value == null) return defaultValue; try { return Long.parseLong(value); } catch (NumberFormatException e) { throw (NumberFormatException) (new NumberFormatException((new StringBuilder()).append( "The value of the ").append(name).append(" property must be a valid ").append("long: /"") .append(value).append("/"").toString())).initCause(e); } } /** * 获得key对应的value,返回Long类型,并且设置返回值的最大范围与最小范围 * @param name * @param defaultValue * @param min * @param max * @return */ public long getLongProperty(String name, long defaultValue, long min, long max) { if (min > max) throw new IllegalArgumentException("The min must not be greater than the max"); if (min > defaultValue || defaultValue > max) throw new IllegalArgumentException("The default value must be between the min and the max"); long result = getLongProperty(name, defaultValue); if (min > result) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be less ").append("than ").append(min).append(": ").append( result).toString()); if (result > max) throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property must not be greater ").append("than ").append(max).append(": ") .append(result).toString()); else return result; } /** * 根据类名,类型,构造参数类型,参数对象,构造对象 * @param name * @param type * @param paramTypes * @param args * @return */ public Object getClassInstanceProperty(String name, Class type, Class paramTypes[],Object args[]) { String className = properties.getProperty(name); if (className == null) return null; else return getClassInstance(name, className, type, paramTypes, args); } /** * 根据类名,默认类名,类型,构造参数类型,构造对象 * @param name * @param defaultClass * @param type * @param paramTypes * @param args * @return */ public Object getClassInstanceProperty(String name, String defaultClass, Class type,Class paramTypes[], Object args[]) { Object instance = getClassInstanceProperty(name, type, paramTypes, args); if (instance != null) return instance; if (defaultClass == null) return null; else return getClassInstance(name, defaultClass, type, paramTypes, args); } /** * 获得类的实例 * @param name * @param className * @param type * @param paramTypes * @param args * @return */ private Object getClassInstance(String name, String className, Class type, Class paramTypes[],Object args[]) { if (className == null) throw new NullPointerException("null className"); Throwable cause; try { return Class.forName(className).asSubclass(type).getConstructor(paramTypes).newInstance(args); } catch (ClassNotFoundException e) { throw new IllegalArgumentException((new StringBuilder()).append("The class ").append(className) .append(getPropertyText(name)).append(" was not found").toString(), e); } catch (ClassCastException e) { throw new IllegalArgumentException((new StringBuilder()).append("The class ").append(className) .append(getPropertyText(name)).append(" does not implement ").append(type.getName()) .toString(), e); } catch (NoSuchMethodException e) { StringBuilder sb = new StringBuilder(); boolean first = true; Class arr$[] = paramTypes; int len$ = arr$.length; for (int i$ = 0; i$ < len$; i$++) { Class paramType = arr$[i$]; if (first) first = false; else sb.append(", "); sb.append(paramType.getName()); } throw new IllegalArgumentException((new StringBuilder()).append("The class ").append(className) .append(getPropertyText(name)).append( " does not have a constructor with required parameters: ").append(sb).toString(), e); } catch (InvocationTargetException e) { cause = e.getCause(); } catch (Exception e) { throw new IllegalArgumentException((new StringBuilder()).append( "Creating an instance of the class ").append(className).append(getPropertyText(name)) .append(" throws: ").append(e).toString(), e); } if (cause instanceof RuntimeException) throw (RuntimeException) cause; if (cause instanceof Error) throw (Error) cause; else throw new IllegalArgumentException((new StringBuilder()).append( "Calling the constructor for the class ").append(className).append(getPropertyText(name)) .append(" throws: ").append(cause).toString(), cause); } private String getPropertyText(String name) { return name == null ? "" : (new StringBuilder()).append(", specified by the property: ").append(name) .append(",").toString(); } public Enum getEnumProperty(String name, Class enumType, Enum defaultValue) { Objects.checkNull("name", name); Objects.checkNull("enumType", enumType); Objects.checkNull("defaultValue", defaultValue); String value = properties.getProperty(name); if (value == null) return defaultValue; try { return Enum.valueOf(enumType, value); } catch (IllegalArgumentException e) { throw new IllegalArgumentException((new StringBuilder()).append("The value of the ").append(name) .append(" property was /"").append(value).append("/", but must be one of: ").append( Arrays.toString(enumType.getEnumConstants())).toString()); } } public List getListProperty(String name, Class type, Object defaultElement) { Objects.checkNull("name", name); Objects.checkNull("type", type); List list = new ArrayList(); String value = properties.getProperty(name); if (value == null) return list; String values[] = value.split(":", -1); Class constructorParams[] = { String.class }; Constructor constructor = null; try { constructor = type.getConstructor(constructorParams); } catch (NoSuchMethodException nsme) { throw new IllegalArgumentException((new StringBuilder()).append("The class ").append( type.getName()).append(" does not have a ").append( "constructor with the required parameter : String").toString(), nsme); } String arr$[] = values; int len$ = arr$.length; for (int i$ = 0; i$ < len$; i$++) { String v = arr$[i$]; if (v.equals("")) { list.add(defaultElement); continue; } try { list.add(constructor.newInstance(new Object[] { v })); } catch (Exception e) { throw new IllegalArgumentException((new StringBuilder()).append( "Creating an instance of the class ").append(type.getName()).append(" throws: ") .append(e).toString(), e); } } return list; } public List getEnumListProperty(String name, Class enumType, Enum defaultElement) { Objects.checkNull("name", name); Objects.checkNull("enumType", enumType); List list = new ArrayList(); String value = properties.getProperty(name); if (value == null) return list; String values[] = value.split(":", -1); String arr$[] = values; int len$ = arr$.length; for (int i$ = 0; i$ < len$; i$++) { String v = arr$[i$]; if (v.equals("")) { list.add(defaultElement); continue; } try { list.add(Enum.valueOf(enumType, v)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException((new StringBuilder()).append( "A value in the list of items in the ").append(name).append(" property was /"") .append(v).append("/", but must be one of: ").append( Arrays.toString(enumType.getEnumConstants())).toString()); } } return list; } public List getClassListProperty(String name) { Objects.checkNull("name", name); List list = new ArrayList(); String value = properties.getProperty(name); if (value == null) return list; String values[] = value.split(":", -1); String arr$[] = values; int len$ = arr$.length; for (int i$ = 0; i$ < len$; i$++) { String v = arr$[i$]; if (v.equals("")) { list.add(null); continue; } try { list.add(Class.forName(v)); } catch (ClassNotFoundException cnfe) { throw new IllegalArgumentException((new StringBuilder()).append( "A value in the list of items in the ").append(name).append(" property was /"") .append(v).append("/", but a class was not found for this value").toString(), cnfe); } } return list; } private final Properties properties; } package cn.vicky.utils; public final class Objects { private Objects() { } public static String safeToString(Object object) { if(object == null) return "null"; if(callingSafeToString.get() != null) break MISSING_BLOCK_LABEL_145; callingSafeToString.set(Boolean.TRUE); String s1; try { String result = object.toString(); if(result != null && result.length() > 256) result = (new StringBuilder()).append(result.substring(0, 253)).append("...").toString(); s1 = result; } catch(RuntimeException e) { String s; try { s = (new StringBuilder()).append(object.getClass().getName()).append('@').append(Integer.toHexString(object.hashCode())).toString(); } // Misplaced declaration of an exception variable catch(String s) { callingSafeToString.remove(); break MISSING_BLOCK_LABEL_145; } callingSafeToString.remove(); return s; } callingSafeToString.remove(); return s1; Exception exception; callingSafeToString.remove(); throw exception; return (new StringBuilder()).append(object.getClass().getName()).append("[identityHashCode=0x").append(Integer.toHexString(System.identityHashCode(object))).append("]").toString(); } public static String fastToString(Object object) { if(object == null) return "null"; else return (new StringBuilder()).append(object.getClass().getName()).append('#').append(Integer.toHexString(System.identityHashCode(object))).toString(); } public static Object uncheckedCast(Object object) { return object; } public static void checkNull(String variableName, Object value) { if(value == null) throw new NullPointerException((new StringBuilder()).append("The value of ").append(variableName).append(" must not be null").toString()); else return; } private static final ThreadLocal callingSafeToString = new ThreadLocal(); } package cn.vicky.utils; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * 泛型工具类 * @author Vicky * */ public class GenericsUtils { /** * 通过反射,获得指定类的父类的泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的类,该类必须继承范型父类 * @param index 泛型参数所在索引,从0开始. * @return 范型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass();//得到泛型父类 //如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class if (!(genType instanceof ParameterizedType)) { return Object.class; } //返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean extends DaoSupport<Buyer,Contact>就返回Buyer和Contact类型 Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } /** * 通过反射,获得指定类的父类的第一个泛型参数的实际类型. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的类,该类必须继承泛型父类 * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getSuperClassGenricType(Class clazz) { return getSuperClassGenricType(clazz,0); } /** * 通过反射,获得方法返回值泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){} * * @param Method method 方法 * @param int index 泛型参数所在索引,从0开始. * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getMethodGenericReturnType(Method method, int index) { Type returnType = method.getGenericReturnType(); if(returnType instanceof ParameterizedType){ ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); if (index >= typeArguments.length || index < 0) { throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); } return (Class)typeArguments[index]; } return Object.class; } /** * 通过反射,获得方法返回值第一个泛型参数的实际类型. 如: public Map<String, Buyer> getNames(){} * * @param Method method 方法 * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getMethodGenericReturnType(Method method) { return getMethodGenericReturnType(method, 0); } /** * 通过反射,获得方法输入参数第index个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, Buyer> maps, List<String> names){} * * @param Method method 方法 * @param int index 第几个输入参数 * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 */ @SuppressWarnings("unchecked") public static List<Class> getMethodGenericParameterTypes(Method method, int index) { List<Class> results = new ArrayList<Class>(); Type[] genericParameterTypes = method.getGenericParameterTypes(); if (index >= genericParameterTypes.length ||index < 0) { throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); } Type genericParameterType = genericParameterTypes[index]; if(genericParameterType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] parameterArgTypes = aType.getActualTypeArguments(); for(Type parameterArgType : parameterArgTypes){ Class parameterArgClass = (Class) parameterArgType; results.add(parameterArgClass); } return results; } return results; } /** * 通过反射,获得方法输入参数第一个输入参数的所有泛型参数的实际类型. 如: public void add(Map<String, Buyer> maps, List<String> names){} * * @param Method method 方法 * @return 输入参数的泛型参数的实际类型集合, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回空集合 */ @SuppressWarnings("unchecked") public static List<Class> getMethodGenericParameterTypes(Method method) { return getMethodGenericParameterTypes(method, 0); } /** * 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names; * * @param Field field 字段 * @param int index 泛型参数所在索引,从0开始. * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getFieldGenericType(Field field, int index) { Type genericFieldType = field.getGenericType(); if(genericFieldType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericFieldType; Type[] fieldArgTypes = aType.getActualTypeArguments(); if (index >= fieldArgTypes.length || index < 0) { throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" : "超出了参数的总数")); } return (Class)fieldArgTypes[index]; } return Object.class; } /** * 通过反射,获得Field泛型参数的实际类型. 如: public Map<String, Buyer> names; * * @param Field field 字段 * @param int index 泛型参数所在索引,从0开始. * @return 泛型参数的实际类型, 如果没有实现ParameterizedType接口,即不支持泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getFieldGenericType(Field field) { return getFieldGenericType(field, 0); } }