public static Throwable getThrowable(String throwstr) { Throwable throwable = throwables.get(throwstr); if (throwable != null) { return throwable; } try { Throwable t; Class<?> bizException = ReflectUtils.forName(throwstr); Constructor<?> constructor; constructor = ReflectUtils.findConstructor(bizException, String.class); t = (Throwable) constructor.newInstance(new Object[]{"mocked exception for service degradation."}); if (throwables.size() < 1000) { throwables.put(throwstr, t); } return t; } catch (Exception e) { throw new RpcException("mock throw error :" + throwstr + " argument error.", e); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) { throw new UnsupportedOperationException("Method [" + ReflectUtils.getName(method) + "] unimplemented."); } };
/** * get class array instance. * * @param desc desc. * @return Class class array. * @throws ClassNotFoundException */ public static Class<?>[] desc2classArray(String desc) throws ClassNotFoundException { Class<?>[] ret = desc2classArray(ClassHelper.getClassLoader(), desc); return ret; }
if (ReflectUtils.isPrimitive(arg.getClass())) { if (!ReflectUtils.isPrimitive(type)) { return false; if (!ReflectUtils.isCompatible(type, arg)) { return false; String name = (String) ((Map<?, ?>) arg).get("class"); if (StringUtils.isNotEmpty(name)) { Class<?> cls = ReflectUtils.forName(name); if (!type.isAssignableFrom(cls)) { return false;
@SuppressWarnings("unchecked") public static Object getMockObject(String mockService, Class serviceType) { if (ConfigUtils.isDefault(mockService)) { mockService = serviceType.getName() + "Mock"; } Class<?> mockClass = ReflectUtils.forName(mockService); if (!serviceType.isAssignableFrom(mockClass)) { throw new IllegalStateException("The mock class " + mockClass.getName() + " not implement interface " + serviceType.getName()); } try { return mockClass.newInstance(); } catch (InstantiationException e) { throw new IllegalStateException("No default constructor from mock class " + mockClass.getName(), e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } }
private static int findMethod(Class<?>[] dcs, String desc) { Class<?> cl; Method[] methods; for (int i = 0; i < dcs.length; i++) { cl = dcs[i]; methods = cl.getMethods(); for (Method method : methods) { if (desc.equals(ReflectUtils.getDesc(method))) { return i; } } } return -1; }
public static Object getEmptyObject(Class<?> returnType) { return getEmptyObject(returnType, new HashMap<Class<?>, Object>(), 0); }
public static Class<?> getGenericClass(Class<?> cls) { return getGenericClass(cls, 0); }
public static Method findMethodByMethodName(Class<?> clazz, String methodName) throws NoSuchMethodException, ClassNotFoundException { return findMethodByMethodSignature(clazz, methodName, null); }
/** * is compatible. * * @param c class. * @param o instance. * @return compatible or not. */ public static boolean isCompatible(Class<?> c, Object o) { boolean pt = c.isPrimitive(); if (o == null) { return !pt; } if (pt) { c = getBoxedClass(c); } return c == o.getClass() || c.isInstance(o); }
private void verify(Class<?> interfaceClass, Class<?> localClass) { if (!interfaceClass.isAssignableFrom(localClass)) { throw new IllegalStateException("The local implementation class " + localClass.getName() + " not implement interface " + interfaceClass.getName()); } try { //Check if the localClass a constructor with parameter who's type is interfaceClass ReflectUtils.findConstructor(localClass, interfaceClass); } catch (NoSuchMethodException e) { throw new IllegalStateException("No such constructor \"public " + localClass.getSimpleName() + "(" + interfaceClass.getName() + ")\" in local implementation class " + localClass.getName()); } }
/** * is compatible. * * @param cs class array. * @param os object array. * @return compatible or not. */ public static boolean isCompatible(Class<?>[] cs, Object[] os) { int len = cs.length; if (len != os.length) { return false; } if (len == 0) { return true; } for (int i = 0; i < len; i++) { if (!isCompatible(cs[i], os[i])) { return false; } } return true; }
if (ReflectUtils.isPrimitive(arg.getClass())) { if (!ReflectUtils.isPrimitive(type)) { return false; if (!ReflectUtils.isCompatible(type, arg)) { return false; String name = (String) ((Map<?, ?>) arg).get("class"); if (StringUtils.isNotEmpty(name)) { Class<?> cls = ReflectUtils.forName(name); if (!type.isAssignableFrom(cls)) { return false;
@SuppressWarnings("unchecked") public static Object getMockObject(String mockService, Class serviceType) { if (ConfigUtils.isDefault(mockService)) { mockService = serviceType.getName() + "Mock"; } Class<?> mockClass = ReflectUtils.forName(mockService); if (!serviceType.isAssignableFrom(mockClass)) { throw new IllegalStateException("The mock class " + mockClass.getName() + " not implement interface " + serviceType.getName()); } try { return mockClass.newInstance(); } catch (InstantiationException e) { throw new IllegalStateException("No default constructor from mock class " + mockClass.getName(), e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } }
private static int findMethod(Class<?>[] dcs, String desc) { Class<?> cl; Method[] methods; for (int i = 0; i < dcs.length; i++) { cl = dcs[i]; methods = cl.getMethods(); for (Method method : methods) { if (desc.equals(ReflectUtils.getDesc(method))) { return i; } } } return -1; }
public static Object getEmptyObject(Class<?> returnType) { return getEmptyObject(returnType, new HashMap<Class<?>, Object>(), 0); }
public static Class<?> getGenericClass(Class<?> cls) { return getGenericClass(cls, 0); }
public static Method findMethodByMethodName(Class<?> clazz, String methodName) throws NoSuchMethodException, ClassNotFoundException { return findMethodByMethodSignature(clazz, methodName, null); }
/** * is compatible. * * @param c class. * @param o instance. * @return compatible or not. */ public static boolean isCompatible(Class<?> c, Object o) { boolean pt = c.isPrimitive(); if (o == null) { return !pt; } if (pt) { c = getBoxedClass(c); } return c == o.getClass() || c.isInstance(o); }
private void verify(Class<?> interfaceClass, Class<?> localClass) { if (!interfaceClass.isAssignableFrom(localClass)) { throw new IllegalStateException("The local implementation class " + localClass.getName() + " not implement interface " + interfaceClass.getName()); } try { //Check if the localClass a constructor with parameter who's type is interfaceClass ReflectUtils.findConstructor(localClass, interfaceClass); } catch (NoSuchMethodException e) { throw new IllegalStateException("No such constructor \"public " + localClass.getSimpleName() + "(" + interfaceClass.getName() + ")\" in local implementation class " + localClass.getName()); } }