Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

In this page you can find the example usage for java.lang Class isAssignableFrom.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

From source file:kenh.xscript.impl.BaseElement.java

/**
 * Check {@code Exclude} annotation, if child element's class is in exclude
 * list, this child element is not allowed to add.
 * If {@code Exclude} annotation do not define, all child element is allowed.
 * /*w  ww .ja va  2  s  . c om*/
 * @param c
 * @return true  this child element is not allowed to add.
 */
private static boolean isExcludedChildElement(Element el, Class c) {
    if (!isElement(c))
        return true; // if not an Element

    Exclude e = el.getClass().getAnnotation(Exclude.class);
    if (e == null)
        return false;

    Class[] classes = e.value();
    if (classes == null || classes.length <= 0)
        return false;

    for (Class class_ : classes) {
        if (class_.isAssignableFrom(c))
            return true;
    }

    return false;
}

From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean shouldConvertToString(Class theClass) {
    for (Class classToCheck : convertToString) {
        if (classToCheck.isAssignableFrom(theClass))
            return true;
    }// w  ww.j  av a 2s  . c  o m
    return false;
}

From source file:edu.indiana.lib.osid.base.loader.OsidLoader.java

/**
 * Returns an instance of the org.osid.OsidManager of the OSID specified by the OSID
 * package org.osid.OsidManager interface name and the implementation package name.
 * The implementation class name is constructed from the SID package
 * Manager interface name. A configuration file name is constructed in a
 * similar manner and if the file exists it is loaded into the
 * implementation's org.osid.OsidManager's configuration.
 *
 * <p>/*  w ww.ja  v a  2s  .  c om*/
 * Example:  To load an implementation of the org.osid.Filing OSID
 * implemented in a package "xyz", one would use:
 * </p>
 *
 * <p>
 * org.osid.filing.FilingManager fm =
 * (org.osid.filing.FilingManager)org.osid.OsidLoader.getManager(
 * </p>
 *
 * <p>
 * "org.osid.filing.FilingManager" ,
 * </p>
 *
 * <p>
 * "xyz" ,
 * </p>
 *
 * <p>
 * new org.osid.OsidContext());
 * </p>
 *
 * @param osidPackageManagerName osidPackageManagerName is a fully
 *        qualified org.osid.OsidManager interface name
 * @param implPackageName implPackageName is a fully qualified
 *        implementation package name
 * @param context
 * @param additionalConfiguration
 *
 * @return org.osid.OsidManager
 *
 * @throws org.osid.OsidException An exception with one of the following
 *         messages defined in org.osid.OsidException:  {@link
 *         org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
 *         {@link org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT},
 *         {@link org.osid.OsidException#VERSION_ERROR VERSION_ERROR},
 *         ={@link org.osid.OsidException#INTERFACE_NOT_FOUND
 *         INTERFACE_NOT_FOUND}, ={@link
 *         org.osid.OsidException#MANAGER_NOT_FOUND MANAGER_NOT_FOUND},
 *         ={@link org.osid.OsidException#MANAGER_INSTANTIATION_ERROR
 *         MANAGER_INSTANTIATION_ERROR}, ={@link
 *         org.osid.OsidException#ERROR_ASSIGNING_CONTEXT
 *         ERROR_ASSIGNING_CONTEXT}, ={@link
 *         org.osid.OsidException#ERROR_ASSIGNING_CONFIGURATION
 *         ERROR_ASSIGNING_CONFIGURATION}
 */
public static org.osid.OsidManager getManager(String osidPackageManagerName, String implPackageName,
        org.osid.OsidContext context, java.util.Properties additionalConfiguration)
        throws org.osid.OsidException {

    try {
        if ((null != context) && (null != osidPackageManagerName) && (null != implPackageName)) {

            String osidInterfaceName = osidPackageManagerName;
            String className = makeClassName(osidPackageManagerName);
            String managerClassName = makeFullyQualifiedClassName(implPackageName, className);

            Class osidInterface = Class.forName(osidInterfaceName);

            if (null != osidInterface) {
                Class managerClass = Class.forName(managerClassName);

                if (null != managerClass) {
                    if (osidInterface.isAssignableFrom(managerClass)) {
                        org.osid.OsidManager manager = (org.osid.OsidManager) managerClass.newInstance();

                        if (null != manager) {
                            try {
                                manager.osidVersion_2_0();
                            } catch (Throwable ex) {
                                throw new org.osid.OsidException(org.osid.OsidException.VERSION_ERROR);
                            }

                            try {
                                manager.assignOsidContext(context);
                            } catch (Exception ex) {
                                throw new org.osid.OsidException(
                                        org.osid.OsidException.ERROR_ASSIGNING_CONTEXT);
                            }

                            try {
                                java.util.Properties configuration = getConfiguration(manager);

                                if (null == configuration) {
                                    configuration = new java.util.Properties();
                                }

                                if (null != additionalConfiguration) {
                                    java.util.Enumeration enumer = additionalConfiguration.propertyNames();

                                    while (enumer.hasMoreElements()) {
                                        java.io.Serializable key = (java.io.Serializable) enumer.nextElement();

                                        if (null != key) {
                                            java.io.Serializable value = (java.io.Serializable) additionalConfiguration
                                                    .get(key);

                                            if (null != value) {
                                                configuration.put(key, value);
                                            }
                                        }
                                    }
                                }

                                manager.assignConfiguration(configuration);

                                return manager;
                            } catch (Exception ex) {
                                throw new org.osid.OsidException(
                                        org.osid.OsidException.ERROR_ASSIGNING_CONFIGURATION);
                            }
                        }

                        throw new org.osid.OsidException(org.osid.OsidException.MANAGER_INSTANTIATION_ERROR);
                    }

                    throw new org.osid.OsidException(org.osid.OsidException.MANAGER_NOT_OSID_IMPLEMENTATION);
                }

                throw new org.osid.OsidException(org.osid.OsidException.MANAGER_NOT_FOUND);
            }

            throw new org.osid.OsidException(org.osid.OsidException.INTERFACE_NOT_FOUND);
        }

        throw new org.osid.OsidException(org.osid.OsidException.NULL_ARGUMENT);
    } catch (org.osid.OsidException oex) {
        oex.printStackTrace();
        throw new org.osid.OsidException(oex.getMessage());
    } catch (java.lang.Throwable ex) {
        ex.printStackTrace();
        throw new org.osid.OsidException(org.osid.OsidException.OPERATION_FAILED);
    }
}

From source file:io.neba.core.resourcemodels.registration.ModelRegistry.java

/**
 * @param sources        can be <code>null</code>.
 * @param compatibleType can be <code>null</code>.
 * @return the original collection if sources or compatibleType are
 * <code>null</code>, or a collection representing the models
 * compatible to the given type.//from w w w .java2s .c  o  m
 */
private static Collection<OsgiModelSource<?>> filter(Collection<OsgiModelSource<?>> sources,
        Class<?> compatibleType) {
    Collection<OsgiModelSource<?>> compatibleSources = sources;
    if (sources != null && compatibleType != null) {
        compatibleSources = new ArrayList<>(sources.size());
        for (OsgiModelSource<?> source : sources) {
            if (compatibleType.isAssignableFrom(source.getModelType())) {
                compatibleSources.add(source);
            }
        }
    }
    return compatibleSources;
}

From source file:com.gemstone.gemfire.internal.security.GeodeSecurityUtil.java

/**
 * this method would never return null, it either throws an exception or
 * returns an object/*  w  w w  . j  a  v  a  2 s. c  o  m*/
 */
public static <T> T getObjectOfTypeFromClassName(String className, Class<T> expectedClazz) {
    Class actualClass = null;
    try {
        actualClass = ClassLoadUtil.classFromName(className);
    } catch (Exception ex) {
        throw new GemFireSecurityException("Instance could not be obtained, " + ex.toString(), ex);
    }

    if (!expectedClazz.isAssignableFrom(actualClass)) {
        throw new GemFireSecurityException(
                "Instance could not be obtained. Expecting a " + expectedClazz.getName() + " class.");
    }

    T actualObject = null;
    try {
        actualObject = (T) actualClass.newInstance();
    } catch (Exception e) {
        throw new GemFireSecurityException(
                "Instance could not be obtained. Error instantiating " + actualClass.getName(), e);
    }
    return actualObject;
}

From source file:com.holonplatform.core.internal.beans.DefaultBeanPropertySet.java

/**
 * Check the given {@link PathProperty} is of given type.
 * @param property Property to check//from w  w w . j  a v  a  2s. co m
 * @param type Required property type
 * @return Typed property
 * @throws TypeMismatchException If the given type is not consistent with actual property type
 */
@SuppressWarnings("unchecked")
private static <X extends PathProperty<?>> X checkPathPropertyType(PathProperty<?> property, Class<X> type) {
    if (!type.isAssignableFrom(property.getClass())) {
        throw new TypeMismatchException("Requested property type " + type.getName()
                + " doesn't match the actual property type " + property.getClass().getName());
    }
    return (X) property;
}

From source file:lapin.load.Loader.java

static private Object _impSymbols(Class clazz, Object export, Env env) throws IllegalAccessException {
    Lisp lisp = env.lisp();/*w ww  .j a  va  2 s .  c  o  m*/
    Field[] fields = clazz.getFields();
    Class symClass = Symbol.class;
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        int m = f.getModifiers();
        if (!Modifier.isStatic(m))
            continue;
        if (!symClass.isAssignableFrom(f.getType()))
            continue;
        Symbol sym = Data.symbol(f.get(null));
        lisp.getObarray().imp(sym.pkg(), sym);
        if (!Data.isNot(export))
            lisp.getObarray().exp(sym.pkg(), sym);
    }
    return Symbols.T;
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static Set<Class> getSubEntities(EntityManagerFactory emf, Class superClass) throws Exception {
    Set<Class> entities = new HashSet<Class>();
    for (Class aClass : getEntityClasses(emf)) {
        if (aClass.isAnnotationPresent(Entity.class) && aClass != superClass
                && superClass.isAssignableFrom(aClass)) {
            entities.add(aClass);/*  w  w w. j a  v  a  2  s  .com*/
        }
    }
    return entities;
}

From source file:com.kangdainfo.common.util.BeanUtil.java

/**
 *
 * @param attributes//from  w ww.j a va 2s  . c o m
 * @param source
 * @param name
 * @param depth
 * @throws IntrospectionException
 */
private static void goingUnder(Map<String, Object> attributes, Object source, String name, int depth)
        throws IntrospectionException {

    if (depth <= 0) {

        return;
    }

    PropertyDescriptor[] properties = getPropertyDescriptors(source);

    for (int i = 0; i < properties.length; i++) {

        String localName = properties[i].getName();

        Object value = getProperty(localName, source);

        if (value != null) {

            String tempName = dealWithAttributeName(name, localName);

            Class<? extends Object> vClass = value.getClass();

            if (!isAssignableFromAny(value, IS_NOT_FROM)) {

                goingUnder(attributes, value, tempName, depth - 1);

            }

            if (!vClass.isAssignableFrom(Class.class)) {

                attributes.put(tempName, value);

            }

        }

    }

}

From source file:com.github.nmorel.gwtjackson.rebind.bean.BeanProcessor.java

public static Optional<BeanIdentityInfo> processIdentity(TreeLogger logger, JacksonTypeOracle typeOracle,
        RebindConfiguration configuration, JClassType type, Optional<JsonIdentityInfo> jsonIdentityInfo,
        Optional<JsonIdentityReference> jsonIdentityReference) throws UnableToCompleteException {

    if (!jsonIdentityInfo.isPresent()) {
        jsonIdentityInfo = findFirstEncounteredAnnotationsOnAllHierarchy(configuration, type,
                JsonIdentityInfo.class);
    }/*from   ww  w  .  ja  va2 s  .  co m*/

    if (jsonIdentityInfo.isPresent() && ObjectIdGenerators.None.class != jsonIdentityInfo.get().generator()) {
        if (!jsonIdentityReference.isPresent()) {
            jsonIdentityReference = findFirstEncounteredAnnotationsOnAllHierarchy(configuration, type,
                    JsonIdentityReference.class);
        }

        String propertyName = jsonIdentityInfo.get().property();
        boolean alwaysAsId = jsonIdentityReference.isPresent() && jsonIdentityReference.get().alwaysAsId();
        Class<? extends ObjectIdGenerator<?>> generator = jsonIdentityInfo.get().generator();
        Class<?> scope = jsonIdentityInfo.get().scope();

        BeanIdentityInfo beanIdentityInfo;
        if (generator.isAssignableFrom(PropertyGenerator.class)) {

            beanIdentityInfo = new BeanIdentityInfo(propertyName, alwaysAsId, generator, scope);

        } else {

            JType identityType;
            if (IntSequenceGenerator.class == generator) {
                identityType = typeOracle.getType(Integer.class.getName());
            } else if (UUIDGenerator.class == generator) {
                identityType = typeOracle.getType(UUID.class.getName());
            } else {
                JClassType generatorType = typeOracle.getType(generator.getCanonicalName());
                JClassType objectIdGeneratorType = generatorType.getSuperclass();
                while (!objectIdGeneratorType.getQualifiedSourceName()
                        .equals(ObjectIdGenerator.class.getName())) {
                    objectIdGeneratorType = objectIdGeneratorType.getSuperclass();
                }
                identityType = objectIdGeneratorType.isParameterized().getTypeArgs()[0];
            }

            beanIdentityInfo = new BeanIdentityInfo(propertyName, alwaysAsId, generator, scope, identityType);

        }
        return Optional.of(beanIdentityInfo);
    }
    return Optional.absent();
}