List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:jp.terasoluna.fw.util.ConvertUtil.java
/** * ???/*ww w.j av a 2 s. co m*/ * <ul> * <li><code>null</code>?? - ?????<code>T</code>?????</li> * <li><code>Object[]</code>?? - <code>T</code>??????</li> * <li><code>Collection</code>?? - <code>T</code>?????</li> * <li>???? - ?1???<code>T</code>?????</li> * </ul> * * @param <E> ????? * @param obj * @param elementClass ????? * @return ??? * @throws IllegalArgumentException <code>clazz</code>? * <code>null</code>?? * <code>obj</code>?????????<code>T</code> * ????? */ @SuppressWarnings("unchecked") public static <E> List<E> toList(Object obj, Class<E> elementClass) throws IllegalArgumentException { if (elementClass == null) { throw new IllegalArgumentException("Argument 'elementClass' (" + Class.class.getName() + ") is null"); } Object[] array = toArray(obj); List<E> result = new ArrayList<E>(); for (Object element : array) { if (element != null && !elementClass.isAssignableFrom(element.getClass())) { String message = "Unable to cast '" + element.getClass().getName() + "' to '" + elementClass.getName() + "'"; throw new IllegalArgumentException(message, new ClassCastException(message)); } result.add((E) element); } return result; }
From source file:ips1ap101.lib.core.web.app.EJBL.java
public static Object lookup(Class<?> interfaz) { Bitacora.trace(EJBL.class, "lookup", interfaz); String key = EAC.JNDI_EJB_LOOKUP_PATTERN; String pattern = EA.getString(key); String base = interfaz.getSimpleName(); String bean = StringUtils.removeEnd(base, "Base") + "Bean"; String jndi = MessageFormat.format(pattern, bean, interfaz.getName()); Bitacora.trace(key + "=" + pattern); Bitacora.trace(key + "=" + jndi); try {// w ww .ja v a 2 s . co m Object facade = InitialContext.doLookup(jndi); boolean assignable = facade != null && interfaz.isAssignableFrom(facade.getClass()); Bitacora.trace(bean + "=" + facade); Bitacora.trace(base + "=" + assignable); return facade; } catch (NamingException ex) { throw new RuntimeException(ex); } }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java
/** * Obtain the concrete type used by an implementation of an interface that * uses a generic type.//www .j a v a 2s. c om * * @param type The interface that defines a generic type * @param clazz The class that implements the interface with a concrete type * @param <T> The interface type * * @return concrete type used by the implementation */ private static <T> Object getGenericType(Class<T> type, Class<? extends T> clazz) { // Look to see if this class implements the generic interface // Get all the interfaces Type[] interfaces = clazz.getGenericInterfaces(); for (Type iface : interfaces) { // Only need to check interfaces that use generics if (iface instanceof ParameterizedType) { ParameterizedType pi = (ParameterizedType) iface; // Look for the generic interface if (pi.getRawType() instanceof Class) { if (type.isAssignableFrom((Class<?>) pi.getRawType())) { return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); } } } } // Interface not found on this class. Look at the superclass. @SuppressWarnings("unchecked") Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass(); Object result = getGenericType(type, superClazz); if (result instanceof Class<?>) { // Superclass implements interface and defines explicit type for // generic return result; } else if (result instanceof Integer) { // Superclass implements interface and defines unknown type for // generic // Map that unknown type to the generic types defined in this class ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]); } else { // Error will be logged further up the call stack return null; } }
From source file:com.link_intersystems.lang.Conversions.java
/** * * @param from//from w w w .j ava2s .c o m * @param to * @return true if from is a subtype of t. * @since 1.0.0.0 */ public static boolean isWideningReference(Class<?> from, Class<?> to) { Assert.notNull("from", from); Assert.notNull("to", to); if (from.isArray() && to.isArray()) { from = from.getComponentType(); to = to.getComponentType(); } return to.isAssignableFrom(from); }
From source file:cn.gov.icon.base.util.Assert.java
/** * Assert that <code>superType.isAssignableFrom(subType)</code> is * <code>true</code>./*from w ww . ja v a2 s . c o m*/ * * <pre class="code"> * Assert.isAssignable(Number.class, myClass); * </pre> * * @param superType * the super type to check against * @param subType * the sub type 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 classes are not assignable */ public static void isAssignable(Class<?> superType, Class<?> subType, String message) { notNull(superType, "Type to check against must not be null"); if (subType == null || !superType.isAssignableFrom(subType)) { throw new IllegalArgumentException(message + subType + " is not assignable to " + superType); } }
From source file:com.bosscs.spark.commons.utils.Utils.java
public static Object castingUtil(String value, Class classCasting) { Object object = value;//from w w w .java 2 s . co m //Numeric if (Number.class.isAssignableFrom(classCasting)) { if (classCasting.isAssignableFrom(Double.class)) { return Double.valueOf(value); } else if (classCasting.isAssignableFrom(Long.class)) { return Long.valueOf(value); } else if (classCasting.isAssignableFrom(Float.class)) { return Float.valueOf(value); } else if (classCasting.isAssignableFrom(Integer.class)) { return Integer.valueOf(value); } else if (classCasting.isAssignableFrom(Short.class)) { return Short.valueOf(value); } else if (classCasting.isAssignableFrom(Byte.class)) { return Byte.valueOf(value); } } else if (String.class.isAssignableFrom(classCasting)) { return object.toString(); } //Class not recognise yet return null; }
From source file:com.buaa.cfs.utils.ReflectionUtils.java
/** * This code is to support backward compatibility and break the compile time dependency of core on mapred. This * should be made deprecated along with the mapred package HADOOP-1230. Should be removed when mapred package is * removed.//from w w w . ja v a 2s .c om */ private static void setJobConf(Object theObject, Configuration conf) { //If JobConf and JobConfigurable are in classpath, AND //theObject is of type JobConfigurable AND //conf is of type JobConf then //invoke configure on theObject try { Class<?> jobConfClass = conf.getClassByNameOrNull("org.apache.hadoop.mapred.JobConf"); if (jobConfClass == null) { return; } Class<?> jobConfigurableClass = conf.getClassByNameOrNull("org.apache.hadoop.mapred.JobConfigurable"); if (jobConfigurableClass == null) { return; } if (jobConfClass.isAssignableFrom(conf.getClass()) && jobConfigurableClass.isAssignableFrom(theObject.getClass())) { Method configureMethod = jobConfigurableClass.getMethod("configure", jobConfClass); configureMethod.invoke(theObject, conf); } } catch (Exception e) { throw new RuntimeException("Error in configuring object", e); } }
From source file:ips1ap101.ejb.core.BeanLocator.java
private static Object lookup(Class<?> local) { Bitacora.trace(BeanLocator.class, "lookup", local); String key = EAC.JNDI_EJB_LOOKUP_PATTERN; String pattern = EA.getString(key); String name = local.getSimpleName(); String root = StringUtils.removeEnd(name, LOCAL_SUFFIX); String arg0 = root + BEANS_SUFFIX; String arg1 = local.getName(); String jndi = MessageFormat.format(pattern, arg0, arg1); Bitacora.trace(key + "=" + pattern); Bitacora.trace(key + "=" + jndi); try {/*from w ww. ja v a 2 s . c om*/ Object object = InitialContext.doLookup(jndi); boolean assignable = object != null && local.isAssignableFrom(object.getClass()); Bitacora.trace(arg0 + "=" + object + ", assignable=" + assignable); return object; } catch (NamingException ex) { return null; } }
From source file:com.asakusafw.yaess.tools.log.cli.Main.java
private static <T> T create(CommandLine cmd, Option opt, Class<T> type, ClassLoader loader) { assert cmd != null; assert opt != null; assert type != null; assert loader != null; String value = cmd.getOptionValue(opt.getOpt()); Class<?> aClass;//from w w w.j a v a2s. c o m try { aClass = Class.forName(value, false, loader); } catch (ClassNotFoundException e) { throw new IllegalArgumentException( MessageFormat.format("Failed to initialize the class \"{1}\" (-{0})", opt.getOpt(), value), e); } if (type.isAssignableFrom(aClass) == false) { throw new IllegalArgumentException(MessageFormat.format("\"{1}\" must be a subtype of \"{2}\" (-{0})", opt.getOpt(), value, type.getName())); } try { return aClass.asSubclass(type).getConstructor().newInstance(); } catch (Exception e) { throw new IllegalArgumentException( MessageFormat.format("Failed to initialize the class \"{1}\" (-{0})", opt.getOpt(), value), e); } }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Determine whether the given method explicitly declares the given * exception or one of its superclasses, which means that an exception of * that type can be propagated as-is within a reflective invocation. * //from w ww. j a v a 2 s.c o m * @param method the declaring method * @param exceptionType the exception to throw * @return <code>true</code> if the exception can be thrown as-is; * <code>false</code> if it needs to be wrapped */ public static boolean declaresException(final Method method, final Class<?> exceptionType) { Validate.notNull(method, "Method must not be null"); final Class<?>[] declaredExceptions = method.getExceptionTypes(); for (final Class<?> declaredException : declaredExceptions) { if (declaredException.isAssignableFrom(exceptionType)) { return true; } } return false; }