Example usage for org.apache.commons.lang ClassUtils getAllInterfaces

List of usage examples for org.apache.commons.lang ClassUtils getAllInterfaces

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils getAllInterfaces.

Prototype

public static List<Class<?>> getAllInterfaces(Class<?> cls) 

Source Link

Document

Gets a List of all interfaces implemented by the given class and its superclasses.

The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up.

Usage

From source file:net.sf.jasperreports.export.CompositeExporterConfigurationFactory.java

/**
 * //  ww w  .  j  a  v a  2 s  . c o  m
 */
private final C getProxy(Class<?> clazz, InvocationHandler handler) {
    List<Class<?>> allInterfaces = new ArrayList<Class<?>>();

    if (clazz.isInterface()) {
        allInterfaces.add(clazz);
    } else {
        @SuppressWarnings("unchecked")
        List<Class<?>> lcInterfaces = ClassUtils.getAllInterfaces(clazz);
        allInterfaces.addAll(lcInterfaces);
    }

    @SuppressWarnings("unchecked")
    C composite = (C) Proxy.newProxyInstance(ExporterConfiguration.class.getClassLoader(),
            allInterfaces.toArray(new Class<?>[allInterfaces.size()]), handler);

    return composite;
}

From source file:net.sf.maltcms.chromaui.project.spi.nodes.DescriptorNode.java

@Override
public Action[] getActions(boolean context) {
    List<?> interfaces = ClassUtils.getAllInterfaces(getBean().getClass());
    List<?> superClasses = ClassUtils.getAllSuperclasses(getBean().getClass());
    LinkedHashSet<Action> descriptorActions = new LinkedHashSet<>();
    for (Object o : interfaces) {
        Class<?> c = (Class) o;
        descriptorActions.addAll(Utilities.actionsForPath("Actions/DescriptorNodeActions/" + c.getName()));
        descriptorActions/* w w w.  j av a 2s.  c om*/
                .addAll(Utilities.actionsForPath("Actions/DescriptorNodeActions/" + c.getSimpleName()));
    }
    for (Object o : superClasses) {
        Class<?> c = (Class) o;
        descriptorActions.addAll(Utilities.actionsForPath("Actions/DescriptorNodeActions/" + c.getName()));
        descriptorActions
                .addAll(Utilities.actionsForPath("Actions/DescriptorNodeActions/" + c.getSimpleName()));
    }
    descriptorActions.addAll(
            Utilities.actionsForPath("Actions/DescriptorNodeActions/" + getBean().getClass().getName()));
    descriptorActions.addAll(
            Utilities.actionsForPath("Actions/DescriptorNodeActions/" + getBean().getClass().getSimpleName()));
    descriptorActions.add(null);
    descriptorActions.addAll(Utilities.actionsForPath("Actions/DescriptorNodeActions/DefaultActions"));
    descriptorActions.add(SystemAction.get(PropertiesAction.class));
    return descriptorActions.toArray(new Action[descriptorActions.size()]);
}

From source file:jenkins.plugins.git.MethodUtils.java

/**
 * <p>Retrieves a method whether or not it's accessible. If no such method
 * can be found, return {@code null}.</p>
 *
 * @param cls            The class that will be subjected to the method search
 * @param methodName     The method that we wish to call
 * @param parameterTypes Argument class types
 * @return The method/*w  w w .  j av a2  s  .c  o m*/
 */
