List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCliTest.java
static boolean consistsOf(Object item, Class<?> type) { if (type.isInstance(item)) { return true; } else if (item instanceof CompositeElement<?>) { for (Object element : ((CompositeElement<?>) item).getElements()) { if (consistsOf(element, type)) { return true; }/* ww w . j a v a 2 s. co m*/ } } return false; }
From source file:com.antsdb.saltedfish.util.UberUtil.java
@SuppressWarnings("unchecked") public static <T> T toObject(Class<T> klass, Object val) { if (val == null) { return null; }//from w w w. j a va 2s . c o m if (klass.isInstance(val)) { return (T) val; } if (val instanceof byte[]) { if (((byte[]) val).length == 0) { return null; } val = new String((byte[]) val, Charsets.UTF_8); } if (klass == String.class) { return (T) String.valueOf(val); } if (val instanceof String) { String text = (String) val; if (klass == String.class) { return (T) text; } if (text.length() == 0) { return null; } if (klass == Integer.class) { return (T) new Integer(text); } else if (klass == Long.class) { return (T) new Long(text); } else if (klass == BigDecimal.class) { return (T) new BigDecimal(text); } else if (klass == Timestamp.class) { return (T) Timestamp.valueOf(text); } else if (klass == Date.class) { return (T) Date.valueOf(text); } else if (klass == Boolean.class) { return (T) new Boolean(text); } else if (klass == Double.class) { return (T) new Double(text); } } if (val instanceof BigDecimal) { if (klass == Long.class) { Long n = ((BigDecimal) val).longValueExact(); return (T) n; } else if (klass == Integer.class) { Integer n = ((BigDecimal) val).intValueExact(); return (T) n; } else if (klass == Double.class) { Double n = ((BigDecimal) val).doubleValue(); return (T) n; } else if (klass == Boolean.class) { Integer n = ((BigDecimal) val).intValueExact(); return (T) (Boolean) (n != 0); } } if (val instanceof Integer) { if (klass == BigDecimal.class) { return (T) BigDecimal.valueOf((Integer) val); } else if (klass == Long.class) { return (T) Long.valueOf((Integer) val); } else if (klass == Boolean.class) { Integer n = (Integer) val; return (T) (Boolean) (n != 0); } } if (val instanceof Long) { if (klass == BigDecimal.class) { return (T) BigDecimal.valueOf((Long) val); } else if (klass == Boolean.class) { Long n = (Long) val; return (T) (Boolean) (n != 0); } } if (val instanceof Boolean) { if (klass == Long.class) { return (T) Long.valueOf((Boolean) val ? 1 : 0); } } throw new IllegalArgumentException("class: " + val.getClass()); }
From source file:com.github.michalbednarski.intentslab.Utils.java
public static Object[] deepCastArray(Object[] array, Class targetType) { assert targetType.isArray() && !targetType.getComponentType().isPrimitive(); if (targetType.isInstance(array) || array == null) { return array; }//from w ww .ja v a 2 s . c om Class componentType = targetType.getComponentType(); Class nestedComponentType = componentType.getComponentType(); Object[] newArray = (Object[]) Array.newInstance(componentType, array.length); if (nestedComponentType != null && !nestedComponentType.isPrimitive()) { for (int i = 0; i < array.length; i++) { newArray[i] = deepCastArray((Object[]) array[i], nestedComponentType); } } else { System.arraycopy(array, 0, newArray, 0, array.length); } return newArray; }
From source file:grails.util.GrailsMetaClassUtils.java
/** * Obtains a property of an instance if it exists * * @param instance The instance/*from www . j a v a 2 s . com*/ * @param property The property * @param requiredType The required type of the property * @return The property value */ @SuppressWarnings("unchecked") public static <T> T getPropertyIfExists(Object instance, String property, Class<T> requiredType) { MetaClass metaClass = getMetaClass(instance); MetaProperty metaProperty = metaClass.getMetaProperty(property); if (metaProperty != null) { Object value = metaProperty.getProperty(instance); if (value != null && requiredType.isInstance(value)) { return (T) value; } } return null; }
From source file:com.marlonjones.voidlauncher.InstallShortcutReceiver.java
/** * @return true is the extra is either null or is of type {@param type} *///from w w w. ja v a 2s . c o m private static boolean isValidExtraType(Intent intent, String key, Class type) { Object extra = intent.getParcelableExtra(key); return extra == null || type.isInstance(extra); }
From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java
public static <T> T deserialize(byte[] serialized, Class<T> clazz) throws JobPersistenceException { // ToDO(keith): Serialize better than Java serialization. ByteArrayInputStream byteStream = new ByteArrayInputStream(serialized); try {/*from www . j a v a2s .c om*/ ObjectInputStream objectStream = new ObjectInputStream(byteStream); Object deserialized = objectStream.readObject(); objectStream.close(); if (clazz.isInstance(deserialized)) { @SuppressWarnings("unchecked") T obj = (T) deserialized; return obj; } throw new JobPersistenceException("Deserialized object is not of the desired type"); } catch (IOException | ClassNotFoundException e) { throw new JobPersistenceException("Could not deserialize.", e); } }
From source file:net.sf.morph.util.TestUtils.java
/** * Return a "not-same" instance./*w w w .ja va 2 s. co m*/ * @param type * @param o * @return */ public static Object getDifferentInstance(Class type, Object o) { if (type == null) { throw new IllegalArgumentException("Non-null type must be specified"); } if (type.isPrimitive()) { type = ClassUtils.getPrimitiveWrapper(type); } if (o != null && !type.isInstance(o)) { throw new IllegalArgumentException("Negative example object should be of type " + type); } if (type == Number.class) { type = Integer.class; } if (Number.class.isAssignableFrom(type)) { byte b = (byte) (o == null ? 0 : ((Number) o).byteValue() + 1); try { return type.getConstructor(ONE_STRING).newInstance(new Object[] { Byte.toString(b) }); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new NestableRuntimeException(e); } } if (type == Character.class) { char c = (char) (o == null ? 0 : ((Character) o).charValue() + 1); return new Character(c); } if (type == Boolean.class) { return o != null && ((Boolean) o).booleanValue() ? Boolean.FALSE : Boolean.TRUE; } if (type.isArray()) { return Array.newInstance(type.getComponentType(), 0); } if (type == Class.class) { return o == Object.class ? Class.class : Object.class; } return ClassUtils.newInstance(convertCommonInterfaces(type)); }
From source file:org.zht.framework.util.ZBeanUtil.java
private static void copy(Object source, Object target, Boolean ignorNull, Class<?> editable, String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); }/*ww w . j av a 2 s . c om*/ actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (ignorNull != null && ignorNull) { if (value != null && (!"[]".equals(value.toString()))) {// ? if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } else { if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:org.rosenvold.spring.convention.ConventionBeanFactory.java
/** * Resolve the given autowiring value against the given required type, * e.g. an {@link org.springframework.beans.factory.ObjectFactory} value to its actual object result. * * @param autowiringValue the value to resolve * @param requiredType the type to assign the result to * @return the resolved value/*from ww w. j a v a 2 s .c o m*/ */ public static Object resolveAutowiringValue(Object autowiringValue, Class requiredType) { if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) { ObjectFactory factory = (ObjectFactory) autowiringValue; if (autowiringValue instanceof Serializable && requiredType.isInterface()) { autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(), new Class[] { requiredType }, new ObjectFactoryDelegatingInvocationHandler(factory)); } else { return factory.getObject(); } } return autowiringValue; }
From source file:kelly.util.BeanUtils.java
/** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean/*w ww .j a v a 2 s .co m*/ * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) { Validate.notNull(source, "Source must not be null"); Validate.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new BeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }