List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:com.textocat.textokit.io.brat.UIMA2BratAnnotator.java
private static boolean isEveryInstanceOf(Iterable<?> srcCol, Class<?> testClass) { for (Object e : srcCol) { if (!testClass.isInstance(e)) { return false; }/*from www.j a va2 s. com*/ } return true; }
From source file:com.xdtech.core.orm.utils.AssertUtils.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/*from ww w. ja va 2 s . c om*/ * @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(message + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } }
From source file:org.shept.util.BeanUtilsExtended.java
/** * Merge the property values of the given source bean into the given target bean. * <p>Note: Only not-null values are merged into the given target bean. * 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 w w. j ava2 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 mergeProperties(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 = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:com.atlassian.jira.rest.client.TestUtil.java
@SuppressWarnings("unused") public static <T extends Throwable> void assertThrows(Class<T> clazz, String regexp, Runnable runnable) { try {/*from w w w .j a v a 2s . com*/ runnable.run(); Assert.fail(clazz.getName() + " exception expected"); } catch (Throwable e) { Assert.assertTrue( "Expected exception of class " + clazz.getName() + " but was caught " + e.getClass().getName(), clazz.isInstance(e)); if (e.getMessage() == null && regexp != null) { Assert.fail("Exception with no message caught, while expected regexp [" + regexp + "]"); } if (regexp != null && e.getMessage() != null) { Assert.assertTrue("Message [" + e.getMessage() + "] does not match regexp [" + regexp + "]", e.getMessage().matches(regexp)); } } }
From source file:org.opencron.server.dao.HibernateDao.java
private static boolean isSimple(Object value) { Class<? extends Object> type = value.getClass(); if (type.isArray() || simpleTypes.contains(type)) return true; for (Class clazz : simpleTypes) { if (clazz.isInstance(value)) return true; }/*from w ww . j av a 2s. co m*/ return false; }
From source file:com.xdtech.core.orm.utils.AssertUtils.java
public static void isInstanceOf(Class type, Object obj, RuntimeException throwIfAssertFail) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throw throwIfAssertFail; }//w w w. j a v a 2 s . c om }
From source file:com.framework.utils.error.PreConditions.java
public static void checkInstanceOf(final Class<?> type, final Object obj, String errorMessageTemplate, Object... errorMessageArgs) { if (!type.isInstance(obj)) { throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs)); }/*from ww w . j a va2 s . co m*/ }
From source file:org.wildfly.camel.test.hipchat.HipchatProducerIntegrationTest.java
public static <T> T assertIsInstanceOf(Class<T> expectedType, Object value) { Assert.assertNotNull("Expected an instance of type: " + expectedType.getName() + " but was null", value); Assert.assertTrue("Object should be of type " + expectedType.getName() + " but was: " + value + " with the type: " + value.getClass().getName(), expectedType.isInstance(value)); return expectedType.cast(value); }
From source file:org.crazydog.util.spring.NumberUtils.java
/** * Convert the given number into an instance of the given target class. * * @param number the number to convert * @param targetClass the target class to convert to * @return the converted number//from ww w . j av a 2 s. c om * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see Byte * @see Short * @see Integer * @see Long * @see BigInteger * @see Float * @see Double * @see BigDecimal */ @SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { org.springframework.util.Assert.notNull(number, "Number must not be null"); org.springframework.util.Assert.notNull(targetClass, "Target class must not be null"); if (targetClass.isInstance(number)) { return (T) number; } else if (Byte.class == targetClass) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Byte(number.byteValue()); } else if (Short.class == targetClass) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Short(number.shortValue()); } else if (Integer.class == targetClass) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Integer(number.intValue()); } else if (Long.class == targetClass) { BigInteger bigInt = null; if (number instanceof BigInteger) { bigInt = (BigInteger) number; } else if (number instanceof BigDecimal) { bigInt = ((BigDecimal) number).toBigInteger(); } // Effectively analogous to JDK 8's BigInteger.longValueExact() if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) { raiseOverflowException(number, targetClass); } return (T) new Long(number.longValue()); } else if (BigInteger.class == targetClass) { if (number instanceof BigDecimal) { // do not lose precision - use BigDecimal's own conversion return (T) ((BigDecimal) number).toBigInteger(); } else { // original value is not a Big* number - use standard long conversion return (T) BigInteger.valueOf(number.longValue()); } } else if (Float.class == targetClass) { return (T) new Float(number.floatValue()); } else if (Double.class == targetClass) { return (T) new Double(number.doubleValue()); } else if (BigDecimal.class == targetClass) { // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return (T) new BigDecimal(number.toString()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); } }
From source file:com.amalto.workbench.compare.Utilities.java
public static Object getAdapter(Object element, Class adapterType, boolean load) { if (adapterType.isInstance(element)) return element; if (element instanceof IAdaptable) { Object adapted = ((IAdaptable) element).getAdapter(adapterType); if (adapterType.isInstance(adapted)) return adapted; }/*www . j a va 2s. c o m*/ if (load) { Object adapted = Platform.getAdapterManager().loadAdapter(element, adapterType.getName()); if (adapterType.isInstance(adapted)) return adapted; } else { Object adapted = Platform.getAdapterManager().getAdapter(element, adapterType); if (adapterType.isInstance(adapted)) return adapted; } return null; }