List of usage examples for org.apache.commons.lang3 ClassUtils isAssignable
public static boolean isAssignable(Class<?> cls, final Class<?> toClass, final boolean autoboxing)
Checks if one Class can be assigned to a variable of another Class .
Unlike the Class#isAssignableFrom(java.lang.Class) method, this method takes into account widenings of primitive classes and null s.
Primitive widenings allow an int to be assigned to a long, float or double.
From source file:edu.umich.flowfence.client.Sealed.java
public static <T> Sealed<T> wrap(IHandle handle, Class<T> refClass) { try {//from ww w. ja va 2 s . c o m ParamInfo pi = handle.getParamInfo(); Class<?> declaredClass = pi.getType(refClass.getClassLoader()); if (!ClassUtils.isAssignable(declaredClass, refClass, true)) { throw new ClassCastException("Can't assign " + declaredClass + " to reference of type " + refClass); } return new Sealed<>(handle); } catch (Exception e) { ParceledThrowable.throwUnchecked(e); return null; } }
From source file:net.radai.beanz.codecs.CollectionCodec.java
public CollectionCodec(Type type, Type elementType, Codec elementCodec) { if (type == null || elementType == null || elementCodec == null || !ClassUtils.isAssignable(erase(elementCodec.getType()), erase(elementType), true) || !ReflectionUtil.isCollection(type)) { throw new IllegalArgumentException(); }/*w w w . j a v a 2s . co m*/ this.type = type; this.elementType = elementType; this.elementCodec = elementCodec; }
From source file:net.radai.beanz.codecs.ArrayCodec.java
public ArrayCodec(Type type, Type elementType, Codec elementCodec) { if (type == null || elementType == null || elementCodec == null || !ClassUtils.isAssignable(erase(elementCodec.getType()), erase(elementType), true) || !ReflectionUtil.isArray(type)) { throw new IllegalArgumentException(); }//from w w w . j a v a 2 s .c om this.type = type; this.elementType = elementType; this.elementCodec = elementCodec; }
From source file:net.radai.beanz.codecs.MapCodec.java
public MapCodec(Type type, Type keyType, Type valueType, Codec keyCodec, Codec valueCodec) { if (type == null || keyType == null || valueType == null || keyCodec == null || valueCodec == null || !ReflectionUtil.isMap(type) || !ClassUtils.isAssignable(erase(keyType), erase(ReflectionUtil.getKeyType(type)), true) || !ClassUtils.isAssignable(erase(valueType), erase(ReflectionUtil.getElementType(type)), true) || !ClassUtils.isAssignable(erase(keyCodec.getType()), erase(keyType), true) || !ClassUtils.isAssignable(erase(valueCodec.getType()), erase(valueType), true)) { throw new IllegalArgumentException(); }/*from w w w. j av a2 s . c om*/ this.type = type; this.keyType = keyType; this.valueType = valueType; this.keyCodec = keyCodec; this.valueCodec = valueCodec; }
From source file:edu.umich.flowfence.client.QuarentineModule.java
private static <T> Class<? extends T> checkClass(String className, Class<T> clazz, ClassLoader loader) throws ClassNotFoundException { Class<?> resultClazz;/*from w ww. java 2 s. c om*/ if ("void".equals(className)) { if ("void".equals(clazz.getName())) { return clazz; } else if ("java.lang.Void".equals(clazz.getName())) { return clazz; } else { throw new ClassCastException("Void type in non-void context"); } } resultClazz = ClassUtils.getClass(loader, className, true); // Special handling for primitives. // If we can be handled by one of the primitive conversions, allow it. if (resultClazz.isPrimitive()) { if (ClassUtils.isAssignable(resultClazz, clazz, true)) { return clazz; } else { throw new ClassCastException("Cannot convert " + className + " to " + clazz.getName()); } } return resultClazz.asSubclass(clazz); }
From source file:com.epam.reportportal.junit.JUnitProvider.java
@Override public IListenerHandler get() { if (reportPortalService.getParameters().getEnable()) { return new ParallelRunningHandler(parallelRunningContext, reportPortalService); }/* w w w . j a va 2 s .c o m*/ return (IListenerHandler) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { IListenerHandler.class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Class<?> returnType = method.getReturnType(); if (ClassUtils.isAssignable(returnType, Boolean.class, true)) { return false; } return null; } }); }
From source file:net.radai.beanz.api.BeanDescriptor.java
public void addCodec(Type type, Codec codec) { if (codec == null || type == null || !ClassUtils.isAssignable(erase(codec.getType()), erase(type), true) || codecs.containsKey(type)) { throw new IllegalArgumentException(); }//from www .j a v a 2s . c o m codecs.put(type, codec); }
From source file:com.jsen.core.reflect.ClassFunction.java
/** * Tests whether are passed arguments assignable to the given types. * //w ww. ja va2s. co m * @param args Arguments to be tested. * @param types Types. * @return True if are passed arguments assignable to the given types. */ public static boolean isAssignableTypes(Object[] args, Class<?>... types) { if (args.length != types.length) { return false; } for (int i = 0; i < args.length; i++) { Class<?> argClass = (args[i] == null) ? null : args[i].getClass(); Class<?> paramClass = types[i]; if (paramClass.isPrimitive() && argClass == null) { return false; } else if (!paramClass.isPrimitive() && argClass == null) { continue; } else if (!ClassUtils.isAssignable(argClass, paramClass, true)) { return false; } } return true; }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
@SuppressWarnings({ "unchecked", "unchecked" }) public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Object... args) throws NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IllegalAccessException {/* ww w . ja v a 2s .com*/ try { List<Method> validMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { Class<?>[] parameterTypes = null; if (!method.getName().equals(methodName)) { continue; } parameterTypes = method.getParameterTypes(); if (method.isVarArgs()) { if (method.isVarArgs() && (parameterTypes.length - args.length >= 2)) { parameterTypes = null; continue; } } else { if (parameterTypes.length != args.length) { parameterTypes = null; continue; } } if (method.isVarArgs()) { boolean matches = false; // check non vararg part for (int i = 0; i < parameterTypes.length - 1; i++) { matches = checkMatch(parameterTypes[i], args[i]); if (!matches) break; } // check rest for (int i = parameterTypes.length - 1; i < args.length; i++) { Class<?> arrayType = parameterTypes[parameterTypes.length - 1].getComponentType(); matches = checkMatch(arrayType, args[i]); if (!matches) break; } if (matches) { validMethods.add(method); } } else { boolean matches = true; for (int i = 0; i < parameterTypes.length; i++) { matches = checkMatch(parameterTypes[i], args[i]); if (!matches) break; } if (matches) { validMethods.add(method); } } } if (!validMethods.isEmpty()) { Method method = validMethods.get(0); for (int i = 1; i < validMethods.size(); i++) { Method targetMethod = validMethods.get(i); Class<?>[] currentParams = method.getParameterTypes(); Class<?>[] targetParams = targetMethod.getParameterTypes(); if (method.isVarArgs() && targetMethod.isVarArgs()) { for (int j = 0; j < currentParams.length; j++) { if (currentParams[j].isAssignableFrom(targetParams[j])) { method = targetMethod; break; } } } else if (method.isVarArgs()) { //usually, non-vararg is more specific method. So we use that method = targetMethod; } else if (targetMethod.isVarArgs()) { //do nothing } else { for (int j = 0; j < currentParams.length; j++) { if (targetParams[j].isEnum()) { // enum will be handled later method = targetMethod; break; } else if (ClassUtils.isAssignable(targetParams[j], currentParams[j], true)) { //narrow down to find the most specific method method = targetMethod; break; } } } } method.setAccessible(true); for (int i = 0; i < args.length; i++) { Class<?>[] parameterTypes = method.getParameterTypes(); if (args[i] instanceof String && i < parameterTypes.length && parameterTypes[i].isEnum()) { try { args[i] = Enum.valueOf((Class<? extends Enum>) parameterTypes[i], (String) args[i]); } catch (IllegalArgumentException ex1) { // Some overloaded methods already has // String to Enum conversion // So just lets see if one exists Class<?>[] types = new Class<?>[args.length]; for (int k = 0; k < args.length; k++) types[k] = args[k].getClass(); try { Method alternative = clazz.getMethod(methodName, types); return alternative.invoke(obj, args); } catch (NoSuchMethodException ex2) { throw new RuntimeException( "Tried to convert value [" + args[i] + "] to Enum [" + parameterTypes[i] + "] or find appropriate method but found nothing. Make sure" + " that the value [" + args[i] + "] matches exactly with one of the Enums in [" + parameterTypes[i] + "] or the method you are looking exists."); } } } } if (method.isVarArgs()) { Class<?>[] parameterTypes = method.getParameterTypes(); Object varargs = Array.newInstance(parameterTypes[parameterTypes.length - 1].getComponentType(), args.length - parameterTypes.length + 1); for (int k = 0; k < Array.getLength(varargs); k++) { Array.set(varargs, k, args[parameterTypes.length - 1 + k]); } Object[] newArgs = new Object[parameterTypes.length]; for (int k = 0; k < newArgs.length - 1; k++) { newArgs[k] = args[k]; } newArgs[newArgs.length - 1] = varargs; args = newArgs; } return method.invoke(obj, args); } if (args.length > 0) { StringBuilder builder = new StringBuilder(String.valueOf(args[0].getClass().getSimpleName())); for (int i = 1; i < args.length; i++) { builder.append(", " + args[i].getClass().getSimpleName()); } throw new NoSuchMethodException(methodName + "(" + builder.toString() + ")"); } else { throw new NoSuchMethodException(methodName + "()"); } } catch (NullPointerException e) { StringBuilder builder = new StringBuilder(String.valueOf(args[0])); for (int i = 1; i < args.length; i++) builder.append("," + String.valueOf(args[i])); throw new NullPointerException("Call " + methodName + "(" + builder.toString() + ")"); } }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static boolean isAssignable(Type from, Type to) { //TODO - be smarter about generics (List<String> is not really assignable to List<Integer> ...) return ClassUtils.isAssignable(erase(from), erase(to), true); }