Example usage for java.lang Class isInterface

List of usage examples for java.lang Class isInterface

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInterface();

Source Link

Document

Determines if the specified Class object represents an interface type.

Usage

From source file:net.ymate.platform.commons.util.ClassUtils.java

/**
 * ? Jar .class??'$'??Java"com/ymatesoft/common/A.class"~"com.ymatesoft.common.A"
 * /*from  www.ja va 2  s .  co  m*/
 * @param collections
 * @param clazz
 * @param packageName
 * @param jarFile
 * @param callingClass
 */
@SuppressWarnings("unchecked")
protected static <T> void __doFindClassByJar(Collection<Class<T>> collections, Class<T> clazz,
        String packageName, JarFile jarFile, Class<?> callingClass) {
    Enumeration<JarEntry> _entriesEnum = jarFile.entries();
    for (; _entriesEnum.hasMoreElements();) {
        JarEntry _entry = _entriesEnum.nextElement();
        // ??? '/'  '.'?.class???'$'??
        String _className = _entry.getName().replaceAll("/", ".");
        if (_className.endsWith(".class") && _className.indexOf('$') < 0) {
            if (_className.startsWith(packageName)) {
                Class<?> _class = null;
                try {
                    _class = ResourceUtils.loadClass(_className.substring(0, _className.lastIndexOf('.')),
                            callingClass);
                    if (_class != null) {
                        if (clazz.isAnnotation()) {
                            if (isAnnotationOf(_class, (Class<Annotation>) clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (clazz.isInterface()) {
                            if (isInterfaceOf(_class, clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (isSubclassOf(_class, clazz)) {
                            collections.add((Class<T>) _class);
                        }
                    }
                } catch (NoClassDefFoundError e) {
                    _LOG.warn("", RuntimeUtils.unwrapThrow(e));
                } catch (ClassNotFoundException e) {
                    _LOG.warn("", RuntimeUtils.unwrapThrow(e));
                }
            }
        }
    }
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private Map toMap(Class<?> type, BasicDBList value) {

    try {// w ww. jav a 2 s .  co  m
        Map map;

        if (!type.isInterface()) {
            map = (Map) repository.getConstructor(type).newInstance();
        } else {
            map = (Map) repository.getConstructor(getClassFor((String) value.get(0))).newInstance();
        }
        for (int i = 1; i < value.size(); i += 2) {
            Object key = fromDBObject(value.get(i));
            Object val = fromDBObject(value.get(i + 1));

            map.put(key, val);
        }

        return map;

    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    }
}

From source file:de.xwic.appkit.core.dao.AbstractDAO.java

/**
 * @param iClass/*from  ww w. j a  v  a 2s .  c o  m*/
 * @param eClass
 */
public AbstractDAO(Class<I> iClass, Class<E> eClass) {
    if (iClass == null || eClass == null) {
        throw new NullPointerException("No classes provided for dao " + getClass());
    }
    if (!iClass.isInterface()) {
        throw new IllegalStateException("First parameter must be an interface.");
    }
    if (!iClass.isAssignableFrom(eClass)) {
        throw new IllegalStateException("Illegal parameters for " + getClass());
    }
    this.iClass = iClass;
    this.eClass = eClass;
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private Collection toCollection(Class<?> type, BasicDBList value) {

    try {//from  ww  w .j  a va 2s  .  c o m
        Collection collection;
        if (!type.isInterface()) {
            collection = (Collection) repository.getConstructor(type).newInstance();
        } else {
            collection = (Collection) repository.getConstructor(getClassFor((String) value.get(0)))
                    .newInstance();
        }

        for (int i = 1; i < value.size(); i++)
            collection.add(fromDBObject(value.get(i)));

        return collection;
    } catch (InvocationTargetException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (InstantiationException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    } catch (IllegalAccessException e) {
        throw new SpaceMongoException("Could not find default constructor for type: " + type.getName(), e);
    }
}

From source file:com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.java

/**
 * <p>This method 'collects' all the validator configurations for a given
 * action invocation.</p>/*  www .j  a  v  a 2  s .  com*/
 * <p/>
 * <p>It will traverse up the class hierarchy looking for validators for every super class
 * and directly implemented interface of the current action, as well as adding validators for
 * any alias of this invocation. Nifty!</p>
 * <p/>
 * <p>Given the following class structure:
 * <pre>
 *   interface Thing;
 *   interface Animal extends Thing;
 *   interface Quadraped extends Animal;
 *   class AnimalImpl implements Animal;
 *   class QuadrapedImpl extends AnimalImpl implements Quadraped;
 *   class Dog extends QuadrapedImpl;
 * </pre></p>
 * <p/>
 * <p>This method will look for the following config files for Dog:
 * <pre>
 *   Animal
 *   Animal-context
 *   AnimalImpl
 *   AnimalImpl-context
 *   Quadraped
 *   Quadraped-context
 *   QuadrapedImpl
 *   QuadrapedImpl-context
 *   Dog
 *   Dog-context
 * </pre></p>
 * <p/>
 * <p>Note that the validation rules for Thing is never looked for because no class in the
 * hierarchy directly implements Thing.</p>
 *
 * @param clazz     the Class to look up validators for.
 * @param context   the context to use when looking up validators.
 * @param checkFile true if the validation config file should be checked to see if it has been
 *                  updated.
 * @param checked   the set of previously checked class-contexts, null if none have been checked
 * @return a list of validator configs for the given class and context.
 */
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile,
        Set<String> checked) {
    List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();

    if (checked == null) {
        checked = new TreeSet<String>();
    } else if (checked.contains(clazz.getName())) {
        return validatorConfigs;
    }

    if (clazz.isInterface()) {
        Class[] interfaces = clazz.getInterfaces();

        for (Class anInterface : interfaces) {
            validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
        }
    } else {
        if (!clazz.equals(Object.class)) {
            validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
        }
    }

    // look for validators for implemented interfaces
    Class[] interfaces = clazz.getInterfaces();

    for (Class anInterface1 : interfaces) {
        if (checked.contains(anInterface1.getName())) {
            continue;
        }

        validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));

        if (context != null) {
            validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
        }

        checked.add(anInterface1.getName());
    }

    validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));

    if (context != null) {
        validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
    }

    checked.add(clazz.getName());

    return validatorConfigs;
}

From source file:com.opensymphony.xwork2.validator.AnnotationActionValidatorManager1.java

/**
 * <p>//ww w .jav  a  2 s.  c  o  m
 * This method 'collects' all the validator configurations for a given action invocation.
 * </p>
 * <p/>
 * <p>
 * It will traverse up the class hierarchy looking for validators for every super class and directly implemented interface of the current action, as well as adding validators for any alias of this invocation. Nifty!
 * </p>
 * <p/>
 * <p>
 * Given the following class structure:
 * 
 * <pre>
 *   interface Thing;
 *   interface Animal extends Thing;
 *   interface Quadraped extends Animal;
 *   class AnimalImpl implements Animal;
 *   class QuadrapedImpl extends AnimalImpl implements Quadraped;
 *   class Dog extends QuadrapedImpl;
 * </pre>
 * 
 * </p>
 * <p/>
 * <p>
 * This method will look for the following config files for Dog:
 * 
 * <pre>
 *   Animal
 *   Animal-context
 *   AnimalImpl
 *   AnimalImpl-context
 *   Quadraped
 *   Quadraped-context
 *   QuadrapedImpl
 *   QuadrapedImpl-context
 *   Dog
 *   Dog-context
 * </pre>
 * 
 * </p>
 * <p/>
 * <p>
 * Note that the validation rules for Thing is never looked for because no class in the hierarchy directly implements Thing.
 * </p>
 * 
 * @param clazz
 *            the Class to look up validators for.
 * @param context
 *            the context to use when looking up validators.
 * @param checkFile
 *            true if the validation config file should be checked to see if it has been updated.
 * @param checked
 *            the set of previously checked class-contexts, null if none have been checked
 * @return a list of validator configs for the given class and context.
 */
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile,
        Set<String> checked) {
    List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();

    if (checked == null) {
        checked = new TreeSet<String>();
    } else if (checked.contains(clazz.getName())) {
        return validatorConfigs;
    }

    if (clazz.isInterface()) {
        Class[] interfaces = clazz.getInterfaces();

        for (Class anInterface : interfaces) {
            validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
        }
    } else {
        if (!clazz.equals(Object.class)) {
            validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
        }
    }

    // look for validators for implemented interfaces
    Class[] interfaces = clazz.getInterfaces();

    for (Class anInterface1 : interfaces) {
        if (checked.contains(anInterface1.getName())) {
            continue;
        }

        validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));

        if (context != null) {
            validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
        }

        checked.add(anInterface1.getName());
    }

    validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));

    if (context != null) {
        validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
        validatorConfigs.addAll(buildAliasValidatorConfigsJSON(clazz, context, checkFile, fileManager,
                validatorFileCache, validatorFactory, LOG));
    }

    checked.add(clazz.getName());

    return validatorConfigs;
}