static Method getMethodImpl(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) {
    Validate.notNull(cls, "Null class not allowed.");
    Validate.notEmpty(methodName, "Null or blank methodName not allowed.");

    // fast path, check if directly declared on the class itself
    for (final Method method : cls.getDeclaredMethods()) {
        if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) {
            return method;
        }
    }
    if (!cls.isInterface()) {
        // ok, now check if directly implemented on a superclass
        // Java 8: note that super-interface implementations trump default methods
        for (Class<?> klass = cls.getSuperclass(); klass != null; klass = klass.getSuperclass()) {
            for (final Method method : klass.getDeclaredMethods()) {
                if (methodName.equals(method.getName())
                        && Arrays.equals(parameterTypes, method.getParameterTypes())) {
                    return method;
                }
            }
        }
    }
    // ok, now we are looking for an interface method... the most specific one
    // in the event that we have two unrelated interfaces both declaring a method of the same name
    // we will give up and say we could not find the method (the logic here is that we are primarily
    // checking for overrides, in the event of a Java 8 default method, that default only
    // applies if there is no conflict from an unrelated interface... thus if there are
    // default methods and they are unrelated then they don't exist... if there are multiple unrelated
    // abstract methods... well they won't count as a non-abstract implementation
    Method res = null;
    for (final Class<?> klass : (List<Class<?>>) ClassUtils.getAllInterfaces(cls)) {
        for (final Method method : klass.getDeclaredMethods()) {
            if (methodName.equals(method.getName())
                    && Arrays.equals(parameterTypes, method.getParameterTypes())) {
                if (res == null) {
                    res = method;
                } else {
                    Class<?> c = res.getDeclaringClass();
                    if (c == klass) {
                        // match, ignore
                    } else if (c.isAssignableFrom(klass)) {
                        // this is a more specific match
                        res = method;
                    } else if (!klass.isAssignableFrom(c)) {
                        // multiple overlapping interfaces declare this method and there is no common ancestor
                        return null;

                    }
                }
            }
        }
    }
    return res;
}

From source file:com.haulmont.cuba.core.app.AbstractBeansMetadata.java

protected List<MethodInfo> getAvailableMethods(String beanName) {
    List<MethodInfo> methods = new ArrayList<>();
    try {/*  www . ja  va2  s .c  om*/
        AutowireCapableBeanFactory beanFactory = AppContext.getApplicationContext()
                .getAutowireCapableBeanFactory();
        if (beanFactory instanceof CubaDefaultListableBeanFactory) {
            BeanDefinition beanDefinition = ((CubaDefaultListableBeanFactory) beanFactory)
                    .getBeanDefinition(beanName);
            if (beanDefinition.isAbstract())
                return methods;
        }

        Object bean = AppBeans.get(beanName);

        @SuppressWarnings("unchecked")
        List<Class> classes = ClassUtils.getAllInterfaces(bean.getClass());
        for (Class aClass : classes) {
            if (aClass.getName().startsWith("org.springframework."))
                continue;

            Class<?> targetClass = bean instanceof TargetClassAware ? ((TargetClassAware) bean).getTargetClass()
                    : bean.getClass();

            for (Method method : aClass.getMethods()) {
                if (isMethodAvailable(method)) {
                    Method targetClassMethod = targetClass.getMethod(method.getName(),
                            method.getParameterTypes());
                    List<MethodParameterInfo> methodParameters = getMethodParameters(targetClassMethod);
                    MethodInfo methodInfo = new MethodInfo(method.getName(), methodParameters);
                    addMethod(methods, methodInfo);
                }
            }

            if (methods.isEmpty()) {
                for (Method method : bean.getClass().getMethods()) {
                    if (!method.getDeclaringClass().equals(Object.class) && isMethodAvailable(method)) {
                        List<MethodParameterInfo> methodParameters = getMethodParameters(method);
                        MethodInfo methodInfo = new MethodInfo(method.getName(), methodParameters);
                        addMethod(methods, methodInfo);
                    }
                }
            }
        }
    } catch (Throwable t) {
        log.debug(t.getMessage());
    }
    return methods;
}

From source file:de.Keyle.MyPet.api.util.hooks.PluginHookManager.java

