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:com.mnt.base.web.action.impl.AbstractActionControllerManager.java

public void setControllers(Collection<ActionController> controllers) {
    if (!CommonUtil.isEmpty(controllers)) {
        for (ActionController ac : controllers) {

            Class<?> acClazz = ac.getController().getClass();

            if (CommonUtil.isEmpty(ac.path())) {
                ACPath acPath = acClazz.getAnnotation(ACPath.class);
                ACWebHandler acWebHandler = acClazz.getAnnotation(ACWebHandler.class);

                if (acWebHandler != null) {
                    ac.setWebHandler(acWebHandler.value());
                }/*  w  w  w .  j a  v a 2 s  .c  om*/

                if (acPath == null && acClazz.getInterfaces().length > 0) {

                    Class<?>[] clazzs = acClazz.getInterfaces();

                    for (Class<?> clazz : clazzs) {
                        acClazz = clazz;
                        acPath = acClazz.getAnnotation(ACPath.class);

                        if (acPath != null) {
                            acWebHandler = acClazz.getAnnotation(ACWebHandler.class);
                            if (acWebHandler != null) {
                                ac.setWebHandler(acWebHandler.value());
                            }

                            break;
                        }
                    }
                }

                if (acPath != null) {
                    log.info(new StringBuilder("Attach the action controller for path: ").append(acPath.value())
                            .append(", implementation class: ")
                            .append(ac.getController().getClass().getName()));
                    actionControllerMap.put(acPath.value(), ac);
                } else {
                    log.error(
                            "Skip the invalid action controller, the path of the action controller need to be specified, class: "
                                    + ac.getClass().getName());
                    continue;
                }
            } else {
                log.info(new StringBuilder("Attach the action controller for path: ").append(ac.path())
                        .append(", implementation class: ").append(ac.getClass().getName()));
                actionControllerMap.put(ac.path(), ac);
            }

            Map<String, MethodInfoHolder> acMs = new HashMap<String, MethodInfoHolder>();
            List<ResourceInfoHolder> acRs = new ArrayList<ResourceInfoHolder>();
            Method[] methods = acClazz.getDeclaredMethods();

            ACMethod acMethod;
            MethodInfoHolder mih;
            ResourceInfoHolder rih;

            for (Method m : methods) {
                acMethod = m.getAnnotation(ACMethod.class);

                if (acMethod != null) {

                    mih = new MethodInfoHolder();
                    mih.method = m;
                    mih.method.setAccessible(true);
                    mih.acMethod = acMethod;
                    mih.paramsMap = new LinkedHashMap<String, Class<?>>();

                    Class<?>[] pts = m.getParameterTypes();

                    if (pts.length > 0) {

                        Annotation[][] pass = m.getParameterAnnotations();

                        Annotation[] pas;

                        ACParam acParam;

                        // skip the first two parameters: parameterMap, responseMap
                        for (int i = 0; i < pass.length; i++) {
                            pas = pass[i];

                            if (pas.length != 1 || !(pas[0] instanceof ACParam)) {
                                log.warn("no corresponding ACParam specified for actioncontroller: "
                                        + acClazz.getName() + " method: " + m.getName()
                                        + " parameter at index: " + i + ".");

                                mih.paramsMap.put(ACParam.NULL_PREFIX + i, pts[i]);
                                continue;
                            }

                            acParam = (ACParam) (pas[0]);

                            if (acParam.beanPrefix()) {
                                String beanFieldIndexKey = ACParam.BEAN_PREFIX + i;
                                mih.paramsMap.put(beanFieldIndexKey, pts[i]);
                                if (mih.beanFieldDefMap == null) {
                                    mih.beanFieldDefMap = new LinkedHashMap<String, Map<String, Field>>();
                                }

                                Class<?> paramClass = pts[i];
                                Field[] fields = paramClass.getDeclaredFields();
                                Map<String, Field> beanFieldDefMap = new LinkedHashMap<String, Field>();
                                for (Field field : fields) {
                                    field.setAccessible(true);
                                    beanFieldDefMap.put(new StringBuilder(acParam.value()).append(".")
                                            .append(field.getName()).toString(), field);
                                }

                                mih.beanFieldDefMap.put(beanFieldIndexKey, beanFieldDefMap);
                            } else {
                                mih.paramsMap.put(acParam.value(), pts[i]);
                            }
                        }
                    }

                    if (!CommonUtil.isEmpty(acMethod.resource())) {
                        String resourceDef = acMethod.resource();
                        rih = mih.new ResourceInfoHolder(resourceDef);
                        if (rih.parse()) {
                            acRs.add(rih);
                        }
                    }

                    acMs.put(acMethod.type(), mih);
                }
            }

            if (!acMs.isEmpty()) {
                acMethodsMap.put(ac, acMs);
            }

            if (!acRs.isEmpty()) {
                acResourcesMap.put(ac, acRs);
            }
        }
    }
}

