Example usage for java.lang Class getInterfaces

List of usage examples for java.lang Class getInterfaces

Introduction

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

Prototype

public Class<?>[] getInterfaces() 

Source Link

Document

Returns the interfaces directly implemented by the class or interface represented by this object.

Usage

From source file:org.jnode.driver.Device.java

/**
 * Add an API implementation to the list of API's implemented by this device.
 *
 * @param apiInterface/*  w  w  w  . j a  v  a  2s.c o m*/
 * @param apiImplementation
 */
@SuppressWarnings("unchecked")
public final <T extends DeviceAPI> void registerAPI(Class<T> apiInterface, T apiImplementation) {
    if (!apiInterface.isInstance(apiImplementation)) {
        throw new IllegalArgumentException("API implementation does not implement API interface");
    }
    if (!apiInterface.isInterface()) {
        throw new IllegalArgumentException("API interface must be an interface");
    }
    apis.put(apiInterface, apiImplementation);
    final Class[] interfaces = apiInterface.getInterfaces();
    if (interfaces != null) {
        for (Class intf : interfaces) {
            if (DeviceAPI.class.isAssignableFrom(intf)) {
                if (!apis.containsKey(intf)) {
                    apis.put((Class<? extends DeviceAPI>) intf, apiImplementation);
                }
            }
        }
    }
}

From source file:com.tower.service.test.SoafwTesterMojo.java

/**
 * //  w  w  w.j  a  v  a2s  .  c  o  m
 * @param className
 *            
 */
