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.femtoparsec.jnlmin.utils.ReflectUtils.java

public static int findClassDistance(Class<?> lower, Class<?> upper) {
    lower = boxClass(lower);/*from   ww w  .  jav a  2  s. co m*/
    upper = boxClass(upper);

    if (lower == upper) {
        return 0;
    }

    if (Number.class.isAssignableFrom(lower) && Number.class.isAssignableFrom(upper)) {
        Integer lowerDistance = NUMBER_DISTANCE_MAP.get(lower.asSubclass(Number.class));
        Integer upperDistance = NUMBER_DISTANCE_MAP.get(upper.asSubclass(Number.class));
        if (lowerDistance != null && upperDistance != null) {
            if (lowerDistance > upperDistance) {
                return -1;
            }
            return upperDistance - lowerDistance;
        }
        //lower or upper is a custom Number (like BigInteger)
        //handle it as normal classes
    }

    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    if (upper.isInterface()) {
        if (lower.isInterface()) {
            return findIIDistance(lower, upper);
        } else {
            return findCIDistance(lower, upper);
        }
    } else {
        assert !lower.isInterface();
        return findCCDistance(lower, upper);
    }

}

From source file:com.laidians.utils.ClassUtils.java

/**
 * ?<br/>//from ww  w  . ja  v  a  2  s  . com
 * Return all interfaces that the given class implements as Set,
 * including ones implemented by superclasses.
 * <p>If the class itself is an interface, it gets returned as sole interface.
 * @param clazz the class to analyze for interfaces
 * @param classLoader the ClassLoader that the interfaces need to be visible in
 * (may be <code>null</code> when accepting all declared interfaces)
 * @return all interfaces that the given object implements as Set
 */
public static Set<Class> getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader) {
    Assert.notNull(clazz, "Class must not be null");
    if (clazz.isInterface() && isVisible(clazz, classLoader)) {
        return Collections.singleton(clazz);
    }
    Set<Class> interfaces = new LinkedHashSet<Class>();
    while (clazz != null) {
        Class<?>[] ifcs = clazz.getInterfaces();
        for (Class<?> ifc : ifcs) {
            interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader));
        }
        clazz = clazz.getSuperclass();
    }
    return interfaces;
}

From source file:org.gradle.model.internal.manage.schema.ModelSchemaExtractor.java

public <T> void validateType(Class<T> type) {
    if (!type.isInterface()) {
        throw invalid(type, "must be defined as an interface");
    }//w  w  w  .ja  v  a  2s.c o m

    if (type.getInterfaces().length != 0) {
        throw invalid(type, "cannot extend other types");
    }

    if (type.getTypeParameters().length != 0) {
        throw invalid(type, "cannot be a parameterized type");
    }
}

From source file:org.cruxframework.crux.tools.servicemap.ServiceMapper.java

/**
 * Generates Remote Service map//from w w w. j a v  a  2  s .c  o m
 */
public void generateServicesMap() {
    try {
        File metaInfFile = getMetaInfFile();
        File serviceMapFile = new File(metaInfFile, "crux-remote");
        if (serviceMapFile.exists() && !isOverride()) {
            logger.info("Service map already exists. Skipping generation...");
            return;
        }
        initializeScannerURLs();
        Set<String> searchClassesByInterface = ClassScanner.searchClassesByInterface(RemoteService.class);
        Properties cruxRemote = new Properties();

        for (String serviceClass : searchClassesByInterface) {
            Class<?> clazz = Class.forName(serviceClass);
            if (clazz.isInterface()) {
                Class<?> service = Services.getService(serviceClass);
                if (service != null) {
                    cruxRemote.put(serviceClass, service.getCanonicalName());
                }
            }
        }
        if (metaInfFile.exists()) {
            if (!metaInfFile.isDirectory()) {
                throw new ServiceMapperException(
                        "Can not create a META-INF directory on " + getProjectDir().getCanonicalPath());
            }
        } else {
            metaInfFile.mkdirs();
        }
        cruxRemote.store(new FileOutputStream(serviceMapFile), "Crux RemoteServices implementations");
    } catch (IOException e) {
        throw new ServiceMapperException("Error creating remote service map", e);
    } catch (ClassNotFoundException e) {
        throw new ServiceMapperException("Error creating remote service map", e);
    }
}

From source file:com.google.code.guice.repository.spi.CustomRepositoryImplementationResolver.java

public Class resolve(Class<? extends Repository> repositoryClass) {
    Assert.notNull(repositoryClass);/* www  . j a v a2s.  c o m*/

    Class customRepository = null;
    /**
     * Detect only custom repository/enhancements interfaces - skip all from Spring and guice-repository project
     */
    Collection<Class<?>> superTypes = ReflectionUtils.getAllSuperTypes(repositoryClass, new Predicate<Class>() {
        @Override
        public boolean apply(Class input) {
            return isValidCustomInterface(input);
        }
    });

    /**
     * Searching for custom repository/enhancement implementation
     */
    if (!superTypes.isEmpty()) {
        Class customRepositoryClass = superTypes.iterator().next();
        Reflections reflections = new Reflections(customRepositoryClass.getPackage().getName());
        Iterable<Class> subTypesOf = reflections.getSubTypesOf(customRepositoryClass);
        for (Class aClass : subTypesOf) {
            if (!aClass.isInterface()) {
                customRepository = aClass;
                logger.info(String.format("Found custom repository implementation: [%s] -> [%s]",
                        repositoryClass.getName(), customRepository.getName()));
                break;
            }
        }
    }

    return customRepository;
}

