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.yahoo.elide.utils.coerce.converters.ISO8601DateSerde.java
@Override public Date deserialize(String val) { Date date;/* w w w .j a v a 2s . c o m*/ try { date = df.parse(val); } catch (java.text.ParseException e) { throw new IllegalArgumentException("Date strings must be formated as yyyy-MM-dd'T'HH:mm'Z'"); } if (ClassUtils.isAssignable(targetType, java.sql.Date.class)) { return new java.sql.Date(date.getTime()); } else if (ClassUtils.isAssignable(targetType, java.sql.Timestamp.class)) { return new java.sql.Timestamp(date.getTime()); } else if (ClassUtils.isAssignable(targetType, java.sql.Time.class)) { return new java.sql.Time(date.getTime()); } else { return date; } }
From source file:de.unentscheidbar.validation.internal.Beans.java
public static boolean hasReadableProperty(Class<?> beanClass, String propertyName, Class<?> returnType) { PropertyDescriptor pd = property(beanClass, propertyName); if (pd == null) return false; Method getter = pd.getReadMethod(); return (getter != null) && ClassUtils.isAssignable(getter.getReturnType(), returnType); }
From source file:com.feilong.core.lang.ClassUtilTest.java
/** * Test is assignable from1.//from w w w. j av a 2 s . co m */ @Test public void testIsAssignableFrom1() { // Class<?>[] klsClasses = { "1234".getClass(), "55555".getClass() }; // assertEquals(true, ClassUtils.isAssignable(klsClasses, CharSequence.class)); assertEquals(true, ClassUtils.isAssignable("1234".getClass(), CharSequence.class)); }
From source file:de.vandermeer.skb.interfaces.application.IsApplication.java
/** * Adds a new option to the application. * Depending on its class, the option will be added to the environment options, the property options, and/or the CLI parser. * If the class of the option is not supported, it will be ignored. * Null values are ignored./*w w w .j av a 2s . co m*/ * @param option the option to be added, ignored if `null` * @throws IllegalStateException if the option is already in use */ default void addOption(Object option) { if (option == null) { return; } if (ClassUtils.isAssignable(option.getClass(), Apo_SimpleC.class)) { this.getCliParser().addOption((Apo_SimpleC) option); } if (ClassUtils.isAssignable(option.getClass(), Apo_TypedC.class)) { this.getCliParser().addOption((Apo_TypedC<?>) option); } if (ClassUtils.isAssignable(option.getClass(), Apo_TypedE.class)) { Apo_TypedE<?> eo = (Apo_TypedE<?>) option; for (Apo_TypedE<?> op : this.getEnvironmentOptions()) { Validate.validState(!op.getEnvironmentKey().equals(eo.getEnvironmentKey()), this.getAppName() + ": environment option <" + eo.getEnvironmentKey() + "> already in use"); } this.getEnvironmentOptions().add(eo); } if (ClassUtils.isAssignable(option.getClass(), Apo_TypedP.class)) { Apo_TypedP<?> po = (Apo_TypedP<?>) option; for (Apo_TypedP<?> op : this.getPropertyOptions()) { Validate.validState(!op.getPropertyKey().equals(po.getPropertyKey()), this.getAppName() + ": property option <" + po.getPropertyKey() + "> already in use"); } this.getPropertyOptions().add(po); } }
From source file:de.vandermeer.skb.datatool.commons.DataUtilities.java
/** * Takes the given entry map and tries to generate a special data object from it. * @param key the key pointing to the map entry * @param keyStart string used to start a key * @param map key/value mappings to load the key from * @param loadedTypes loaded types as lookup for links * @param cs core settings required for loading data * @return a new data object of specific type (as read from the map) on success, null on no success * @throws IllegalArgumentException if any of the required arguments or map entries are not set or empty * @throws URISyntaxException if creating a URI for an SKB link failed */// www.j av a 2 s. c o m public static Object loadData(EntryKey key, String keyStart, Map<?, ?> map, LoadedTypeMap loadedTypes, CoreSettings cs) throws URISyntaxException { if (!map.containsKey(key.getKey())) { return null; } Object data = map.get(key.getKey()); if (key.getSkbUri() != null && data instanceof String) { return loadLink(data, loadedTypes); } if (key.getType().equals(String.class) && data instanceof String) { if (key.useTranslator() == true && cs.getTranslator() != null) { return cs.getTranslator().translate((String) data); } return data; } if (key.getType().equals(Integer.class) && data instanceof Integer) { return data; } if (ClassUtils.isAssignable(key.getType(), EntryObject.class)) { EntryObject eo; try { eo = (EntryObject) key.getType().newInstance(); if (data instanceof Map) { eo.loadObject(keyStart, data, loadedTypes, cs); } return eo; } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
From source file:com.nova.geracao.portfolio.BlogServlet.java
private Object getInstanceFromPropertiesMap(Entity entity, Class<?> klass) { Object result = null;/*from w ww . j a v a 2s. co m*/ try { Field[] fields = klass.getDeclaredFields(); result = klass.newInstance(); for (int i = 0; i < fields.length; i++) { if (entity.hasProperty(fields[i].getName())) { fields[i].setAccessible(true); Object value = entity.getProperty(fields[i].getName()); if (!ClassUtils.isAssignable(fields[i].getType(), BaseDataClass.class)) { fields[i].set(result, value); } else { Object embedValue = getInstanceFromPropertiesMap((EmbeddedEntity) value, fields[i].getType()); fields[i].set(result, embedValue); } } } Field fieldId = klass.getDeclaredField("id"); fieldId.setAccessible(true); fieldId.set(result, entity.getKey().getId()); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return result; }
From source file:de.unentscheidbar.validation.swing.trigger.ChangeTrigger.java
private static boolean supportsClass(Class<?> clazz) { List<Class<?>> supportedClasses = Arrays.<Class<?>>asList(JComboBox.class, JRadioButton.class, JCheckBox.class, JColorChooser.class, JSlider.class, JSpinner.class, JTable.class); for (Class<?> supportedClass : supportedClasses) { if (ClassUtils.isAssignable(clazz, supportedClass)) return true; }/* w w w . j a v a 2 s. c o m*/ return getAddMethod(clazz) != null && getRemoveMethod(clazz) != null; }
From source file:de.vandermeer.skb.interfaces.transformers.Object_To_Target.java
/** * Type safe transformation from Object to target class, with optional special processing for `Object[]` and `Collection`. * The conversion is done in the following sequence: * // w w w . j ava 2 s.c om * * If value is `null`, the `nullValue` will be returned. * * If the requested class is an object array ({@link #getClazzT()}==Object[]), * then the return is `value` (if value is an `Object[]`) * or `Collection.toArray()` (if value is a `Collection)`. * In all other cases the process proceeds * * If `collFist` is set to `true` and value is a `Collection`, the first value of this collection will be used for the further process * * Now another `null` test returning `nullValue` if value is `null` * * Next test if the {@link #getClazzT()} is an instance of `value.class`. If true, `value` will be returned * * Next try for some standard type conversions for `value`, namely * ** If {@link #getClazzT()} is an instance of `Boolean` and value is `true` or `on` (case insensitive), then return `Boolean(true)` * ** If {@link #getClazzT()} is an instance of `Boolean` and value is `false` or `off` (case insensitive), then return `Boolean(false)` * ** If {@link #getClazzT()} is an instance of `Integer` then try to return `Integer.valueOf(value.toString)` * ** If {@link #getClazzT()} is an instance of `Double` then try to return `Double.valueOf(value.toString)` * ** If {@link #getClazzT()} is an instance of `Long` then try to return `Long.valueOf(value.toString)` * * The last option is to return `valueFalse` to indicate that no test was successful * * This method does suppress warnings for "`unchecked`" castings, because a casting from any concrete return type to `T` is unsafe. * Because all actual castings follow an explicit type check, this suppression should have not negative impact (i.e. there are no {@link ClassCastException}). * * @param obj input object for conversion * @return a value of type `T` if a conversion was successful, `nullValue` if `null` was tested successfully, `falseValue` in all other cases */ @SuppressWarnings("unchecked") @Override default T transform(Object obj) { if (obj == null) { return this.getNullValue(); } //next check for Object[], because here we want collections unchanged if (this.getClazzT().isInstance(new Object[] {})) { if (obj instanceof Object[]) { return (T) obj; } else if (obj instanceof Collection) { return (T) ((Collection<?>) obj).toArray(); } } //now, if collection use the first value if (this.getCollFirstFlag() == true && ClassUtils.isAssignable(obj.getClass(), Collection.class)) { Collection_To_FirstElement<Object> tr = Collection_To_FirstElement.create(); obj = tr.transform((Collection<Object>) obj); } //check value again, this maybe the one from the collection if (obj == null) { return this.getNullValue(); } //if value is T, return caste to T if (this.getClazzT().isInstance(obj)) { return (T) obj; } if (this.getClazzT().isInstance(new Boolean(true))) { Object ret = String_To_Boolean.create().transform(obj.toString()); if (ret != null) { return (T) ret; } } if (this.getClazzT().isInstance(new Integer(0))) { try { return (T) Integer.valueOf(obj.toString()); } catch (Exception ignore) { } } if (this.getClazzT().isInstance(new Double(0))) { try { return (T) Double.valueOf(obj.toString()); } catch (Exception ignore) { } } if (this.getClazzT().isInstance(new Long(0))) { try { return (T) Long.valueOf(obj.toString()); } catch (Exception ignore) { } } //no other option, return falseValue return this.getFalseValue(); }
From source file:de.unentscheidbar.validation.swing.trigger.SelectionChangeTrigger.java
private static boolean supportsClass(Class<?> clazz) { final List<Class<?>> supportedClasses = Arrays.<Class<?>>asList(JTable.class, JList.class, JTree.class); for (Class<?> supportedClass : supportedClasses) { if (ClassUtils.isAssignable(clazz, supportedClass)) return true; }/*from www. j ava2 s. c o m*/ return getAddMethod(clazz) != null && getRemoveMethod(clazz) != null; }
From source file:lineage2.gameserver.scripts.Scripts.java
/** * Method init./*w ww . j a v a 2s. com*/ */ public void init() { for (Class<?> clazz : _classes.values()) { addHandlers(clazz); if (Config.DONTLOADQUEST) { if (ClassUtils.isAssignable(clazz, Quest.class)) { continue; } } if (ClassUtils.isAssignable(clazz, ScriptFile.class)) { try { ((ScriptFile) clazz.newInstance()).onLoad(); } catch (Exception e) { _log.error("Scripts: Failed running " + clazz.getName() + ".onLoad()", e); } } } }