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:geva.Main.AbstractRun.java

/**
 * Load the fitness class according to the parameters
 * @param p Properties/*from  w  ww  .j  av a 2 s . c  o m*/
 * @return FitnessFunction
 */
protected FitnessFunction getFitnessFunction(Properties p) {
    FitnessFunction fitnessFunction = null;
    String className;
    String key = Constants.FITNESS_FUNCTION;
    try {
        className = p.getProperty(key);
        if (className == null) {
            throw new BadParameterException(key);
        }
        Class clazz = Class.forName(className);
        fitnessFunction = (FitnessFunction) clazz.newInstance();
        fitnessFunction.setProperties(p);
        Class[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            if (interfaces[i].getName().equals(Stochastic.class.getName())) {
                ((Stochastic) fitnessFunction).setRNG(this.rng);
            }
        }
    } catch (Exception e) {
        logger.error("Exception: ", e);
    }
    return fitnessFunction;
}

From source file:org.apache.camel.component.bean.MethodInfo.java

/**
 * Finds the oneway annotation in priority order; look for method level annotations first, then the class level annotations,
 * then super class annotations then interface annotations
 *
 * @param method the method on which to search
 * @return the first matching annotation or none if it is not available
 */// w  w w.  ja v  a2s  .c  o  m
protected Pattern findOneWayAnnotation(Method method) {
    Pattern answer = getPatternAnnotation(method);
    if (answer == null) {
        Class<?> type = method.getDeclaringClass();

        // lets create the search order of types to scan
        List<Class<?>> typesToSearch = new ArrayList<Class<?>>();
        addTypeAndSuperTypes(type, typesToSearch);
        Class<?>[] interfaces = type.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            addTypeAndSuperTypes(anInterface, typesToSearch);
        }

        // now lets scan for a type which the current declared class overloads
        answer = findOneWayAnnotationOnMethod(typesToSearch, method);
        if (answer == null) {
            answer = findOneWayAnnotation(typesToSearch);
        }
    }
    return answer;
}

From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java

/**
 * Get all the classes referenced by the given class, which might be used by an extension. We consider visible
 * methods, constructors, fields and inner classes, as well as superclasses and interfaces extended by the class.
 *
 * @param initialClass The class to analyse.
 * @param consideredClasses Classes that have already been considered, and which should not be considered again. If
 *            the given class has already been considered then an empty set will be returned. This set will be
 *            updated with the given class.
 * @return The set of classes that might be accessible by an extension of this class.
 *//* w w  w .  j  a v a 2  s . c o  m*/
private static Set<Class<?>> getReferencedClassesFromClass(Class<?> initialClass,
        Set<Class<?>> consideredClasses) {
    Set<Class<?>> referencedClasses = new HashSet<>();

    if (consideredClasses.add(initialClass)) {
        for (Method method : initialClass.getDeclaredMethods()) {
            if (isVisibleToExtender(method.getModifiers())) {
                referencedClasses.addAll(getClassesFromMethod(method));
            }
        }
        for (Constructor<?> constructor : initialClass.getDeclaredConstructors()) {
            if (isVisibleToExtender(constructor.getModifiers())) {
                referencedClasses.addAll(getClassesFromConstructor(constructor));
            }
        }
        for (Field field : initialClass.getDeclaredFields()) {
            if (isVisibleToExtender(field.getModifiers())) {
                referencedClasses.addAll(getClassesFromField(field));
            }
        }
        for (Class<?> clazz : initialClass.getDeclaredClasses()) {
            if (isVisibleToExtender(clazz.getModifiers())) {
                referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
            }
        }
        if (initialClass.getSuperclass() != null) {
            referencedClasses
                    .addAll(getReferencedClassesFromClass(initialClass.getSuperclass(), consideredClasses));
        }
        for (Class<?> clazz : initialClass.getInterfaces()) {
            referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
        }
    }
    return referencedClasses;
}

From source file:com.zenesis.qx.remote.ProxyTypeImpl.java

protected Boolean canProxy(Class clazz, Method method) {
    if (method.isAnnotationPresent(AlwaysProxy.class)
            || method.isAnnotationPresent(com.zenesis.qx.remote.annotations.Method.class))
        return true;
    for (Class tmp : clazz.getInterfaces())
        if (Proxied.class.isAssignableFrom(tmp)) {
            Boolean test = canProxy(tmp, method);
            if (test != null)
                return test;
        }/*from  w ww . j  av a 2  s.  c om*/
    Class superClazz = clazz.getSuperclass();
    if (superClazz != null && Proxied.class.isAssignableFrom(superClazz))
        return canProxy(superClazz, method);
    return null;
}

From source file:eu.qualityontime.commons.MethodUtils.java

/**
 * <p>// w w  w  .  java 2 s .  c  o m
 * Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified method, by scanning through all
 * implemented interfaces and subinterfaces. If no such method can be found,
 * return <code>null</code>.
 * </p>
 *
 * <p>
 * There isn't any good reason why this method must be private. It is
 * because there doesn't seem any reason why other classes should call this
 * rather than the higher level methods.
 * </p>
 *
 * @param clazz
 *            Parent class for the interfaces to be checked
 * @param methodName
 *            Method name of the method we wish to call
 * @param parameterTypes
 *            The parameter type signatures
 */