From source file:it.cilea.osd.common.dao.impl.NamedQueryIntroductionAdvisor.java

public NamedQueryIntroductionAdvisor() {
    super(new IntroductionInterceptor() {
        /**//from ww w.j a  v a  2 s. c  o m
         * Execute the appropriate method of the genericDAO implementation
         * basing on the start characters of the original invoked method on
         * the DAO class
         */
        public Object invoke(MethodInvocation mi) throws Throwable {
            NamedQueryExecutor genericDao = (NamedQueryExecutor) mi.getThis();
            String methodName = mi.getMethod().getName();
            if (methodName.startsWith("find")) {
                Object[] args = mi.getArguments();
                return genericDao.executeFinder(mi.getMethod(), args);
            } else if (methodName.startsWith("unique")) {
                Object[] args = mi.getArguments();
                return genericDao.executeUnique(mi.getMethod(), args);
            } else if (methodName.startsWith("count")) {
                Object[] args = mi.getArguments();
                return genericDao.executeCounter(mi.getMethod(), args);
            } else if (methodName.startsWith("delete") && !methodName.equals("delete")) {
                Object[] args = mi.getArguments();
                return genericDao.executeDelete(mi.getMethod(), args);
            } else if (methodName.startsWith("idFind")) {
                Object[] args = mi.getArguments();
                return genericDao.executeIdFinder(mi.getMethod(), args);
            } else if (methodName.startsWith("paginate")) {
                Object[] args = mi.getArguments();
                String sort = (String) args[args.length - 4];
                boolean inverse = (Boolean) args[args.length - 3];
                int firstResult = (Integer) args[args.length - 2];
                int maxResults = (Integer) args[args.length - 1];
                args = Arrays.asList(args).subList(0, args.length - 4).toArray();
                return genericDao.executePaginator(mi.getMethod(), args, sort, inverse, firstResult,
                        maxResults);
            } else if (methodName.startsWith("is") || methodName.startsWith("has")
                    || methodName.startsWith("check")) {
                Object[] args = mi.getArguments();
                return genericDao.executeBoolean(mi.getMethod(), args);
            } else if (methodName.startsWith("sum")) {
                Object[] args = mi.getArguments();
                return genericDao.executeDouble(mi.getMethod(), args);
            } else if (methodName.startsWith("singleResult")) {
                Object[] args = mi.getArguments();
                return genericDao.executeSingleResult(mi.getMethod(), args);
            } else if (methodName.startsWith("max")) {
                Object[] args = mi.getArguments();
                return genericDao.executeMax(mi.getMethod(), args);
            } else {
                return mi.proceed();
            }
        }

        public boolean implementsInterface(Class intf) {
            return intf.isInterface() && NamedQueryExecutor.class.isAssignableFrom(intf);
        }
    });
}

From source file:net.kaczmarzyk.spring.data.jpa.web.AnnotatedSpecInterfaceArgumentResolver.java

@Override
public boolean supportsParameter(MethodParameter parameter) {
    Class<?> paramType = parameter.getParameterType();
    return paramType.isInterface() && Specification.class.isAssignableFrom(paramType) && isAnnotated(paramType);
}

From source file:com.github.philippn.springremotingautoconfigure.server.annotation.HttpInvokerServiceExporterRegistrar.java

private void setupExport(Class<?> clazz, String beanName, BeanDefinitionRegistry registry) {
    Assert.isTrue(clazz.isInterface(), "Annotation @RemoteExport may only be used on interfaces");

    BeanDefinitionBuilder builder = BeanDefinitionBuilder
            .genericBeanDefinition(HttpInvokerServiceExporter.class).addPropertyReference("service", beanName)
            .addPropertyValue("serviceInterface", clazz);

    String mappingPath = getMappingPath(clazz);

    registry.registerBeanDefinition(mappingPath, builder.getBeanDefinition());

    logger.info(// w  w w  .  j  a  va 2 s  . c  om
            "Mapping HttpInvokerServiceExporter for " + clazz.getSimpleName() + " to [" + mappingPath + "]");
}

From source file:org.apache.camel.blueprint.PackageScanRouteBuilderFinder.java

/**
 * Returns true if the object is non-abstract and supports a zero argument constructor
 *//*from w  ww.ja va  2 s.co m*/
protected boolean isValidClass(Class type) {
    if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
        return true;
    }
    return false;
}

From source file:org.arrow.data.Neo4JDataConfiguration.java

/**
 * {@inheritDoc}/*w ww  .  jav  a2  s.  com*/
 */
@Override
public Neo4jTemplate neo4jTemplate() throws Exception {
    return new Neo4jTemplate(mappingInfrastructure().getObject()) {

        @SuppressWarnings("unchecked")
        @Override
        public <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type,
                MappingPolicy mappingPolicy) {

            if (type.isInterface() && type.isAssignableFrom(ProcessSpecification.class)) {
                return (T) super.createEntityFromState(state, Process.class, mappingPolicy);
            }
            return super.createEntityFromState(state, type, mappingPolicy);
        }

    };
}