@SuppressWarnings("unchecked")
public void enableHooks() {
    for (PluginHook hook : registeredHooks) {
        try {/*from w w  w.  j  av  a  2s  .  co m*/
            PluginHookName hookNameAnnotation = hook.getClass().getAnnotation(PluginHookName.class);
            if (!hookNameAnnotation.classPath().equalsIgnoreCase("")) {
                if (!isPluginUsable(hook.getPluginName(), hookNameAnnotation.classPath())) {
                    return;
                }
            } else {
                if (!isPluginUsable(hook.getPluginName())) {
                    return;
                }
            }
            if (hook.onEnable()) {
                boolean genericHook = true;
                for (Object o : ClassUtils.getAllInterfaces(hook.getClass())) {
                    if (o != PluginHook.class && PluginHook.class.isAssignableFrom((Class) o)) {
                        hooks.put((Class) o, hook);
                        genericHook = false;
                    }
                }
                if (genericHook) {
                    hooks.put(PluginHook.class, hook);
                }
                hookByName.put(hook.getPluginName(), hook);
                hookByClass.put(hook.getClass(), hook);

                String message = hook.getPluginName();
                message += " (" + Bukkit.getPluginManager().getPlugin(hook.getPluginName()).getDescription()
                        .getVersion() + ")";
                if (!hookNameAnnotation.classPath().equalsIgnoreCase("")) {
                    message += "(" + hookNameAnnotation.classPath() + ")";
                }
                MyPetApi.getLogger().info(message + " hook activated.");
            }
        } catch (Throwable e) {
            MyPetApi.getLogger().warning("Error occured while enabling " + hook.getPluginName() + " ("
                    + Bukkit.getPluginManager().getPlugin(hook.getPluginName()).getDescription().getVersion()
                    + ") hook.");
            e.printStackTrace();
        }
    }
    registeredHooks.clear();
}

From source file:com.comphenix.noxp.FieldUtils.java

/**
 * Gets an accessible <code>Field</code> by name breaking scope if
 * requested. Superclasses/interfaces will be considered.
 * //from  w ww  .  j a  va2 s  .co m
 * @param cls the class to reflect, must not be null
 * @param fieldName the field name to obtain
 * @param forceAccess whether to break scope restrictions using the
 *            <code>setAccessible</code> method. <code>False</code> will
 *            only match public fields.
 * @return the Field object
 * @throws IllegalArgumentException if the class or field name is null
 */
public static Field getField(final Class cls, String fieldName, boolean forceAccess) {
    if (cls == null) {
        throw new IllegalArgumentException("The class must not be null");
    }
    if (fieldName == null) {
        throw new IllegalArgumentException("The field name must not be null");
    }
    // Sun Java 1.3 has a bugged implementation of getField hence we write
    // the
    // code ourselves

    // getField() will return the Field object with the declaring class
    // set correctly to the class that declares the field. Thus requesting
    // the
    // field on a subclass will return the field from the superclass.
    //
    // priority order for lookup:
    // searchclass private/protected/package/public
    // superclass protected/package/public
    // private/different package blocks access to further superclasses
    // implementedinterface public

    // check up the superclass hierarchy
    for (Class acls = cls; acls != null; acls = acls.getSuperclass()) {
        try {
            Field field = acls.getDeclaredField(fieldName);
            // getDeclaredField checks for non-public scopes as well
            // and it returns accurate results
            if (!Modifier.isPublic(field.getModifiers())) {
                if (forceAccess) {
                    field.setAccessible(true);
                } else {
                    continue;
                }
            }
            return field;
        } catch (NoSuchFieldException ex) {
            // ignore
        }
    }
    // check the public interface case. This must be manually searched for
    // incase there is a public supersuperclass field hidden by a
    // private/package
    // superclass field.
    Field match = null;
    for (Iterator intf = ClassUtils.getAllInterfaces(cls).iterator(); intf.hasNext();) {
        try {
            Field test = ((Class) intf.next()).getField(fieldName);
            if (match != null) {
                throw new IllegalArgumentException(
                        "Reference to field " + fieldName + " is ambiguous relative to " + cls
                                + "; a matching field exists on two or more implemented interfaces.");
            }
            match = test;
        } catch (NoSuchFieldException ex) {
            // ignore
        }
    }
    return match;
}

From source file:net.datenwerke.transloader.ClassWrapper.java

private static boolean classIsAssignableToType(Class rootClass, String typeName) {
    List allClasses = new ArrayList();
    allClasses.add(rootClass);//  w w w  .  java  2  s .  c om
    allClasses.addAll(ClassUtils.getAllSuperclasses(rootClass));
    allClasses.addAll(ClassUtils.getAllInterfaces(rootClass));
    List allClassNames = ClassUtils.convertClassesToClassNames(allClasses);
    return allClassNames.contains(typeName);
}

From source file:fr.exanpe.t5.lib.internal.authorize.AuthorizePageFilter.java