From source file:org.evosuite.setup.TestClusterGenerator.java

/**
 * Get the set of fields defined in this class and its superclasses
 * //  w ww  .j av  a2  s.  com
 * @param clazz
 * @return
 */
public static Set<Field> getFields(Class<?> clazz) {
    // TODO: Helper not necessary here!
    Map<String, Field> helper = new TreeMap<String, Field>();

    Set<Field> fields = new LinkedHashSet<Field>();
    if (clazz.getSuperclass() != null) {
        for (Field f : getFields(clazz.getSuperclass())) {
            helper.put(f.toGenericString(), f);
        }

    }
    for (Class<?> in : clazz.getInterfaces()) {
        for (Field f : getFields(in)) {
            helper.put(f.toGenericString(), f);
        }
    }

    try {
        for (Field f : clazz.getDeclaredFields()) {
            helper.put(f.toGenericString(), f);
        }
    } catch (NoClassDefFoundError e) {
        // TODO: What shall we do?
        logger.info("Error while trying to load fields of class {}: {}", clazz.getName(), e);
    }
    fields.addAll(helper.values());

    return fields;
}

From source file:org.apache.beehive.controls.runtime.bean.ControlBean.java

/**
 * Finds all of the EventSets extended by the input EventSet, and adds them to
 * the provided list. /* www.ja v a2 s  . c  om*/
 * @param eventSet
 * @param superEventSets
 */
private void getSuperEventSets(Class eventSet, List<Class> superEventSets) {
    Class[] superInterfaces = eventSet.getInterfaces();
    if (superInterfaces != null) {
        for (int i = 0; i < superInterfaces.length; i++) {
            Class superInterface = superInterfaces[i];
            if (superInterface.isAnnotationPresent(EventSet.class)) {
                superEventSets.add(superInterface);

                // Continue traversing up the EventSet inheritance hierarchy
                getSuperEventSets(superInterface, superEventSets);
            }
        }
    }
}

From source file:org.apache.stratos.metadata.service.handlers.StratosAuthorizingHandler.java

/**
 * Goes through the class hierarchy and figure out the supertenant
 * annotations coupled with operations/methods.
 *
 * @param clazz//w w w. j a v  a2 s  .  co  m
 * @param superTenantServiceSet
 */
private void findSuperTenantServices(Class<?> clazz, Set<String> superTenantServiceSet) {
    if (clazz == null || clazz == Object.class) {
        return;
    }
    for (Method m : clazz.getMethods()) {
        if (SKIP_METHODS.contains(m.getName())) {
            continue;
        }
        boolean isSuperTenantService = getSuperTenantServices(m.getAnnotations(), TENANT_ANNOTATION_CLASS_NAME);
        if (isSuperTenantService) {
            superTenantServiceSet.add(m.getName());
        }
    }
    if (!superTenantServiceSet.isEmpty()) {
        return;
    }

    findSuperTenantServices(clazz.getSuperclass(), superTenantServiceSet);

    if (!superTenantServiceSet.isEmpty()) {
        return;
    }

    for (Class<?> interfaceCls : clazz.getInterfaces()) {
        findSuperTenantServices(interfaceCls, superTenantServiceSet);
    }
}

