List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:Main.java
public static Object findValueOfType(Collection collection, Class type) { if (isEmpty(collection)) { return null; } else {/*from w ww. ja v a 2 s . c o m*/ Object value = null; Iterator it = collection.iterator(); while (true) { Object obj; do { if (!it.hasNext()) { return value; } obj = it.next(); } while (type != null && !type.isInstance(obj)); if (value != null) { return null; } value = obj; } } }
From source file:edu.umn.msi.tropix.common.logging.ExceptionUtils.java
@SuppressWarnings("unchecked") public static <E extends Throwable> E convertException(final Throwable t, final String message, final Class<E> exceptionClass) { E exceptionInstance = null;// ww w . jav a2 s .c o m try { if (message != null) { final Constructor<E> constructor = exceptionClass.getConstructor(String.class, Throwable.class); exceptionInstance = constructor.newInstance(message, t); } else if (exceptionClass.isInstance(t)) { exceptionInstance = (E) t; } else { final Constructor<E> constructor = exceptionClass.getConstructor(Throwable.class); exceptionInstance = constructor.newInstance(t); } } catch (final Exception exception) { ExceptionUtils.INTERNAL_LOG.warn("Failed to produce exception of class " + exceptionClass + " with message " + message + " and throwable " + t); ExceptionUtils.INTERNAL_LOG.info("Exception Info : " + exception); throw new ExceptionConversionException(exception); } return exceptionInstance; }
From source file:com.healthmarketscience.rmiio.RemoteRetry.java
/** * Checks the given exception against the given Exception type, throwing if * the given exception is an instanceof the given type. Otherwise, returns. *//*from w w w.ja va2s .c o m*/ private static <ExType extends Throwable> void throwIfMatchesType(Throwable throwable, Class<ExType> throwType) throws ExType { if (throwType.isInstance(throwable)) { throw throwType.cast(throwable); } }
From source file:com.joyent.manta.http.ApacheHttpGetResponseEntityContentContinuator.java
/** * Determine whether an {@link IOException} indicates a fatal issue or not. Shamelessly plagiarized from {@link * org.apache.http.impl.client.DefaultHttpRequestRetryHandler}. * * @param ex the exception to check// www . j a va 2 s. c o m * @return whether or not the caller should retry their request */ private static boolean isRecoverable(final IOException ex) { if (EXCEPTIONS_FATAL.contains(ex.getClass())) { return false; } for (final Class<? extends IOException> exceptionClass : EXCEPTIONS_FATAL) { if (exceptionClass.isInstance(ex)) { return false; } } return true; }
From source file:com.langnatech.util.AssertUtil.java
/** * Assert that the provided object is an instance of the provided class. <pre * class="code">Assert.instanceOf(Foo.class, foo);</pre> * /* w ww. j av a2 s . co m*/ * @param type the type to check against * @param obj the object to check * @param message a message which will be prepended to the message produced by the function itself, and which may be * used to provide context. It should normally end in a ": " or ". " so that the function generate * message looks ok when prepended to it. * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class<?> type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throw new IllegalArgumentException((StringUtils.isNotEmpty(message) ? message + " " : "") + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } }
From source file:org.caratarse.auth.model.util.BeanUtils.java
/** * Copy the not-null property values of the given source bean into the given target bean. * <p>// w w w . jav a 2 s.com * 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 * @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 copyNotNullProperties(Object source, Object target, 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() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.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 = org.springframework.beans.BeanUtils .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 (value == null) { continue; } 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:com.evolveum.midpoint.prism.util.JavaTypeConverter.java
public static <T> T convert(Class<T> expectedType, Object rawValue) { if (rawValue == null || expectedType.isInstance(rawValue)) { return (T) rawValue; }/*from w ww . j a v a 2s. c om*/ if (rawValue instanceof PrismPropertyValue<?>) { rawValue = ((PrismPropertyValue<?>) rawValue).getValue(); } // This really needs to be checked twice if (rawValue == null || expectedType.isInstance(rawValue)) { return (T) rawValue; } // Primitive types // boolean if (expectedType == boolean.class && rawValue instanceof Boolean) { return (T) ((Boolean) rawValue); } if (expectedType == Boolean.class && rawValue instanceof String) { return (T) (Boolean) Boolean.parseBoolean(((String) rawValue)); } if (expectedType == Boolean.class && rawValue instanceof PolyString) { return (T) (Boolean) Boolean.parseBoolean(((PolyString) rawValue).toString()); } if (expectedType == boolean.class && rawValue instanceof String) { return (T) (Boolean) Boolean.parseBoolean(((String) rawValue)); } if (expectedType == boolean.class && rawValue instanceof PolyString) { return (T) (Boolean) Boolean.parseBoolean(((PolyString) rawValue).toString()); } if (expectedType == String.class && rawValue instanceof Boolean) { return (T) rawValue.toString(); } // int if (expectedType == int.class && rawValue instanceof Integer) { return (T) ((Integer) rawValue); } if (expectedType == Integer.class && rawValue instanceof String) { return (T) (Integer) Integer.parseInt(((String) rawValue)); } if (expectedType == int.class && rawValue instanceof String) { return (T) (Integer) Integer.parseInt(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof Integer) { return (T) rawValue.toString(); } if (expectedType == int.class && rawValue instanceof Long) { return (T) (Integer) ((Long) rawValue).intValue(); } if (expectedType == long.class && rawValue instanceof Long) { return (T) ((Long) rawValue); } if (expectedType == Long.class && rawValue instanceof String) { return (T) (Long) Long.parseLong(((String) rawValue)); } if (expectedType == long.class && rawValue instanceof String) { return (T) (Long) Long.parseLong(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof Long) { return (T) rawValue.toString(); } if (expectedType == float.class && rawValue instanceof Float) { return (T) ((Float) rawValue); } if (expectedType == Float.class && rawValue instanceof String) { return (T) (Float) Float.parseFloat(((String) rawValue)); } if (expectedType == float.class && rawValue instanceof String) { return (T) (Float) Float.parseFloat(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof Float) { return (T) rawValue.toString(); } if (expectedType == double.class && rawValue instanceof Double) { return (T) ((Double) rawValue); } if (expectedType == Double.class && rawValue instanceof String) { return (T) (Double) Double.parseDouble(((String) rawValue)); } if (expectedType == double.class && rawValue instanceof String) { return (T) (Double) Double.parseDouble(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof Float) { return (T) rawValue.toString(); } if (expectedType == byte.class && rawValue instanceof Byte) { return (T) ((Byte) rawValue); } if (expectedType == Byte.class && rawValue instanceof String) { return (T) (Byte) Byte.parseByte(((String) rawValue)); } if (expectedType == byte.class && rawValue instanceof String) { return (T) (Byte) Byte.parseByte(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof Byte) { return (T) rawValue.toString(); } if (expectedType == BigInteger.class && rawValue instanceof String) { return (T) new BigInteger(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof BigInteger) { return (T) ((BigInteger) rawValue).toString(); } if (expectedType == BigDecimal.class && rawValue instanceof String) { return (T) new BigDecimal(((String) rawValue)); } if (expectedType == String.class && rawValue instanceof BigDecimal) { return (T) ((BigDecimal) rawValue).toString(); } if (expectedType == PolyString.class && rawValue instanceof String) { return (T) new PolyString((String) rawValue); } if (expectedType == PolyStringType.class && rawValue instanceof String) { PolyStringType polyStringType = new PolyStringType(); polyStringType.setOrig((String) rawValue); return (T) polyStringType; } if (expectedType == String.class && rawValue instanceof PolyString) { return (T) ((PolyString) rawValue).getOrig(); } if (expectedType == String.class && rawValue instanceof PolyStringType) { return (T) ((PolyStringType) rawValue).getOrig(); } if (expectedType == PolyString.class && rawValue instanceof PolyStringType) { return (T) ((PolyStringType) rawValue).toPolyString(); } if (expectedType == PolyString.class && rawValue instanceof Integer) { return (T) new PolyString(((Integer) rawValue).toString()); } if (expectedType == PolyStringType.class && rawValue instanceof PolyString) { PolyStringType polyStringType = new PolyStringType((PolyString) rawValue); return (T) polyStringType; } if (expectedType == PolyStringType.class && rawValue instanceof Integer) { PolyStringType polyStringType = new PolyStringType(((Integer) rawValue).toString()); return (T) polyStringType; } // Date and time if (expectedType == XMLGregorianCalendar.class && rawValue instanceof Long) { XMLGregorianCalendar xmlCalType = XmlTypeConverter.createXMLGregorianCalendar((Long) rawValue); return (T) xmlCalType; } if (expectedType == XMLGregorianCalendar.class && rawValue instanceof String) { XMLGregorianCalendar xmlCalType = magicDateTimeParse((String) rawValue); return (T) xmlCalType; } if (expectedType == String.class && rawValue instanceof XMLGregorianCalendar) { return (T) ((XMLGregorianCalendar) rawValue).toXMLFormat(); } if (expectedType == Long.class && rawValue instanceof XMLGregorianCalendar) { return (T) (Long) XmlTypeConverter.toMillis((XMLGregorianCalendar) rawValue); } // XML Enums (JAXB) if (expectedType.isEnum() && expectedType.getAnnotation(XmlEnum.class) != null && rawValue instanceof String) { return XmlTypeConverter.toXmlEnum(expectedType, (String) rawValue); } if (expectedType == String.class && rawValue.getClass().isEnum() && rawValue.getClass().getAnnotation(XmlEnum.class) != null) { return (T) XmlTypeConverter.fromXmlEnum(rawValue); } // Java Enums if (expectedType.isEnum() && rawValue instanceof String) { return (T) Enum.valueOf((Class<Enum>) expectedType, (String) rawValue); } if (expectedType == String.class && rawValue.getClass().isEnum()) { return (T) rawValue.toString(); } //QName if (expectedType == QName.class && rawValue instanceof QName) { return (T) rawValue; } if (expectedType == QName.class && rawValue instanceof String) { return (T) QNameUtil.uriToQName((String) rawValue); } throw new IllegalArgumentException("Expected " + expectedType + " type, but got " + rawValue.getClass()); }
From source file:com.google.api.tools.framework.importers.swagger.AuthBuilder.java
/** * Returns whether vendor extension contains the particular extension. *//* www . j a v a 2s. c o m*/ private static <T> boolean hasVendorExtension(Map<String, Object> vendorExtensions, String extensionName, Class<T> clazz) { if (vendorExtensions != null) { Object extensionValue = vendorExtensions.get(extensionName); if (extensionValue != null && clazz.isInstance(extensionValue)) { return true; } } return false; }
From source file:com.hiway.util.Assert.java
/** * Assert that the provided object is an instance of the provided class. * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * @param type the type to check against * @param obj the object to check/* w w w .j a v a2 s . co m*/ * @param message a message which will be prepended to the message produced by * the function itself, and which may be used to provide context. It should * normally end in a ": " or ". " so that the function generate message looks * ok when prepended to it. * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class<?> type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throw new IllegalArgumentException((StringUtils.isNotBlank(message) ? message + " " : "") + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } }
From source file:Main.java
public final static Component getDescendantOfClass(Class<?> c, Container comp, boolean exactInstance) { Component theObject = null;//from ww w . ja v a 2 s. co m if (c != null && comp != null) { Component[] carray = comp.getComponents(); if (carray != null) { for (int i = 0; i < carray.length && theObject == null; i++) { if (exactInstance && carray[i].getClass() == c) { theObject = carray[i]; } else if (!exactInstance && c.isInstance(carray[i])) { theObject = carray[i]; } else if (carray[i] instanceof Container) { theObject = getDescendantOfClass(c, (Container) carray[i], exactInstance); } } } } return theObject; }