private static Method getAccessibleMethodFromInterfaceNest(Class<?> clazz, final String methodName,
        final Class<?>[] parameterTypes) {

    Method method = null;

    // Search up the superclass chain
    for (; clazz != null; clazz = clazz.getSuperclass()) {

        // Check the implemented interfaces of the parent class
        final Class<?>[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {

            // Is this interface public?
            if (!Modifier.isPublic(interfaces[i].getModifiers())) {
                continue;
            }

            // Does the method exist on this interface?
            try {
                method = interfaces[i].getDeclaredMethod(methodName, parameterTypes);
            } catch (final NoSuchMethodException e) {
                /*
                 * Swallow, if no method is found after the loop then this
                 * method returns null.
                 */
            }
            if (method != null) {
                return method;
            }

            // Recursively check our parent interfaces
            method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
            if (method != null) {
                return method;
            }

        }

    }

    // We did not find anything
    return null;
}

From source file:de.javadesign.cdi.extension.spring.SpringBeanIntegrationExtension.java

/**
 * Try to create a bean if class is found.
 * //from   w w  w . java 2s  . c o m
 * @param beanName
 *            the bean name to find and create
 * @param beanDefinition
 *            the spring bean definition
 * @param beanManager
 *            the injection bean manager
 * @param beanFactory
 *            a ConfigurableListableBeanFactory
 * @return the created bean or null if class was not found
 */
private Bean<?> createBean(final String beanName, final BeanDefinition beanDefinition,
        final ConfigurableListableBeanFactory beanFactory, final BeanManager beanManager) {
    Class<?> beanClass = null;

    try {
        if (null == beanDefinition.getBeanClassName()) {
            // TODO: Support beans which are created via factory bean.
            LOGGER.warn("Ignored bean with name {} - there is no definition via bean's class name available",
                    beanName);
            return CLASS_NOT_FOUND;
        }
        beanClass = Class.forName(beanDefinition.getBeanClassName());

        // FactoryBean? Bean class is returned by getObjectType method
        for (Class<?> beanClassInterface : beanClass.getInterfaces()) {
            if (!beanClassInterface.equals(FactoryBean.class)) {
                continue;
            }
            try {
                Method getObjectTypeMethod = beanClass.getDeclaredMethod(METHOD_NAME_GET_OBJECT_TYPE);
                String s = ((ParameterizedType) getObjectTypeMethod.getGenericReturnType())
                        .getActualTypeArguments()[0].toString();
                beanClass = Class.forName(s.substring(s.lastIndexOf(" ") + 1));

            } catch (NoSuchMethodException ignored) {
                LOGGER.warn("Ignored bean {} with Class {} that is assumed to be a FactoryBean, "
                        + "but has no getObjectType method.", beanName, beanClass);
                return CLASS_NOT_FOUND;
            }
            break;
        }

    } catch (final ClassNotFoundException e) {
        LOGGER.warn("Class {} not found.", beanName);
        return CLASS_NOT_FOUND;
    }

    final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(beanClass);
    final Set<Type> beanTypes = annotatedType.getTypeClosure();
    final Set<Annotation> qualifiers = new HashSet<Annotation>();
    qualifiers.add(new AnnotationLiteral<Any>() {

        private static final long serialVersionUID = 1L;
        // any implementation
    });
    qualifiers.add(new AnnotationLiteral<Default>() {

        private static final long serialVersionUID = 1L;
        // default implementation
    });

    final Set<Class<? extends Annotation>> stereotypes = new HashSet<Class<? extends Annotation>>();

    for (final Annotation annotation : annotatedType.getAnnotations()) {
        if (beanManager.isQualifier(annotation.annotationType())) {
            qualifiers.add(annotation);
        }
        if (beanManager.isStereotype(annotation.annotationType())) {
            stereotypes.add(annotation.annotationType());
        }
    }

    return new SpringBean(beanName, beanClass, beanTypes, qualifiers, stereotypes, beanFactory);
}

From source file:org.ebayopensource.twin.ElementImpl.java

public boolean is(Class<? extends Element> pattern) {
    if (pattern.isInterface())
        for (Class<?> iface : pattern.getInterfaces()) {
            if (iface == ControlPattern.class)
                return controlPatterns.contains(pattern);
            else if (iface == ControlType.class)
                return controlType == pattern;
        }//ww w.  ja v a  2  s .co  m
    // if it's not a control type or control pattern, return instanceof
    return pattern.isInstance(this);
}

From source file:org.apache.tapestry.util.AdaptorRegistry.java

/**
 * Searches the registration Map for a match, based on inheritance.
 *
 * <p>Searches class inheritance first, then interfaces (in a rather vague order).
 * Really should match the order from the JVM spec.
 *
 * <p>There's a degenerate case where we may check the same interface more than once:
 * <ul>//w  w w  .j a va 2  s.  c o m
 * <li>Two interfaces, I1 and I2
 * <li>Two classes, C1 and C2
 * <li>I2 extends I1
 * <li>C2 extends C1
 * <li>C1 implements I1
 * <li>C2 implements I2
 * <li>The search will be: C2, C1, I2, I1, I1
 * <li>I1 is searched twice, because C1 implements it, and I2 extends it
 * <li>There are other such cases, but none of them cause infinite loops
 * and most are rare (we could guard against it, but its relatively expensive).
 * <li>Multiple checks only occur if we don't find a registration
 * </ul>
 *
 *  <p>
 *  This method is only called from a synchronized block, so it is
 *  implicitly synchronized.
 * 
 **/

private Object searchForAdaptor(Class subjectClass) {
    LinkedList queue = null;
    Object result = null;

    if (LOG.isDebugEnabled())
        LOG.debug("Searching for adaptor for class " + Tapestry.getClassName(subjectClass));

    // Step one: work up through the class inheritance.

    Class searchClass = subjectClass;

    // Primitive types have null, not Object, as their parent
    // class.

    while (searchClass != Object.class && searchClass != null) {
        result = registrations.get(searchClass);
        if (result != null)
            return result;

        // Not an exact match.  If the search class
        // implements any interfaces, add them to the queue.

        Class[] interfaces = searchClass.getInterfaces();
        int length = interfaces.length;

        if (queue == null && length > 0)
            queue = new LinkedList();

        for (int i = 0; i < length; i++)
            queue.addLast(interfaces[i]);

        // Advance up to the next superclass

        searchClass = getSuperclass(searchClass);

    }

    // Ok, the easy part failed, lets start searching
    // interfaces.

    if (queue != null) {
        while (!queue.isEmpty()) {
            searchClass = (Class) queue.removeFirst();

            result = registrations.get(searchClass);
            if (result != null)
                return result;

            // Interfaces can extend other interfaces; add them
            // to the queue.

            Class[] interfaces = searchClass.getInterfaces();
            int length = interfaces.length;

            for (int i = 0; i < length; i++)
                queue.addLast(interfaces[i]);
        }
    }

    // Not a match on interface; our last gasp is to check
    // for a registration for java.lang.Object

    result = registrations.get(Object.class);
    if (result != null)
        return result;

    // No match?  That's rare ... and an error.

    throw new IllegalArgumentException(
            Tapestry.format("AdaptorRegistry.adaptor-not-found", Tapestry.getClassName(subjectClass)));
}

From source file:org.gradle.model.internal.method.WeaklyTypeReferencingMethod.java

private Method findMethod(Class<?> clazz, Class<?>[] paramTypes) {
    Set<Class<?>> seenInterfaces = null;
    Queue<Class<?>> queue = null;
    Class<?> type = clazz;
    while (type != null) {
        for (Method method : type.getDeclaredMethods()) {
            if (method.getName().equals(name) && Arrays.equals(paramTypes, method.getParameterTypes())) {
                return method;
            }/*from   www. j a  v  a2s  .  c om*/
        }

        if (queue == null) {
            queue = new ArrayDeque<Class<?>>();
            seenInterfaces = Sets.newHashSet();
        }

        Class<?> superclass = type.getSuperclass();
        if (superclass != null) {
            queue.add(superclass);
        }
        for (Class<?> iface : type.getInterfaces()) {
            if (seenInterfaces.add(iface)) {
                queue.add(iface);
            }
        }

        type = queue.poll();
    }

    throw new org.gradle.internal.reflect.NoSuchMethodException(
            String.format("Could not find method %s(%s) on %s.", name, Joiner.on(", ").join(paramTypes),
                    this.target.getRawClass().getSimpleName()));
}

From source file:com.m4rc310.cb.builders.ComponentBuilder.java

private Object getNewInstanceObjectAnnotated(String ref, Object... args) {
    Object ret = null;/*from  w  w w.  j  av  a 2  s. co  m*/
    try {
        for (Class in : ClassScan.findAll().annotatedWith(Adialog.class).recursively()
                .in(conf.get("path_gui").toString(), "com.m4rc310.gui")) {
            Adialog ad = (Adialog) in.getDeclaredAnnotation(Adialog.class);
            if (ad.ref().equals(ref)) {
                if (ret != null) {
                    throw new Exception(String.format("H mais de uma classe refernciada como [%s]!", ref));
                }

                Class[] types = new Class[args.length];

                Constructor constructor = null;
                for (int i = 0; i < args.length; i++) {
                    types[i] = args[i].getClass();
                    Class type = args[i].getClass();
                    for (Class ai : type.getInterfaces()) {
                        try {
                            constructor = in.getDeclaredConstructor(ai);
                            break;
                        } catch (NoSuchMethodException | SecurityException e) {
                            infoError(e);
                        }
                    }
                }

                constructor = constructor == null ? in.getDeclaredConstructor(types) : constructor;

                //                    Constructor constructor = in.getDeclaredConstructor(types);
                constructor.setAccessible(true);
                ret = constructor.newInstance(args);
            }
        }
        return ret;
    } catch (Exception e) {
        infoError(e);
        throw new UnsupportedOperationException(e);
    }
}