/**
 * Look for an {@link Authorize} annotation in the Page class or superclass
 * /*w ww  . j av a2 s .  c om*/
 * @param pageName the name of the page
 * @return the annotation found, or null
 */
@SuppressWarnings("unchecked")
private Authorize process(String pageName) {
    Component page = componentSource.getPage(pageName);

    Authorize auth = null;

    if ((auth = process(page.getClass())) != null) {
        return auth;
    }

    // handle inheritance
    for (Class<?> c : (List<Class<?>>) ClassUtils.getAllSuperclasses(page.getClass())) {
        if ((auth = process(c)) != null) {
            return auth;
        }
    }

    // handle inheritance
    for (Class<?> c : (List<Class<?>>) ClassUtils.getAllInterfaces(page.getClass())) {
        if ((auth = process(c)) != null) {
            return auth;
        }
    }

    return auth;
}

From source file:com.github.darwinevolution.darwin.EvolutionTest.java

@Test
public void commonsLangBackwardCompatibility() throws Exception {
    final String value = "some definitely not blank string";

    Boolean isBlank = Evolution.<Boolean>of("StringUtilsIsBlank")
            .withEvolutionResultConsumer(new EvolutionResultConsumer<Boolean>() {
                @Override//w w  w .  j  a v  a 2 s . co  m
                public void consumeResults(ComparisonResult<Boolean> comparisonResult,
                        ResultConsumerConfiguration resultConsumerConfiguration) {
                    Assertions.assertThat(comparisonResult.getResultType()).isEqualTo(ResultType.OK);
                }
            }).from(new ProtoplastExecutionHarness<Boolean>() {
                @Override
                public Boolean execute() throws Exception {
                    arguments(value);
                    return StringUtils.isBlank(value);
                }
            }).to(new EvolvedExecutionHarness<Boolean>() {
                @Override
                public Boolean execute() throws Exception {
                    arguments(value);
                    return org.apache.commons.lang3.StringUtils.isBlank(value);
                }
            }).evolve();

    List allInterfaces = Evolution.<List>of("allInterfaces")
            .withEvolutionResultConsumer(new EvolutionResultConsumer<List>() {
                @Override
                public void consumeResults(ComparisonResult<List> comparisonResult,
                        ResultConsumerConfiguration resultConsumerConfiguration) {
                    Assertions.assertThat(comparisonResult.getResultType()).isEqualTo(ResultType.OK);
                }
            }).from(new ProtoplastExecutionHarness<List>() {
                @Override
                public List execute() throws Exception {
                    return ClassUtils.getAllInterfaces(ProtoplastValueExecutionResult.class);
                }
            }).to(new EvolvedExecutionHarness<List>() {
                @Override
                public List execute() throws Exception {
                    return org.apache.commons.lang3.ClassUtils
                            .getAllInterfaces(ProtoplastValueExecutionResult.class);
                }
            }).evolve();
}

From source file:com.oltpbenchmark.util.ClassUtil.java

/**
 * Get a set of all of the interfaces that the element_class implements
 * @param element_class/*from  w  w  w.j a va  2 s  .c o  m*/
 * @return
 */
@SuppressWarnings("unchecked")
public static Collection<Class<?>> getInterfaces(Class<?> element_class) {
    Set<Class<?>> ret = ClassUtil.CACHE_getInterfaceClasses.get(element_class);
    if (ret == null) {
        //            ret = new HashSet<Class<?>>();
        //            Queue<Class<?>> queue = new LinkedList<Class<?>>();
        //            queue.add(element_class);
        //            while (!queue.isEmpty()) {
        //                Class<?> current = queue.poll();
        //                for (Class<?> i : current.getInterfaces()) {
        //                    ret.add(i);
        //                    queue.add(i);
        //                } // FOR
        //            } // WHILE
        ret = new HashSet<Class<?>>(ClassUtils.getAllInterfaces(element_class));
        if (element_class.isInterface())
            ret.add(element_class);
        ret = Collections.unmodifiableSet(ret);
        ClassUtil.CACHE_getInterfaceClasses.put(element_class, ret);
    }
    return (ret);
}