List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:com.codelanx.codelanxlib.config.Config.java
/** * Attempts to return the {@link Config} value as a casted type. If the * value cannot be casted it will attempt to return the default value. If * the default value is inappropriate for the class, the method will * throw a {@link ClassCastException}. This exception is also throwing if * the type used for the members contained within the collection are * incorrect/*from www .j a v a2 s . c om*/ * * @since 0.1.0 * @version 0.1.0 * * @param <T> The type of the casting class * @param <G> The type of the collection's contents * @param collection The collection type to cast to * @param type The generic bounding for the collection * @return A casted value, or {@code null} if unable to cast. If the passed * class parameter is of a primitive type or autoboxed primitive, * then a casted value of -1 is returned, or {@code false} for * booleans. If the passed class parameter is for {@link String}, * then {@link Object#toString()} is called on the value instead */ @SuppressWarnings("rawtypes") default public <G, T extends Collection<G>> T as(Class<? extends Collection> collection, Class<G> type) { Collection<G> col = this.as(collection); for (Object o : col) { if (!type.isInstance(o)) { throw new ClassCastException("Inappropriate generic type for collection"); } } return (T) col; }
From source file:hudson.Util.java
/** * Create a sub-list by only picking up instances of the specified type. *///from www . ja v a2 s . c om public static <T> List<T> createSubList(Collection<?> source, Class<T> type) { List<T> r = new ArrayList<T>(); for (Object item : source) { if (type.isInstance(item)) { r.add(type.cast(item)); } } return r; }
From source file:com.amazon.carbonado.spi.AbstractRepository.java
/** * Default implementation checks if Repository implements Capability * interface, and if so, returns the Repository. *///w ww.j a va 2 s .c o m @SuppressWarnings("unchecked") public <C extends Capability> C getCapability(Class<C> capabilityType) { if (capabilityType.isInstance(this)) { return (C) this; } return null; }
From source file:de.codecentric.boot.admin.config.AdminServerWebConfiguration.java
private boolean hasConverter(List<HttpMessageConverter<?>> converters, Class<? extends HttpMessageConverter<?>> clazz) { for (HttpMessageConverter<?> converter : converters) { if (clazz.isInstance(converter)) { return true; }/* w w w . java2 s . com*/ } return false; }
From source file:com.google.inject.persist.jpa.KuneJpaLocalTxnInterceptor.java
/** * Returns True if rollback DID NOT HAPPEN (i.e. if commit should continue). * //from w w w .ja v a 2s .c o m * @param transactional * The metadata annotaiton of the method * @param e * The exception to test for rollback * @param txn * A JPA Transaction to issue rollbacks on * @return true, if successful */ private boolean rollbackIfNecessary(final KuneTransactional transactional, final Exception e, final EntityTransaction txn) { boolean commit = true; // check rollback clauses for (final Class<? extends Exception> rollBackOn : transactional.rollbackOn()) { // if one matched, try to perform a rollback if (rollBackOn.isInstance(e)) { commit = false; // check ignore clauses (supercedes rollback clause) for (final Class<? extends Exception> exceptOn : transactional.ignore()) { // An exception to the rollback clause was found, DON'T rollback // (i.e. commit and throw anyway) if (exceptOn.isInstance(e)) { commit = true; break; } } // rollback only if nothing matched the ignore check if (!commit) { txn.rollback(); } // otherwise continue to commit break; } } return commit; }
From source file:edu.vt.middleware.ldap.handler.AbstractConnectionHandler.java
/** {@inheritDoc} */ public void connect(final String dn, final Object credential) throws NamingException { NamingException lastThrown = null; final String[] urls = this.parseLdapUrl(this.config.getLdapUrl(), this.connectionStrategy); for (String url : urls) { final Hashtable<String, Object> env = new Hashtable<String, Object>(this.config.getEnvironment()); env.put(LdapConstants.PROVIDER_URL, url); try {/*from w ww. ja v a 2 s.c o m*/ if (this.logger.isTraceEnabled()) { this.logger.trace("{" + this.connectionCount + "} Attempting connection to " + url + " for strategy " + this.connectionStrategy); } this.connectInternal(this.config.getAuthtype(), dn, credential, env); this.connectionCount.incrementCount(); lastThrown = null; break; } catch (NamingException e) { lastThrown = e; if (this.logger.isDebugEnabled()) { this.logger.debug("Error connecting to LDAP URL: " + url, e); } boolean ignoreException = false; if (this.connectionRetryExceptions != null && this.connectionRetryExceptions.length > 0) { for (Class<?> ne : this.connectionRetryExceptions) { if (ne.isInstance(e)) { ignoreException = true; break; } } } if (!ignoreException) { break; } } } if (lastThrown != null) { throw lastThrown; } }
From source file:edu.jhuapl.graphs.jfreechart.TimeSeriesRenderer.java
private <T> T getItemProperty(int row, int column, Class<T> klazz, T defaultValue, String key) { Map<String, Object> metadata = getMetadata(row, column); if (metadata == null) { return defaultValue; }// w w w. ja va 2 s.c o m Object o = metadata.get(key); if (o != null && klazz.isInstance(o)) { return klazz.cast(o); } else { return defaultValue; } }
From source file:com.codelanx.codelanxlib.config.Config.java
/** * Attempts to return the {@link Config} value as a casted type. If the * value cannot be casted it will attempt to return the default value. If * the default value is inappropriate for the class, the method will * throw a {@link ClassCastException}. This exception is also throwing if * the type used for the members contained within the collection are * incorrect/*from ww w . jav a 2 s. c o m*/ * * @since 0.1.0 * @version 0.1.0 * * @param <K> The type of the keys for this map * @param <V> The type of the values for this map * @param <M> The type of the map * @param map The class object of the map type to use * @param key The class object of the key types * @param value The class object of the value types * @return A casted value, or {@code null} if unable to cast. If the passed * class parameter is of a primitive type or autoboxed primitive, * then a casted value of -1 is returned, or {@code false} for * booleans. If the passed class parameter is for {@link String}, * then {@link Object#toString()} is called on the value instead */ @SuppressWarnings("rawtypes") default public <K, V, M extends Map<K, V>> M as(Class<? extends Map> map, Class<K> key, Class<V> value) { Map<?, ?> m = this.as(map); for (Map.Entry<?, ?> ent : m.entrySet()) { if (!key.isInstance(ent.getKey()) || !value.isInstance(ent.getValue())) { throw new ClassCastException("Inappropriate generic types for map"); } } return (M) m; }
From source file:io.haze.core.Application.java
/** * Retrieve a collection of services whose class matches <code>T</code>. * * @param clazz The class type./*from w w w . ja v a 2 s .c o m*/ */ public <T> Collection<T> getServices(Class<T> clazz) { List<T> matches = new ArrayList(); for (Service service : services) { if (clazz.isInstance(service)) { matches.add((T) service); } } return matches; }
From source file:jails.http.converter.xml.MarshallingHttpMessageConverter.java
@Override protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException { Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); try {// w w w. j a v a 2 s. co m Object result = this.unmarshaller.unmarshal(source); if (!clazz.isInstance(result)) { throw new TypeMismatchException(result, clazz); } return result; } catch (UnmarshallingFailureException ex) { throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex); } }