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:eu.crisis_economics.abm.model.ModelUtils.java
public static List<GetterSetterPair> parameters(final Object of, final Class<?> paramType) { final List<GetterSetterPair> result = new ArrayList<GetterSetterPair>(); final Class<?> parentType = of.getClass(); for (Class<?> typeToSearch = parentType; typeToSearch != null; typeToSearch = typeToSearch .getSuperclass()) {/*from ww w . j a va 2 s.co m*/ final Map<String, Method> methodNames = new HashMap<String, Method>(); final Method[] allCalleeMethods = typeToSearch.getDeclaredMethods(); for (final Method method : allCalleeMethods) { final String name = method.getName(); if (name.startsWith("get") && ClassUtils.isAssignable(paramType, method.getReturnType(), true)) methodNames.put(method.getName(), method); else if (name.startsWith("set") && method.getParameterTypes().length == 1 && ClassUtils.isAssignable(paramType, method.getParameterTypes()[0], true)) methodNames.put(method.getName(), method); else continue; final String complement; if (name.startsWith("get")) { complement = "set" + name.substring(3); if (methodNames.containsKey(complement)) result.add(new GetterSetterPair(method, methodNames.get(complement), of)); } else if (name.startsWith("set")) { complement = "get" + name.substring(3); if (methodNames.containsKey(complement)) result.add(new GetterSetterPair(methodNames.get(complement), method, of)); } } } // Search for any ComponentConfiguration fields in the specified object: for (Class<?> typeToSearch = parentType; typeToSearch != null; typeToSearch = typeToSearch .getSuperclass()) { for (Field field : typeToSearch.getDeclaredFields()) { if (!ComponentConfiguration.class.isAssignableFrom(field.getType())) continue; field.setAccessible(true); final Object instance; try { instance = field.get(of); } catch (final IllegalArgumentException e) { continue; // Not found } catch (final IllegalAccessException e) { continue; // Not found } if (instance != null) { final List<GetterSetterPair> subResult = parameters(instance, paramType); // Descend into fields if (subResult != null && !subResult.isEmpty()) result.addAll(subResult); else continue; } } } return result; }
From source file:com.holonplatform.core.internal.utils.TypeUtils.java
/** * Checks if one Class can be assigned to a variable of another Class. * //from ww w .j a v a2s . c o m * <p> * Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this method takes into account widenings of * primitive classes and <code>null</code>s. * </p> * * <p> * Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct * result for these cases. * </p> * * <p> * <code>null</code> may be assigned to any reference type. This method will return <code>true</code> if * <code>null</code> is passed in and the toClass is non-primitive. * </p> * * @param cls the Class to check, may be null * @param toClass the Class to try to assign into, returns false if null * @return <code>true</code> if assignment possible */ public static boolean isAssignable(Class<?> cls, Class<?> toClass) { return ClassUtils.isAssignable(cls, toClass, true); }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
private static boolean checkMatch(Class<?> parameterType, Object arg) { // skip enum if argument was String. We will try valueOf() later if (!(arg instanceof String && parameterType.isEnum()) && !ClassUtils.isAssignable(arg == null ? null : arg.getClass(), parameterType, true)) { return false; }/*from w ww . j av a 2 s .com*/ return true; }
From source file:objenome.util.TypeUtil.java
/** * Returns the super-type class of the given classes, or <code>null</code> * if none of them are a super class of all others * * @param autobox whether autoboxing should be allowed in the consideration * of a super-type/* w w w . j a v a2 s .c o m*/ * @param classes the classes to check for a super-type * @return the super-type if there is one, or <code>null</code> otherwise */ public static Class<?> getSuper(boolean autobox, Class<?>... classes) { outer: for (Class<?> cls1 : classes) { for (Class<?> cls2 : classes) { if (!ClassUtils.isAssignable(cls2, cls1, autobox)) { continue outer; } } return cls1; } return null; }
From source file:objenome.util.TypeUtil.java
/** * Returns the sub-type class of the given classes, or <code>null</code> if * none of them are a sub class of all others * * @param autobox whether autoboxing should be allowed in the consideration * of a sub-type//from ww w .ja v a 2 s .co m * @param classes the classes to check for a sub-type * @return the sub-type if there is one, or <code>null</code> otherwise */ public static Class<?> getSub(boolean autobox, Class<?>... classes) { outer: for (Class<?> cls1 : classes) { for (Class<?> cls2 : classes) { if (!ClassUtils.isAssignable(cls1, cls2, autobox)) { continue outer; } } return cls1; } return null; }
From source file:org.dbasu.robomvvm.binding.TypedValueConverter.java
/** * Attempt to convert the value of the source property to the target type. If the type of the source property * is assignable to the target type, it performs a cast. If it is not, and the target type is {@link java.lang.String}, * it calls the the source property's {@link Object#toString()} function. * * @param value/*from w w w . j a va2 s . c om*/ * The value of the source property. * @throws RuntimeException * When the conversion fails. * @return * The result of the conversion of the source property to the target type. */ @Override public Object convertToTarget(Object value) { if (value == null) return null; Class valueType = value.getClass(); if (ClassUtils.isAssignable(targetType, valueType, true)) { return targetType.cast(value); } if (ClassUtils.isAssignable(String.class, targetType, true)) { return targetType.cast(value.toString()); } throw new RuntimeException( "Unsupported Conversion From " + valueType.getName() + " To " + targetType.getName()); }
From source file:org.dbasu.robomvvm.binding.TypedValueConverter.java
/** * Attempt to convert the value of the target property to the source type. If the type of the target property * is assignable to the source type, it performs a cast. If it is not, and the source type is {@link java.lang.String}, * it calls the the target property's {@link Object#toString()} function. * * @param value/* w w w . j av a 2s . c om*/ * The value of the target property. * @throws RuntimeException * When the conversion fails. * @return * The result of the conversion of the target property to the source type. */ @Override public Object convertToSource(Object value) { if (value == null) return null; Class valueType = value.getClass(); if (ClassUtils.isAssignable(sourceType, valueType, true)) { return sourceType.cast(value); } if (ClassUtils.isAssignable(String.class, sourceType, true)) { return sourceType.cast(value.toString()); } throw new RuntimeException(this.getClass().getName() + " Does Not Support Conversion From " + valueType.getName() + " To " + sourceType.getName()); }
From source file:org.dbasu.robomvvm.componentmodel.ActionManager.java
void invokeAction(final Object targetObject, String actionName, final EventArg eventArg) { Class targetType = targetObject.getClass(); final Class<? extends EventArg> eventType = eventArg != null ? eventArg.getClass() : null; final ActionDescriptor handlerDescription = getActionDescriptor(targetType, actionName); if (handlerDescription == null) { throw new RuntimeException( "No Action By Name " + actionName + " Found In Class " + targetType.getName()); }// w ww .j ava2s . co m final Collection<Method> relevantMethods = Collections2.filter(handlerDescription.methods, new Predicate<Method>() { @Override public boolean apply(Method m) { Class[] params = m.getParameterTypes(); return params.length == 0 || (eventType != null && params.length == 1 && ClassUtils.isAssignable(eventType, params[0], true)); } }); if (relevantMethods.size() == 0) { throw new RuntimeException("Action By Name " + actionName + " Does Not Support Actions Of Type " + ((eventType == null) ? " {null} " : eventType.getName())); } if (targetObject instanceof View) { Activity activity = (Activity) ((View) targetObject).getContext(); activity.runOnUiThread(new Runnable() { @Override public void run() { reallyInvokeAction(relevantMethods, targetObject, eventArg); } }); } else { reallyInvokeAction(relevantMethods, targetObject, eventArg); } }
From source file:org.dbasu.robomvvm.componentmodel.Component.java
/** * Gets all attached event listeners of a particular type. * * @param type//from w ww . ja v a 2 s. com * The to event arg class to query for. * @param <T> * Generic parameter representing the event arg class. * @return * List of all event listeners corresponding to the given type, or an empty list * if no corresponding listeners are found. */ public synchronized <T extends EventArg> List<EventListener> getEventListenersOfType(final Class<T> type) { Preconditions.checkNotNull(type); Collection<EventListener> ret = Collections2.filter(listenerList, new Predicate<EventListener>() { @Override public boolean apply(EventListener input) { return ClassUtils.isAssignable(type, input.getEventType(), true); } }); return new ArrayList<EventListener>(ret); }
From source file:org.dbasu.robomvvm.componentmodel.PropertyManager.java
void setProperty(final Object targetObject, String name, final Object value) { Class targetType = targetObject.getClass(); final PropertyDescriptor desc = getPropertyDescriptor(targetType, name); if (desc == null || desc.setters.size() == 0) { throw new RuntimeException( "No Settable Property By Name " + name + " Found In Class " + targetType.getName()); }//from ww w.j a va 2s . c o m final Class valueClass = value.getClass(); final Method setter = Iterables.find(desc.setters, new Predicate<Method>() { @Override public boolean apply(Method m) { return ClassUtils.isAssignable(valueClass, m.getParameterTypes()[0], true); } }, null); if (setter == null) { throw new RuntimeException("Type Mismatch: Can Not Assign Value Of Type " + valueClass.getName() + " To Property " + name + " In Class " + targetType.getName()); } if (targetObject instanceof View) { Activity activity = (Activity) ((View) targetObject).getContext(); activity.runOnUiThread(new Runnable() { @Override public void run() { reallySetProperty(setter, targetObject, value); } }); } else { reallySetProperty(setter, targetObject, value); } }