private void appendTest(String className) {
    /**
     * ?? ?public
     * ?MojoExecutionException??????
     */
    try {

        Map<String, Integer> methodCnt = new HashMap<String, Integer>();
        boolean hasmethod = false;

        Map<String, String> methodDefs = new HashMap<String, String>();
        Class cls = cl.loadClass(className);// 
        Class[] inters = cls.getInterfaces();
        int len = 0;
        if (inters == null || (len = inters.length) == 0) {
            return;
        }
        for (int i = 0; i < len; i++) {

            Class interCls = inters[i];

            this.getLog().info("@interface: " + interCls.getName());

            String name = project.getName();

            Method[] methods = null;

            if (name.endsWith("-dao")) {
                methods = interCls.getDeclaredMethods();
            } else {
                methods = interCls.getMethods();
            }

            int mlen = 0;
            if (methods != null && (mlen = methods.length) > 0) {

                StringBuffer methodBuf = new StringBuffer();

                for (int m = 0; m < mlen; m++) {
                    Method method = methods[m];
                    int modf = method.getModifiers();
                    if (modf == 1025) {// ??
                        hasmethod = true;
                        /**
                         * ??????Test ???=??+Test
                         * ??:basedPath+File.separator
                         * +src+File.separator+test+File.separator
                         * +pkg+definesArray[i]+Test+.java
                         */
                        if (methodCnt.containsKey(method.getName())) {
                            methodCnt.put(method.getName(), methodCnt.get(method.getName()) + 1);
                        } else {
                            methodCnt.put(method.getName(), 0);
                        }
                        int cnt = methodCnt.get(method.getName());

                        addMethod(methodDefs, methodBuf, method, cnt);
                    }
                }
            }
        }

        Class tstCls = cl.loadClass(className + "Test");// 

        Method[] methods = tstCls.getDeclaredMethods();
        len = methods == null ? 0 : methods.length;
        this.getLog().info("" + tstCls.getSimpleName() + "?" + len);

        /**
         * ??public
         */
        for (int m = 0; m < len; m++) {

            Method method = methods[m];
            SoaFwTest test = method.getAnnotation(SoaFwTest.class);
            if (test == null) {
                this.getLog()
                        .info(tstCls.getSimpleName() + " method " + method.getName() + "SoaFwTest");
                continue;
            }

            String id = test.id();

            if (methodDefs.containsKey(id)) {
                methodDefs.remove(id);
            }
        }

        if ((len = methodDefs.size()) == 0) {
            return;
        }

        String[] methodImpls = new String[len];
        methodDefs.keySet().toArray(methodImpls);
        // TODO  ???

        StringBuilder src = new StringBuilder();

        String srcs = readTestSrc(className);

        int index = srcs.lastIndexOf("}");

        // this.getLog().info(srcs);
        //this.getLog().info("lastIndexOf(}):" + index);

        String impls = srcs.substring(0, index - 1);

        src.append(impls);

        src.append("\n");
        this.getLog().info("?: " + className + "Test");
        StringBuilder appends = new StringBuilder();
        this.getLog().info("?");
        for (int i = 0; i < len; i++) {
            String methodId = methodImpls[i];
            String method = methodDefs.get(methodId);
            appends.append(method);
            appends.append("\n");
        }

        src.append(appends.toString());

        src.append("}");

        Package pkg = tstCls.getPackage();
        String pkgName = pkg.getName();
        String pkgPath = pkgName.replace(".", File.separator);

        String testBaseSrcPath = basedPath + File.separator + "src" + File.separator + "test" + File.separator
                + "java";
        String testSrcFullPath = testBaseSrcPath + File.separator + pkgPath;

        write(testSrcFullPath, tstCls.getSimpleName() + ".java", src.toString());

    } catch (Exception e) {
        this.getLog().error(e);
    } catch (Error er) {
        this.getLog().error(er);
    }
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns as {@link Set} of assignable types which are implemented by the given type. This includes inherited types if the
 * respective parameter is set to true. The given type is returned within the result {@link Set} if it is compliant to the
 * onlyReturnInterfaces flag./*  w  ww  .  j  a  va 2 s .  c o m*/
 * 
 * @param type
 * @param inherited
 * @param onlyReturnInterfaces
 * @return
 */
public static Set<Class<?>> assignableTypeSet(Class<?> type, boolean inherited, boolean onlyReturnInterfaces) {
    //    
    final Set<Class<?>> retset = new LinkedHashSet<Class<?>>();

    //
    if (type != null) {
        //
        final Set<Class<?>> remainingTypeSet = new LinkedHashSet<Class<?>>();

        //
        class Helper {
            public void addNewInterfaceTypes(Class<?> type) {
                Class<?>[] interfaces = type.getInterfaces();
                if (interfaces != null) {
                    for (Class<?> interfaceType : interfaces) {
                        if (!retset.contains(interfaceType)) {
                            remainingTypeSet.add(interfaceType);
                        }
                    }
                }
            }
        }

        //
        if (!onlyReturnInterfaces || type.isInterface()) {
            retset.add(type);
        }

        //
        if (inherited) {
            remainingTypeSet.addAll(supertypeSet(type));
        }

        //
        Helper helper = new Helper();
        helper.addNewInterfaceTypes(type);

        //
        while (!remainingTypeSet.isEmpty()) {
            //
            Iterator<Class<?>> iterator = remainingTypeSet.iterator();
            Class<?> remainingType = iterator.next();
            iterator.remove();

            //
            if (!onlyReturnInterfaces || remainingType.isInterface()) {
                retset.add(remainingType);
            }

            //
            if (inherited) {
                //
                helper.addNewInterfaceTypes(remainingType);
            }

        }
    }

    //
    return retset;
}

From source file:com.kjt.service.common.SoafwTesterMojo.java

/**
 * //from   ww  w .j  av a2s  .com
 * @param className 
 */
private void appendTest(String className) {
    /**
     * ?? ?public
     * ?MojoExecutionException??????
     */
    try {

        Map<String, Integer> methodCnt = new HashMap<String, Integer>();
        boolean hasmethod = false;

        Map<String, String> methodDefs = new HashMap<String, String>();
        Class cls = cl.loadClass(className);// 
        Class[] inters = cls.getInterfaces();
        int len = 0;
        if (inters == null || (len = inters.length) == 0) {
            return;
        }
        for (int i = 0; i < len; i++) {

            Class interCls = inters[i];

            this.getLog().info("@interface: " + interCls.getName());

            String name = project.getName();

            Method[] methods = null;

            if (name.endsWith("-dao")) {
                methods = interCls.getDeclaredMethods();
            } else {
                methods = interCls.getMethods();
            }

            int mlen = 0;
            if (methods != null && (mlen = methods.length) > 0) {

                StringBuffer methodBuf = new StringBuffer();

                for (int m = 0; m < mlen; m++) {
                    Method method = methods[m];
                    int modf = method.getModifiers();
                    if (modf == 1025) {// ??
                        hasmethod = true;
                        /**
                         * ??????Test ???=??+Test
                         * ??:basedPath+File.separator
                         * +src+File.separator+test+File.separator
                         * +pkg+definesArray[i]+Test+.java
                         */
                        if (methodCnt.containsKey(method.getName())) {
                            methodCnt.put(method.getName(), methodCnt.get(method.getName()) + 1);
                        } else {
                            methodCnt.put(method.getName(), 0);
                        }
                        int cnt = methodCnt.get(method.getName());

                        addMethod(methodDefs, methodBuf, method, cnt);
                    }
                }
            }
        }

        Class tstCls = cl.loadClass(className + "Test");// 

        Method[] methods = tstCls.getDeclaredMethods();
        len = methods == null ? 0 : methods.length;
        this.getLog().info("" + tstCls.getSimpleName() + "?" + len);

        /**
         * ??public
         */
        for (int m = 0; m < len; m++) {

            Method method = methods[m];
            SoaFwTest test = method.getAnnotation(SoaFwTest.class);
            if (test == null) {
                this.getLog()
                        .info(tstCls.getSimpleName() + " method " + method.getName() + "SoaFwTest");
                continue;
            }

            String id = test.id();

            if (methodDefs.containsKey(id)) {
                methodDefs.remove(id);
            }
        }

        if ((len = methodDefs.size()) == 0) {
            return;
        }

        String[] methodImpls = new String[len];
        methodDefs.keySet().toArray(methodImpls);
        // TODO  ???
        this.getLog().info("???");

        StringBuilder src = new StringBuilder();

        String srcs = readTestSrc(className);

        int index = srcs.lastIndexOf("}");

        this.getLog().info(srcs);
        this.getLog().info("lastIndexOf(}):" + index);
        String impls = srcs.substring(0, index - 1);

        src.append(impls);

        src.append("\n");

        StringBuilder appends = new StringBuilder();
        this.getLog().info("?");
        for (int i = 0; i < len; i++) {
            String methodId = methodImpls[i];
            String method = methodDefs.get(methodId);
            appends.append(method);
            appends.append("\n");
        }

        src.append(appends.toString());

        src.append("}");

        Package pkg = tstCls.getPackage();
        String pkgName = pkg.getName();
        String pkgPath = pkgName.replace(".", File.separator);

        String testBaseSrcPath = basedPath + File.separator + "src" + File.separator + "test" + File.separator
                + "java";
        String testSrcFullPath = testBaseSrcPath + File.separator + pkgPath;

        write(testSrcFullPath, tstCls.getSimpleName() + ".java", src.toString());

    } catch (Exception e) {
        this.getLog().error(e);
    } catch (Error er) {
        this.getLog().error(er);
    }
}

From source file:com.agiletec.aps.system.common.entity.ApsEntityManager.java

/**
 * This method is used and hereby found in the spring XML configuration of the service.
 * By default, the declaration of the abstract service in the Spring configuration presents a 
 * standard class (ApsEntity); this class must be substituted in the definition of the service
 * if a different class, which extends the standard ApsEntity, must be used.
 * This method checks the validity of the class.
 * @param className The name of the entity class.
 *//*from  w  ww. j a v a 2s .  c  om*/
public void setEntityClassName(String className) {
    try {
        this._entityClass = Class.forName(className);
        Class check = this._entityClass;
        do {
            Class[] interfaces = check.getInterfaces();
            for (int j = 0; j < interfaces.length; j++) {
                if (interfaces[j].equals(IApsEntity.class))
                    return;
            }
            check = check.getSuperclass();
        } while (!check.equals(Object.class));
        throw new RuntimeException("Invalid entity class");
    } catch (ClassNotFoundException e) {
        _logger.error("Errore creating the entity class", e);
        //ApsSystemUtils.logThrowable(e, this, "setEntityClassName","Error detected while creating the entity class");
        throw new RuntimeException("Error creating the entity class", e);
    }
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

private static Class[] getInterfaces_priv(final Class cls) {
    Class[] intferfaces = null;// w ww.  j a va 2 s  .co m
    if (cls == null) {
        return null;
    }
    try {
        intferfaces = (Class[]) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return cls.getInterfaces();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
            log.debug("Exception is ignored.");
        }
    }
    return intferfaces;
}

From source file:org.apache.nifi.controller.service.StandardControllerServiceProvider.java

private void populateInterfaces(final Class<?> cls, final List<Class<?>> interfacesDefinedThusFar) {
    final Class<?>[] ifc = cls.getInterfaces();
    if (ifc != null && ifc.length > 0) {
        for (final Class<?> i : ifc) {
            interfacesDefinedThusFar.add(i);
        }/*from ww w . j a  v a2s  .c o m*/
    }

    final Class<?> superClass = cls.getSuperclass();
    if (superClass != null) {
        populateInterfaces(superClass, interfacesDefinedThusFar);
    }
}

From source file:message.mybatis.common.mapper.MapperHelper.java

/**
 * Mapper?/*w  w w .j  a  va  2  s .c om*/
 *
 * @param mapperClass
 * @throws Exception
 */
public void registerMapper(Class<?> mapperClass) {
    if (!registerMapper.containsKey(mapperClass)) {
        registerMapper.put(mapperClass, fromMapperClass(mapperClass));
    }
    //?
    Class<?>[] interfaces = mapperClass.getInterfaces();
    if (interfaces != null && interfaces.length > 0) {
        for (Class<?> anInterface : interfaces) {
            registerMapper(anInterface);
        }
    }
}

From source file:ome.services.graphs.GraphPathBean.java

/**
 * If the given property of the given class is actually declared by an interface that it implements,
 * find the name of the interface that first declares the property.
 * @param className the name of an {@link IObject} class
 * @param propertyName the name of a property of the class
 * @return the interface declaring the property, or {@code null} if none
 *///from  w w  w.j a  v a2s . c o  m
private Class<? extends IObject> getInterfaceForProperty(String className, String propertyName) {
    Class<? extends IObject> interfaceForProperty = null;
    Set<Class<? extends IObject>> interfacesFrom, interfacesTo;
    try {
        interfacesFrom = ImmutableSet
                .<Class<? extends IObject>>of(Class.forName(className).asSubclass(IObject.class));
    } catch (ClassNotFoundException e) {
        log.error("could not load " + IObject.class.getName() + " subclass " + className);
        return null;
    }
    while (!interfacesFrom.isEmpty()) {
        interfacesTo = new HashSet<Class<? extends IObject>>();
        for (final Class<? extends IObject> interfaceFrom : interfacesFrom) {
            if (interfaceFrom.isInterface()
                    && BeanUtils.getPropertyDescriptor(interfaceFrom, propertyName) != null) {
                interfaceForProperty = interfaceFrom;
            }
            for (final Class<?> newInterface : interfaceFrom.getInterfaces()) {
                if (newInterface != IObject.class && IObject.class.isAssignableFrom(newInterface)) {
                    interfacesTo.add(newInterface.asSubclass(IObject.class));
                    classesBySimpleName.put(newInterface.getSimpleName(),
                            newInterface.asSubclass(IObject.class));
                }
            }
        }
        interfacesFrom = interfacesTo;
    }
    return interfaceForProperty == null ? null : interfaceForProperty;
}

From source file:de.hasait.genesis.base.freemarker.FreemarkerModelWriter.java

static void write(final Configuration pConfiguration, final Writer pWriter, final Object pModel,
        final Map pParams) throws IOException, TemplateException {
    final Map<Class<?>, Template> templateCache = TEMPLATE_CACHE.get();

    Class<?> currentType = pModel.getClass();
    Template template = templateCache.get(currentType);
    if (template == null) {
        final LinkedList<TypeNode> queue = new LinkedList<TypeNode>();
        queue.add(new TypeNode(null, currentType));

        TemplateNotFoundException firstE = null;

        do {//from   w  w w  .  j  a v  a 2 s  .c o  m
            // take first from queue
            TypeNode current = queue.removeFirst();
            currentType = current._type;

            // determine template
            template = templateCache.get(currentType);
            if (template == null) {
                final String templateName = currentType.getSimpleName() + ".ftl";
                try {
                    template = pConfiguration.getTemplate(templateName);
                } catch (final TemplateNotFoundException e) {
                    if (firstE == null) {
                        firstE = e;
                    }
                }
            }

            if (template != null) {
                // fill cache including parents
                templateCache.put(currentType, template);
                while (true) {
                    templateCache.put(currentType, template);
                    current = current._parent;
                    if (current == null) {
                        break;
                    }
                    currentType = current._type;
                }
            } else {
                // fill queue with next nodes
                for (final Class<?> interfaceType : currentType.getInterfaces()) {
                    queue.add(new TypeNode(current, interfaceType));
                }
                final Class<?> superclassType = currentType.getSuperclass();
                if (superclassType != null) {
                    queue.add(new TypeNode(current, superclassType));
                }
            }
        } while (template == null && !queue.isEmpty());

        if (template == null) {
            throw firstE;
        }
    }

    write(pConfiguration, pWriter, template, pModel, pParams);
}