From source file:hu.netmind.beankeeper.modification.impl.ModificationTrackerImpl.java

private void modifyClassEntries(Class currentClass, ModificationEntry entry, boolean add) {
    if (currentClass == null)
        return;/*from  w w  w  .  java2s  . c  o  m*/
    // Do current class
    Set entries = (Set) entriesByClass.get(currentClass);
    if (entries == null) {
        entries = new HashSet();
        entriesByClass.put(currentClass, entries);
    }
    if (add)
        entries.add(entry);
    else
        entries.remove(entry);
    if (entries.size() == 0)
        entriesByClass.remove(currentClass);
    // Do superclass
    modifyClassEntries(currentClass.getSuperclass(), entry, add);
    // Do interfaces
    Class interfaces[] = currentClass.getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
        modifyClassEntries(interfaces[i], entry, add);
}

From source file:org.apache.flink.api.java.typeutils.TypeExtractor.java

private static boolean hasHadoopWritableInterface(Class<?> clazz, HashSet<Class<?>> alreadySeen) {
    Class<?>[] interfaces = clazz.getInterfaces();
    for (Class<?> c : interfaces) {
        if (c.getName().equals("org.apache.hadoop.io.Writable")) {
            return true;
        } else if (alreadySeen.add(c) && hasHadoopWritableInterface(c, alreadySeen)) {
            return true;
        }/*www  .ja v  a  2s  . c o  m*/
    }

    Class<?> superclass = clazz.getSuperclass();
    return superclass != null && alreadySeen.add(superclass)
            && hasHadoopWritableInterface(superclass, alreadySeen);
}

From source file:com.nick.scalpel.core.opt.BeanFactory.java

private void readPrebuilt(Context context, int res) {
    new AbsBeanXmlParser(context) {
        @Override/*from  www .ja v a2s.c o  m*/
        protected void onCreateBeanItem(BeanItem item) {
            super.onCreateBeanItem(item);
            synchronized (mBeanMap) {
                if (!mBeanMap.containsKey(item)) {
                    Object bean = createBean(item.clz);
                    logV("Created prebuilt bean:" + bean + ", for:" + item);
                    if (bean != null) {
                        mBeanMap.put(item, bean);
                        // Find supers
                        // cacheForSuper(bean.getClass(), bean, item);
                        cacheForInterface(bean.getClass(), bean, item);
                    }
                }
            }
        }

        void cacheForSuper(Class clz, Object bean, BeanItem createdItem) {
            Class superClz = clz.getSuperclass();
            if (superClz == null)
                return;
            if (superClz == Object.class)
                return;
            BeanItem item = new BeanItem(createdItem.id, createdItem.name, superClz.getName());
            logV("Caching for super clz:" + item);
            if (!mBeanMap.containsKey(item))
                mBeanMap.put(item, bean);
            else {
                throw new IllegalStateException("Found multiple class beans for:" + clz);
            }
            cacheForSuper(superClz, bean, item);
        }

        void cacheForInterface(Class clz, Object bean, BeanItem createdItem) {
            Class[] interfaces = clz.getInterfaces();
            if (interfaces == null)
                return;
            for (Class iface : interfaces) {
                BeanItem item = new BeanItem(createdItem.id, createdItem.name, iface.getName());
                logV("Caching for iface:" + item);
                if (!mBeanMap.containsKey(item))
                    mBeanMap.put(item, bean);
                else {
                    logE("Found multiple interface beans for:" + mBeanMap.get(item));
                }
            }
        }
    }.parse(res);
}

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