From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java

@SuppressWarnings("unchecked")
private <T> T makeInterface(final Object obj, final Class<T> clazz) {
    if (null == clazz || !clazz.isInterface())
        throw new IllegalArgumentException("interface Class expected");

    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
            (proxy, m, args) -> invokeImpl(obj, m.getName(), args));
}

From source file:net.ymate.platform.commons.util.ClassUtils.java

@SuppressWarnings("unchecked")
protected static <T> void __doFindClassByZip(Collection<Class<T>> collections, Class<T> clazz,
        String packageName, URL zipUrl, Class<?> callingClass) {
    ZipInputStream _zipStream = null;
    try {//  ww  w. j  av  a 2 s .  c  om
        String _zipFilePath = zipUrl.toString();
        if (_zipFilePath.indexOf('!') > 0) {
            _zipFilePath = StringUtils.substringBetween(zipUrl.toString(), "zip:", "!");
        } else {
            _zipFilePath = StringUtils.substringAfter(zipUrl.toString(), "zip:");
        }
        _zipStream = new ZipInputStream(new FileInputStream(new File(_zipFilePath)));
        ZipEntry _zipEntry = null;
        while (null != (_zipEntry = _zipStream.getNextEntry())) {
            if (!_zipEntry.isDirectory()) {
                if (_zipEntry.getName().endsWith(".class") && _zipEntry.getName().indexOf('$') < 0) {
                    Class<?> _class = __doProcessEntry(zipUrl, _zipEntry);
                    if (_class != null) {
                        if (clazz.isAnnotation()) {
                            if (isAnnotationOf(_class, (Class<Annotation>) clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (clazz.isInterface()) {
                            if (isInterfaceOf(_class, clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (isSubclassOf(_class, clazz)) {
                            collections.add((Class<T>) _class);
                        }
                    }
                }
            }
            _zipStream.closeEntry();
        }
    } catch (Exception e) {
        _LOG.warn("", RuntimeUtils.unwrapThrow(e));
    } finally {
        if (_zipStream != null) {
            try {
                _zipStream.close();
            } catch (IOException e) {
                _LOG.warn("", RuntimeUtils.unwrapThrow(e));
            }
        }
    }
}

From source file:org.apache.tapestry.spec.LibrarySpecification.java

/**
 *  Checks that an extension conforms to the supplied type constraint.
 * // w  w w .j a va  2 s.c  o m
 *  @throws IllegalArgumentException if the extension fails the check.
 * 
 *  @since 3.0
 *  
 **/

protected void applyTypeConstraint(String name, Object extension, Class typeConstraint, ILocation location) {
    Class extensionClass = extension.getClass();

    // Can you assign an instance of the extension to a variable
    // of type typeContraint legally?

    if (typeConstraint.isAssignableFrom(extensionClass))
        return;

    String key = typeConstraint.isInterface() ? "LibrarySpecification.extension-does-not-implement-interface"
            : "LibrarySpecification.extension-not-a-subclass";

    throw new ApplicationRuntimeException(
            Tapestry.format(key, name, extensionClass.getName(), typeConstraint.getName()), location, null);
}

From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java

/**
 * @return all of the classes x in the given directory (recursively) such
 * that clazz.isAssignableFrom(x).//  w  ww.j a va  2 s .  c o m
 */
private List<Class> getAssignableClasses(File path, Class<TetradSerializable> clazz) {
    if (!path.isDirectory()) {
        throw new IllegalArgumentException("Not a directory: " + path);
    }

    @SuppressWarnings("Convert2Diamond")
    List<Class> classes = new LinkedList<>();
    File[] files = path.listFiles();

    for (File file : files) {
        if (file.isDirectory()) {
            classes.addAll(getAssignableClasses(file, clazz));
        } else {
            String packagePath = file.getPath();
            packagePath = packagePath.replace('\\', '.');
            packagePath = packagePath.replace('/', '.');
            packagePath = packagePath.substring(packagePath.indexOf("edu.cmu"), packagePath.length());
            int index = packagePath.indexOf(".class");

            if (index == -1) {
                continue;
            }

            packagePath = packagePath.substring(0, index);

            try {
                Class _clazz = getClass().getClassLoader().loadClass(packagePath);

                if (clazz.isAssignableFrom(_clazz) && !_clazz.isInterface()) {
                    classes.add(_clazz);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    return classes;
}