List of usage examples for org.apache.commons.lang3 ClassUtils isAssignable
public static boolean isAssignable(final Class<?> cls, final Class<?> toClass)
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:com.microsoft.rest.Validator.java
/** * Validates a user provided required parameter to be not null. * A {@link ServiceException} is thrown if a property fails the validation. * * @param parameter the parameter to validate * @throws ServiceException failures wrapped in {@link ServiceException} *//* w w w . j av a 2 s . c o m*/ public static void validate(Object parameter) throws ServiceException { // Validation of top level payload is done outside if (parameter == null) { return; } Class parameterType = parameter.getClass(); if (ClassUtils.isPrimitiveOrWrapper(parameterType) || parameterType.isEnum() || ClassUtils.isAssignable(parameterType, LocalDate.class) || ClassUtils.isAssignable(parameterType, DateTime.class) || ClassUtils.isAssignable(parameterType, String.class) || ClassUtils.isAssignable(parameterType, DateTimeRfc1123.class) || ClassUtils.isAssignable(parameterType, Period.class)) { return; } Field[] fields = FieldUtils.getAllFields(parameterType); for (Field field : fields) { field.setAccessible(true); JsonProperty annotation = field.getAnnotation(JsonProperty.class); Object property; try { property = field.get(parameter); } catch (IllegalAccessException e) { throw new ServiceException(e); } if (property == null) { if (annotation != null && annotation.required()) { throw new ServiceException( new IllegalArgumentException(field.getName() + " is required and cannot be null.")); } } else { try { Class propertyType = property.getClass(); if (ClassUtils.isAssignable(propertyType, List.class)) { List<?> items = (List<?>) property; for (Object item : items) { Validator.validate(item); } } else if (ClassUtils.isAssignable(propertyType, Map.class)) { Map<?, ?> entries = (Map<?, ?>) property; for (Map.Entry<?, ?> entry : entries.entrySet()) { Validator.validate(entry.getKey()); Validator.validate(entry.getValue()); } } else if (parameter.getClass().getDeclaringClass() != propertyType) { Validator.validate(property); } } catch (ServiceException ex) { IllegalArgumentException cause = (IllegalArgumentException) (ex.getCause()); if (cause != null) { // Build property chain throw new ServiceException( new IllegalArgumentException(field.getName() + "." + cause.getMessage())); } else { throw ex; } } } } }
From source file:com.yahoo.elide.utils.coerce.converters.EpochToDateConverter.java
private static <T> T longToDate(Class<T> cls, Long epoch) throws ReflectiveOperationException { if (ClassUtils.isAssignable(cls, java.sql.Date.class)) { return (T) new java.sql.Date(epoch); } else if (ClassUtils.isAssignable(cls, Timestamp.class)) { return (T) new Timestamp(epoch); } else if (ClassUtils.isAssignable(cls, Time.class)) { return (T) new Time(epoch); } else if (ClassUtils.isAssignable(cls, Date.class)) { return (T) new Date(epoch); } else {/*from w ww . j a v a 2 s .c o m*/ throw new UnsupportedOperationException("Cannot convert to " + cls.getSimpleName()); } }
From source file:com.yahoo.elide.utils.coerce.converters.EpochToDateConverter.java
@Override public <T> T convert(Class<T> cls, Object value) { try {/* w ww. jav a2 s.c o m*/ if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToDate(cls, (String) value); } else if (ClassUtils.isAssignable(value.getClass(), Long.class, true)) { return longToDate(cls, (Long) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " is not a valid epoch"); } } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
From source file:de.vandermeer.skb.interfaces.application.CliOptionList.java
/** * Returns all required options, taking short or long CLI argument. * @param list input option list/*from w ww. j av a 2 s .c om*/ * @return all required options */ static Set<String> getRequired(Set<ApoBaseC> list) { Set<String> ret = new TreeSet<>(); if (list != null) { for (ApoBaseC opt : list) { if (opt.cliIsRequired()) { String required = (opt.getCliShortLong() == null) ? "--" + opt.getCliLong() : "-" + opt.getCliShort(); if (ClassUtils.isAssignable(opt.getClass(), Apo_TypedC.class)) { required += " <" + ((Apo_TypedC<?>) opt).getCliArgumentName() + ">"; } ret.add(required); } } } return ret; }
From source file:com.yahoo.elide.utils.coerce.converters.ToEnumConverter.java
/** * Convert value to enum./* w w w .j av a 2s . co m*/ * * @param cls enum to convert to * @param value value to convert * @param <T> enum type * @return enum */ @Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToEnum(cls, (String) value); } else if (ClassUtils.isAssignable(value.getClass(), Integer.class, true)) { return intToEnum(cls, (Integer) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " to Enum no supported"); } } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }
From source file:com.eryansky.core.datasource.DataSourceMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Class<?> clazz = invocation.getThis().getClass(); String className = clazz.getName(); if (ClassUtils.isAssignable(clazz, Proxy.class)) { className = invocation.getMethod().getDeclaringClass().getName(); }/* w w w .ja v a2 s . c o m*/ String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); logger.trace("execute {}.{}({})", className, methodName, arguments); invocation.getMethod(); DataSource classDataSource = ReflectionUtils.getAnnotation(invocation.getThis(), DataSource.class); DataSource methodDataSource = ReflectionUtils.getAnnotation(invocation.getMethod(), DataSource.class); if (methodDataSource != null) { DataSourceContextHolder.setDataSourceType(methodDataSource.value()); } else if (classDataSource != null) { DataSourceContextHolder.setDataSourceType(classDataSource.value()); } else { DataSourceContextHolder.clearDataSourceType(); } Object result = invocation.proceed(); return result; }
From source file:com.eryansky.common.datasource.DataSourceMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Class<?> clazz = invocation.getThis().getClass(); String className = clazz.getName(); if (ClassUtils.isAssignable(clazz, Proxy.class)) { className = invocation.getMethod().getDeclaringClass().getName(); }/*from w ww . j av a2 s . c o m*/ String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); logger.debug("execute {}.{}({})", className, methodName, arguments); invocation.getMethod(); DataSource classDataSource = ReflectionUtils.getAnnotation(invocation.getThis(), DataSource.class); DataSource methodDataSource = ReflectionUtils.getAnnotation(invocation.getMethod(), DataSource.class); Boolean autoReset = null; if (methodDataSource != null) { DataSourceContextHolder.setDataSourceType(methodDataSource.value()); autoReset = methodDataSource.autoReset(); } else if (classDataSource != null) { DataSourceContextHolder.setDataSourceType(classDataSource.value()); autoReset = classDataSource.autoReset(); } else { DataSourceContextHolder.clearDataSourceType(); } Object result = invocation.proceed(); if (autoReset != null && autoReset) { DataSourceContextHolder.clearDataSourceType(); } return result; }
From source file:com.eryansky.core.datasource.SessionFactoryMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Class<?> clazz = invocation.getThis().getClass(); String className = clazz.getName(); if (ClassUtils.isAssignable(clazz, Proxy.class)) { className = invocation.getMethod().getDeclaringClass().getName(); }//from www . java 2 s. c o m String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); logger.trace("execute {}.{}({})", className, methodName, arguments); invocation.getMethod(); SessionFactory classSessionFactory = ReflectionUtils.getAnnotation(invocation.getThis(), SessionFactory.class); SessionFactory methodSessionFactory = ReflectionUtils.getAnnotation(invocation.getMethod(), SessionFactory.class); if (methodSessionFactory != null) { SessionFactoryContextHolder.setSessionFactoryType(methodSessionFactory.value()); } else if (classSessionFactory != null) { SessionFactoryContextHolder.setSessionFactoryType(classSessionFactory.value()); } else { SessionFactoryContextHolder.clearSessionFactory(); } Object result = invocation.proceed(); return result; }
From source file:com.eryansky.common.datasource.SessionFactoryMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Class<?> clazz = invocation.getThis().getClass(); String className = clazz.getName(); if (ClassUtils.isAssignable(clazz, Proxy.class)) { className = invocation.getMethod().getDeclaringClass().getName(); }// w w w .j av a 2 s .c om String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); logger.debug("execute {}.{}({})", className, methodName, arguments); invocation.getMethod(); SessionFactory classSessionFactory = ReflectionUtils.getAnnotation(invocation.getThis(), SessionFactory.class); SessionFactory methodSessionFactory = ReflectionUtils.getAnnotation(invocation.getMethod(), SessionFactory.class); Boolean autoReset = null; if (methodSessionFactory != null) { SessionFactoryContextHolder.setSessionFactoryType(methodSessionFactory.value()); autoReset = methodSessionFactory.autoReset(); } else if (classSessionFactory != null) { SessionFactoryContextHolder.setSessionFactoryType(classSessionFactory.value()); autoReset = classSessionFactory.autoReset(); } else { SessionFactoryContextHolder.clearSessionFactory(); } Object result = invocation.proceed(); if (autoReset != null && autoReset) { SessionFactoryContextHolder.clearSessionFactory(); } return result; }
From source file:edu.umich.flowfence.common.ParceledPayload.java
public static boolean canParcelType(Class<?> clazz, boolean allowVoid) { // All primitives and wrapper types are parcelable, except Character and Void. if (ClassUtils.isPrimitiveOrWrapper(clazz)) { return (clazz != char.class && clazz != Character.class && (allowVoid || (clazz != void.class && clazz != Void.class))); }//w ww. j a v a 2 s . c o m // String and CharSequence are parcelable. if (clazz == String.class || ClassUtils.isAssignable(clazz, CharSequence.class)) { return true; } // Object arrays are parcelable if their component type is parcelable. // Primitive boolean[], byte[], int[], and long[] arrays are parcelable. if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (componentType.isPrimitive()) { return (componentType == int.class || componentType == long.class || componentType == byte.class || componentType == boolean.class); } else { return canParcelType(componentType, false); } } // Parcelable, obviously, is parcelable. // This covers Bundle as well. if (ClassUtils.isAssignable(clazz, Parcelable.class)) { return true; } // Map, List, and SparseArray are all parcelable, with restrictions on their component type // that we can't check here. if (ClassUtils.isAssignable(clazz, Map.class) || ClassUtils.isAssignable(clazz, List.class) || ClassUtils.isAssignable(clazz, SparseArray.class)) { return true; } // IBinder is parcelable. if (ClassUtils.isAssignable(clazz, IBinder.class)) { return true; } // Serializable is parcelable. if (ClassUtils.isAssignable(clazz, Serializable.class)) { return true; } return false; }