List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:org.apache.taverna.scufl2.translator.t2flow.defaultactivities.AbstractActivityParser.java
public <ConfigType> ConfigType unmarshallConfig(T2FlowParser t2FlowParser, ConfigBean configBean, String encoding, Class<ConfigType> configType) throws ReaderException { Object config = configBean.getAny(); if (config instanceof JAXBElement) { JAXBElement<?> jaxbElement = (JAXBElement<?>) config; if (!configType.isInstance((jaxbElement.getValue()))) throw new ReaderException("Unexpected config type: " + jaxbElement.getValue().getClass() + ", expected " + configType); return configType.cast(jaxbElement.getValue()); }/* w w w.j a v a2s . com*/ if (!(config instanceof Element) || !configBean.getEncoding().equals(encoding)) throw new ReaderException("Unsupported config bean " + configBean); return unmarshallElement(t2FlowParser, (Element) config, configType); }
From source file:com.redhat.rhn.manager.user.test.UpdateUserCommandTest.java
private void assertCommandThrows(Class expectedEx, UpdateUserCommand cmd) { try {/*from w w w . ja va 2s.co m*/ cmd.updateUser(); fail("Expected [" + expectedEx.getName() + "]"); } catch (Exception e) { assertTrue("Expected [" + expectedEx.getName() + "]", expectedEx.isInstance(e)); } }
From source file:org.spring.data.gemfire.cache.JsonToPdxToObjectDataAccessIntegrationTest.java
protected <T> T fromPdx(Object pdxInstance, Class<T> toType) { try {/*from ww w . j a va 2s . c o m*/ if (pdxInstance == null) { return null; } else if (toType.isInstance(pdxInstance)) { return toType.cast(pdxInstance); } else if (pdxInstance instanceof PdxInstance) { return new ObjectMapper().readValue(JSONFormatter.toJSON(((PdxInstance) pdxInstance)), toType); } else { throw new IllegalArgumentException(String.format( "Expected object of type PdxInstance; but was (%1$s)", pdxInstance.getClass().getName())); } } catch (IOException e) { throw new RuntimeException(String.format("Failed to convert PDX to object of type (%1$s)", toType), e); } }
From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java
@SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotationAtMatchingOffsets(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation);/*from ww w .j a v a2s .com*/ if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); // Now that we have our starting point, we go to the right until we find an annotation of the correct type and // the same offsets as focusAnnotation Annotation currentAnnotation = null; while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.equals(currentRange)) return (T) currentAnnotation; cursor.moveToNext(); } return null; }
From source file:org.codhaus.groovy.grails.validation.ext.ConstrainedPropertyGunn.java
public static void removeConstraint(String name, Class<?> constraintClass) { Assert.hasLength(name, "Argument [name] cannot be null"); List<Object> objects = getOrInitializeConstraint(name); objects.remove(constraintClass);/*from www .jav a 2 s . c o m*/ List<Object> toRemove = new ArrayList<Object>(); for (Object object : objects) { if (constraintClass.isInstance(object)) { toRemove.add(object); } } objects.removeAll(toRemove); }
From source file:com.mystudy.source.spring.aop.JdkDynamicAopProxy.java
/** * /*from w w w .j ava 2s.c om*/ */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MethodInvocation invocation; Object oldProxy = null; boolean setProxyContext = false; TargetSource targetSource = this.advised.targetSource; Class<?> targetClass = null; Object target = null; try {//eqal if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) { // The target does not implement the equals(Object) method itself. return equals(args[0]); } //hashcode if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) { // The target does not implement the hashCode() method itself. return hashCode(); } if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) { // Service invocations on ProxyConfig with the proxy config... return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args); } Object retVal; if (this.advised.exposeProxy) { // Make invocation available if necessary. oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } // May be null. Get as late as possible to minimize the time we "own" the target, // in case it comes from a pool. target = targetSource.getTarget(); if (target != null) { targetClass = target.getClass(); } // Get the interception chain for this method. List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); // Check whether we have any advice. If we don't, we can fallback on direct // reflective invocation of the target, and avoid creating a MethodInvocation. if (chain.isEmpty()) { // We can skip creating a MethodInvocation: just invoke the target directly // Note that the final invoker must be an InvokerInterceptor so we know it does // nothing but a reflective operation on the target, and no hot swapping or fancy proxying. retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { //chain???? // We need to create a method invocation... invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain. retVal = invocation.proceed(); } // Massage return value if necessary. Class<?> returnType = method.getReturnType(); if (retVal != null && retVal == target && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) { // Special case: it returned "this" and the return type of the method // is type-compatible. Note that we can't help if the target sets // a reference to itself in another returned object. retVal = proxy; } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) { throw new AopInvocationException( "Null return value from advice does not match primitive return type for: " + method); } return retVal; } finally { if (target != null && !targetSource.isStatic()) { // Must have come from TargetSource. targetSource.releaseTarget(target); } if (setProxyContext) { // Restore old proxy. AopContext.setCurrentProxy(oldProxy); } } }
From source file:com.github.jknack.handlebars.helper.MethodHelper.java
/** * Choose between context, options or a possible argument that matches the parameter type. * * @param paramType The expected parameter type. * @param argument The possible argument. * @param context The context object./*from www .j av a2s. c o m*/ * @param options The options object. * @return An object argument. */ private Object argument(final Class<?> paramType, final Object argument, final Object context, final Options options) { // priority order is as follows: // 1. context // 2. argument // 3. options for (Object candidate : new Object[] { context, argument, options }) { if (paramType.isInstance(candidate) || wrap(paramType).isInstance(candidate)) { return candidate; } } return null; }
From source file:org.impalaframework.extension.mvc.annotation.handler.LightweightAnnotationHandlerAdapter.java
protected boolean hasHandlerAnnotation(Object handler, final Class<? extends Annotation> annotationClass) { final AnnotationHandlerMethodResolver methodResolver = getMethodResolver(handler); final Collection<Annotation> handlerAnnotations = methodResolver.getHandlerAnnotations(); for (Annotation annotation : handlerAnnotations) { if (annotationClass.isInstance(annotation)) return true; }/*from w ww. ja v a2s . com*/ return false; }
From source file:net.nicholaswilliams.java.licensing.ObjectSerializer.java
/** * Deserializes an object of the specified type from the provided byte stream. * * @param expectedType The type that is expected to be retrieved from {@code byteStream} (must implement {@link Serializable}) * @param byteStream The byte stream to retrieve the object from (it must contain exactly one object, of the exact type passed to {@code expectedType}) * @return the requested unserialized object, presumably in the stream. * @throws ObjectTypeNotExpectedException If the object found in the stream does not match the type {@code expectedType} or if a {@link ClassNotFoundException} or {@link NoClassDefFoundError} occurs * @throws ObjectDeserializationException If an I/O exception occurs while deserializing the object from the stream *//*ww w .j a va 2 s . com*/ public final <T extends Serializable> T readObject(Class<T> expectedType, byte[] byteStream) throws ObjectDeserializationException { ByteArrayInputStream bytes = new ByteArrayInputStream(byteStream); ObjectInputStream stream = null; try { stream = new ObjectInputStream(bytes); Object allegedObject = stream.readObject(); if (!expectedType.isInstance(allegedObject)) { throw new ObjectTypeNotExpectedException(expectedType.getName(), allegedObject.getClass().getName()); } return expectedType.cast(allegedObject); } catch (IOException e) { throw new ObjectDeserializationException( "An I/O error occurred while reading the object from the byte array.", e); } catch (ClassNotFoundException e) { throw new ObjectTypeNotExpectedException(expectedType.getName(), e.getMessage(), e); } catch (NoClassDefFoundError e) { throw new ObjectTypeNotExpectedException(expectedType.getName(), e.getMessage(), e); } finally { try { if (stream != null) stream.close(); } catch (IOException ignore) { } } }
From source file:org.pivotal.gemfire.pdx.GemFireJsonToPdxToObjectDataAccessIntegrationTest.java
protected <T> T fromPdx(Object pdxInstance, Class<T> toType) { try {/*from www .j a v a 2 s . c om*/ if (pdxInstance == null) { return null; } else if (toType.isInstance(pdxInstance)) { return toType.cast(pdxInstance); } else if (pdxInstance instanceof PdxInstance) { return new ObjectMapper().readValue(JSONFormatter.toJSON(((PdxInstance) pdxInstance)), toType); } else { throw new IllegalArgumentException(String.format( "Expected object of type PdxInstance; but was (%1$s)", nullSafeClassName(pdxInstance))); } } catch (IOException e) { throw new RuntimeException(String.format("Failed to convert PDX to object of type (%1$s)", toType), e); } }