List of usage examples for org.apache.commons.lang ClassUtils isAssignable
public static boolean isAssignable(Class<?> cls, Class<?> toClass, 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:com.ginema.api.reflection.ReflectionUtils.java
public static boolean isAssignableFrom(Class c, Set<Class> allowedClasses) { return allowedClasses.stream().filter(clazz -> ClassUtils.isAssignable(c, clazz, true)).findFirst() .isPresent();//from w w w . j ava 2 s . c o m }
From source file:com.ginema.api.reflection.ReflectionUtils.java
public static boolean isAssignableFrom(Class c, Class clazz) { return ClassUtils.isAssignable(c, clazz, true); }
From source file:com.ginema.api.reflection.ReflectionUtilsTest.java
@Test public void testShouldGetIsAssignable() { assertTrue(ClassUtils.isAssignable(Integer.class, Number.class, true)); assertTrue(ReflectionUtils.isAssignableFrom(Long.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(Integer.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(Double.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(Float.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(boolean.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(int.class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); assertTrue(ReflectionUtils.isAssignableFrom(byte[].class, SensitiveDataField.SUPPORTED_FIELD_TYPES)); }
From source file:com.github.dmn1k.supercsv.io.declarative.CsvDeclarativeBeanReader.java
private <T> T populateBean(final T resultBean, List<Object> processedColumns, BeanCells cells) { for (int i = 0; i < processedColumns.size(); i++) { final Object fieldValue = processedColumns.get(i); BeanCell cell = cells.getCell(i); if (cell == null || cell.getType() == null) { continue; }//from www. j a v a 2s . co m // ClassUtils handles boxed types if (fieldValue != null && ClassUtils.isAssignable(fieldValue.getClass(), cell.getType(), true)) { cell.setValue(resultBean, fieldValue); } else { Class<?> fieldValueClass = fieldValue == null ? Object.class : fieldValue.getClass(); TypeConverter<Object, Object> converter = (TypeConverter<Object, Object>) typeConverterRegistry .getConverter(fieldValueClass, cell.getType()); if (converter == null) { throw new SuperCsvException(Form.at( "No converter registered from type {} to type {}. Add one or fix your CellProcessor-annotations to return the field's type", fieldValueClass.getName(), cell.getType().getName())); } cell.setValue(resultBean, converter.convert(fieldValue)); } } return resultBean; }
From source file:org.apache.niolex.commons.bean.BeanUtil.java
/** * Merge the non null properties from the source bean to the target bean. * * @param to the target bean/* w w w .j a v a2 s . c o m*/ * @param from the source bean * @param mergeDefault whether do we merge default numeric primitives * @return the target bean */ public static final <To, From> To merge(To to, From from, boolean mergeDefault) { try { Map<String, Method> writeMap = prepareWriteMethodMap(to.getClass()); BeanInfo fromInfo = Introspector.getBeanInfo(from.getClass()); // Iterate over all the attributes of from, do copy here. for (PropertyDescriptor descriptor : fromInfo.getPropertyDescriptors()) { Method readMethod = descriptor.getReadMethod(); if (readMethod == null) { continue; } Method writeMethod = writeMap.get(descriptor.getName()); if (writeMethod == null) { continue; } Object value = readMethod.invoke(from); if (value == null) { continue; } if (!mergeDefault && isNumericPrimitiveDefaultValue(readMethod.getReturnType(), value)) { continue; } // Only copy value if it's assignable, auto boxing is OK. if (ClassUtils.isAssignable(value.getClass(), writeMethod.getParameterTypes()[0], true)) { writeMethod.invoke(to, value); } } } catch (Exception e) { throw new IllegalArgumentException("Failed to merge propeties.", e); } return to; }
From source file:org.apache.niolex.commons.bean.BeanUtil.java
/** * Merge the non null properties from the map to the target bean. * * @param to the target bean// w w w .java 2 s . c om * @param from the source map * @return the target bean */ public static final <To> To merge(To to, Map<String, Object> from) { try { Map<String, Method> writeMap = prepareWriteMethodMap(to.getClass()); for (Map.Entry<String, Object> entry : from.entrySet()) { Method writeMethod = writeMap.get(entry.getKey()); if (writeMethod == null) { continue; } Object value = entry.getValue(); // Only copy value if it's assignable, auto boxing is OK. if (ClassUtils.isAssignable(value.getClass(), writeMethod.getParameterTypes()[0], true)) { writeMethod.invoke(to, value); } } } catch (Exception e) { throw new IllegalArgumentException("Failed to merge propeties.", e); } return to; }
From source file:org.apache.niolex.commons.reflect.MethodFilter.java
/** * This is the override of super method. * @see org.apache.niolex.commons.reflect.MethodUtil.Filter#isValid(java.lang.reflect.Method) *//*from w w w . j a va2 s. c o m*/ @Override public boolean isValid(Method m) { if (methodName != null && !methodName.equals(m.getName())) { return false; } if (returnType != null && !ClassUtils.isAssignable(m.getReturnType(), returnType, true)) { return false; } if (parameterTypes != null && !ClassUtils.isAssignable(parameterTypes, m.getParameterTypes(), true)) { return false; } if (!includeStatic && Modifier.isStatic(m.getModifiers())) { return false; } if (!includeAbstract && Modifier.isAbstract(m.getModifiers())) { return false; } if (!includeSynthetic && m.isSynthetic()) { return false; } return true; }
From source file:org.epochx.tools.DataTypeUtils.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 * //from w ww . ja v a2s . co m * @param autobox whether autoboxing should be allowed in the consideration * of a super-type * @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:org.epochx.tools.DataTypeUtils.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 * // w ww.j a va 2 s . com * @param autobox whether autoboxing should be allowed in the consideration * of a sub-type * @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.epochx.tools.util.TypeUtils.java
/** * Returns whichever class is the super class of the other, or null if * neither are a super class of the other. * /*from w w w . j ava 2s .c o m*/ * @return */ public static Class<?> getSuper(final boolean autobox, final Class<?>... classes) { outer: for (final Class<?> cls1 : classes) { for (final Class<?> cls2 : classes) { if (!ClassUtils.isAssignable(cls2, cls1, autobox)) { continue outer; } } return cls1; } return null; }