private void genTest(String basedPath, String className) {
    //  //w w w  .  j  a v a2 s.  c  o  m
    this.getLog().info("" + className + "");
    Map<String, Integer> methodCnt = new HashMap<String, Integer>();
    boolean hasmethod = false;
    try {

        Class cls = cl.loadClass(className);

        String testJFileName = cls.getSimpleName() + "Test.java";
        String pkgPath = cls.getPackage().getName().replace(".", File.separator);

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

        int len = 0;
        Class[] inters = cls.getInterfaces();
        if (inters == null || (len = inters.length) == 0) {
            return;
        }

        /**
         * package import
         */
        StringBuffer jHeadBuf = createTestJHeadByClass(cls);

        StringBuffer testJBuf = new StringBuffer();

        testJBuf.append(jHeadBuf);
        Map<String, String> methodDefs = new HashMap<String, String>();
        for (int j = 0; j < len; j++) {
            /**
             * ?
             */
            Class interCls = inters[j];

            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) {

                this.getLog().info("?" + className + "Test?");

                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);
                    }
                }

                testJBuf.append(methodBuf);
            } else {
                this.getLog().info(className + "");
            }
        }

        String testJFile = testJBuf.append("}").toString();
        if (hasmethod) {
            write(testJFilePath, testJFileName, testJFile);
        }

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

}

From source file:org.broadinstitute.gatk.utils.help.GenericDocumentationHandler.java

/**
 * Utility function that checks which parallelism options are available for an instance of class c.
 *
 * @param myClass the class to query for the interfaces
 * @param parallelOptions an empty HashSet in which to collect the info
 * @return a hash set of parallelism options, otherwise an empty set
 *//*from  www. j  ava  2s  . co  m*/
private HashSet<HashMap<String, Object>> getParallelism(Class myClass,
        HashSet<HashMap<String, Object>> parallelOptions) {
    //
    // Retrieve interfaces
    Class[] implementedInterfaces = myClass.getInterfaces();
    for (Class intfClass : implementedInterfaces) {
        final HashMap<String, Object> nugget = new HashMap<String, Object>();
        if (intfClass.getSimpleName().equals("TreeReducible")) {
            nugget.put("name", intfClass.getSimpleName());
            nugget.put("arg", HelpConstants.ARG_TREEREDUCIBLE);
            nugget.put("link", HelpConstants.CMDLINE_GATK_URL + "#" + HelpConstants.ARG_TREEREDUCIBLE);
        } else if (intfClass.getSimpleName().equals("NanoSchedulable")) {
            nugget.put("name", intfClass.getSimpleName());
            nugget.put("arg", HelpConstants.ARG_NANOSCHEDULABLE);
            nugget.put("link", HelpConstants.CMDLINE_GATK_URL + "#" + HelpConstants.ARG_NANOSCHEDULABLE);
        } else {
            continue;
        }
        parallelOptions.add(nugget);
    }
    // Look up superclasses recursively
    final Class mySuperClass = myClass.getSuperclass();
    if (mySuperClass.getSimpleName().equals("Object")) {
        return parallelOptions;
    }
    return getParallelism(mySuperClass, parallelOptions);
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @return all declared {@link Method}'s, including protected and private.
 *//*w  w  w.  java 2s.com*/
public static Map<String, Method> getMethods(Class<?> clazz) {
    Map<String, Method> methods = Maps.newHashMap();
    // process classes
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        for (Method method : c.getDeclaredMethods()) {
            String signature = getMethodSignature(method);
            if (!methods.containsKey(signature)) {
                method.setAccessible(true);
                methods.put(signature, method);
            }
        }
    }
    // process interfaces
    for (Class<?> interfaceClass : clazz.getInterfaces()) {
        for (Method method : interfaceClass.getDeclaredMethods()) {
            String signature = getMethodSignature(method);
            if (!methods.containsKey(signature)) {
                method.setAccessible(true);
                methods.put(signature, method);
            }
        }
    }
    // done
    return methods;
}