List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:com.switchfly.inputvalidation.CobrandNameValidationStrategyTest.java
public static void assertThrowException(Class<? extends Throwable> exceptionClass, Closure closure) { try {//from ww w . j a va 2 s.co m closure.execute(null); fail("Should throw " + exceptionClass.getName()); } catch (Exception e) { assertTrue(exceptionClass.isAssignableFrom(e.getClass())); } }
From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.KayentaService.java
private static AbstractCanaryServiceIntegration getServiceIntegrationByClass(Canary canary, Class<? extends AbstractCanaryServiceIntegration> serviceIntegrationClass) { return canary.getServiceIntegrations().stream() .filter(s -> serviceIntegrationClass.isAssignableFrom(s.getClass())).findFirst() .orElseThrow(() -> new IllegalArgumentException("Canary service integration of type " + serviceIntegrationClass.getSimpleName() + " not found.")); }
From source file:com.opensymphony.xwork2.util.ProxyUtil.java
/** * Check whether the given class implements an interface with a given class name. * @param clazz the class to check//from w ww . j a v a 2 s . com * @param ifaceClassName the interface class name to check */ private static boolean implementsInterface(Class<?> clazz, String ifaceClassName) { try { Class<?> ifaceClass = ClassLoaderUtil.loadClass(ifaceClassName, ProxyUtil.class); return ifaceClass.isAssignableFrom(clazz); } catch (ClassNotFoundException e) { return false; } }
From source file:org.openmrs.module.kenyaemr.calculation.CalculationManager.java
/** * Instantiates and configures a calculation * @param clazz the calculation class/* www. j a va 2 s. c o m*/ * @param configuration the configuration * @return the calculation instance */ public static BaseEmrCalculation instantiateCalculation(Class<? extends BaseEmrCalculation> clazz, String configuration) { try { BaseEmrCalculation calc = clazz.newInstance(); if (configuration != null && clazz.isAssignableFrom(ConfigurableCalculation.class)) { ((ConfigurableCalculation) calc).setConfiguration(configuration); } return calc; } catch (Exception ex) { return null; } }
From source file:com.springframework.core.io.support.SpringFactoriesLoader.java
@SuppressWarnings("unchecked") private static <T> T instantiateFactory(String instanceClassName, Class<T> factoryClass, ClassLoader classLoader) { try {/* w w w . jav a 2 s . c o m*/ Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader); if (!factoryClass.isAssignableFrom(instanceClass)) { throw new IllegalArgumentException( "Class [" + instanceClassName + "] is not assignable to [" + factoryClass.getName() + "]"); } return (T) instanceClass.newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate factory class: " + factoryClass.getName(), ex); } }
From source file:jenkins.plugins.git.MethodUtils.java
/** * <p>Retrieves a method whether or not it's accessible. If no such method * can be found, return {@code null}.</p> * * @param cls The class that will be subjected to the method search * @param methodName The method that we wish to call * @param parameterTypes Argument class types * @return The method//w w w . j av a 2 s . c om */ static Method getMethodImpl(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { Validate.notNull(cls, "Null class not allowed."); Validate.notEmpty(methodName, "Null or blank methodName not allowed."); // fast path, check if directly declared on the class itself for (final Method method : cls.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } if (!cls.isInterface()) { // ok, now check if directly implemented on a superclass // Java 8: note that super-interface implementations trump default methods for (Class<?> klass = cls.getSuperclass(); klass != null; klass = klass.getSuperclass()) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } } } // ok, now we are looking for an interface method... the most specific one // in the event that we have two unrelated interfaces both declaring a method of the same name // we will give up and say we could not find the method (the logic here is that we are primarily // checking for overrides, in the event of a Java 8 default method, that default only // applies if there is no conflict from an unrelated interface... thus if there are // default methods and they are unrelated then they don't exist... if there are multiple unrelated // abstract methods... well they won't count as a non-abstract implementation Method res = null; for (final Class<?> klass : (List<Class<?>>) ClassUtils.getAllInterfaces(cls)) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { if (res == null) { res = method; } else { Class<?> c = res.getDeclaringClass(); if (c == klass) { // match, ignore } else if (c.isAssignableFrom(klass)) { // this is a more specific match res = method; } else if (!klass.isAssignableFrom(c)) { // multiple overlapping interfaces declare this method and there is no common ancestor return null; } } } } } return res; }
From source file:edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList.java
/** * Replace the first instance of this class of policy in the list. If no * instance is found, add the policy to the end of the list. *//*w ww .jav a 2s. c om*/ public static void replacePolicy(ServletContext sc, PolicyIface policy) { if (policy == null) { return; } Class<?> clzz = policy.getClass(); PolicyList policies = getPolicyList(sc); ListIterator<PolicyIface> it = policies.listIterator(); while (it.hasNext()) { if (clzz.isAssignableFrom(it.next().getClass())) { it.set(policy); return; } } addPolicy(sc, policy); }
From source file:Main.java
public static boolean isAssignableFrom(Class<?> toClass, Class<?> fromClass) { if (toClass == Object.class && !fromClass.isPrimitive()) { return true; }//from w w w . ja v a 2 s.c om if (toClass.isPrimitive()) { fromClass = getPrimitiveClassIfWrapper(fromClass); } return toClass.isAssignableFrom(fromClass); }
From source file:com.sonicle.webtop.core.app.util.ClassHelper.java
public static Class loadClass(String className, Class requiredParentClass, String targetDescription) { String tdesc = StringUtils.defaultIfBlank(targetDescription, "Target"); try {/*from w w w . jav a2s . co m*/ Class clazz = Class.forName(className); if (!requiredParentClass.isAssignableFrom(clazz)) throw new ClassCastException(); return clazz; } catch (ClassNotFoundException ex) { LOGGER.debug("{} class not found [{}]", tdesc, className); } catch (ClassCastException ex) { LOGGER.warn("A valid {} class must extends '{}' class", tdesc, requiredParentClass.toString()); } catch (Throwable t) { LOGGER.error("Unable to load class [{}]", className, t); } return null; }
From source file:com.cloudera.oryx.common.OryxTest.java
public static <T> void assertInstanceOf(T value, Class<? extends T> clazz) { assertTrue(value + " of type " + value.getClass() + " should be a " + clazz, clazz.isAssignableFrom(value.getClass())); }