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

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

Introduction

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

Prototype

public static List<Class<?>> getAllInterfaces(final 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:com.link_intersystems.lang.reflect.ThreadLocalProxy.java

@SuppressWarnings("unchecked")
public static <T, TC> T createProxy(ThreadLocal<T> threadLocal, T nullInstance, Class<TC> targetClass) {
    List<Class<?>> allInterfaces = new ArrayList<Class<?>>(ClassUtils.getAllInterfaces(targetClass));
    if (targetClass.isInterface()) {
        allInterfaces.add(targetClass);//from ww  w  .j a v a  2  s  .c o m
    }
    Class<?>[] interfaces = (Class<?>[]) allInterfaces.toArray(new Class<?>[allInterfaces.size()]);
    T proxy = (T) Proxy.newProxyInstance(targetClass.getClassLoader(), interfaces,
            new ThreadLocalProxy(threadLocal, nullInstance));
    return proxy;
}

From source file:com.quatico.base.aem.test.api.setup.SetupFactory.java

public T getSetup(final Object... implementors) {
    final Map<Class<?>, Object> interfaces = new HashMap<>();
    for (Object implementor : implementors) {
        List<Class<?>> implementorInterfaces = ClassUtils.getAllInterfaces(implementor.getClass());

        assert !implementorInterfaces.isEmpty();

        for (Class<?> implementorInterface : implementorInterfaces) {
            interfaces.put(implementorInterface, implementor);
        }//from  ww  w.ja va 2s  .co m
    }

    @SuppressWarnings("unchecked")
    T result = (T) Proxy.newProxyInstance(this.interfaze.getClassLoader(), new Class<?>[] { this.interfaze },
            new InvocationHandler() {
                private final Object fProxyObject = new Object() {
                    @Override
                    public String toString() {
                        return "Proxy for: " + SetupFactory.this.interfaze.getDeclaringClass().getName(); //$NON-NLS-1$
                    }
                };

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getDeclaringClass().equals(Object.class)) {
                        method.invoke(this.fProxyObject, args);
                    }

                    if (interfaces.containsKey(method.getDeclaringClass())) {
                        return method.invoke(interfaces.get(method.getDeclaringClass()), args);
                    }

                    throw new UnsupportedOperationException(
                            "Created proxy has not received an implementation that supports this method: " //$NON-NLS-1$
                                    + method.getName());
                }
            });

    return result;
}

From source file:net.audumla.climate.ClimateDataFactory.java

/**
 * Replaces the climate data with a readonly version.
 *
 * @param cd the existing climate data bean
 * @return the climate data//from   w w  w  .j a  v  a 2  s  .co  m
 */
public static ClimateData convertToReadOnlyClimateData(ClimateData cd) {
    if (cd == null) {
        return null;
    }
    Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
    interfaces.addAll(ClassUtils.getAllInterfaces(cd.getClass()));
    interfaces.remove(WritableClimateData.class);
    return BeanUtils.convertBean(cd, interfaces.toArray(new Class<?>[interfaces.size()]));
}

From source file:com.github.robozonky.app.events.SessionEvents.java

@SuppressWarnings("unchecked")
static <T extends Event> Class<T> getImplementingEvent(final Class<T> original) {
    final Stream<Class<?>> provided = ClassUtils.getAllInterfaces(original).stream();
    final Stream<Class<?>> interfaces = original.isInterface() ? // interface could be extending it directly
            Stream.concat(Stream.of(original), provided) : provided;
    final String apiPackage = "com.github.robozonky.api.notifications";
    return (Class<T>) interfaces.filter(i -> Objects.equals(i.getPackage().getName(), apiPackage))
            .filter(i -> i.getSimpleName().endsWith("Event"))
            .filter(i -> !Objects.equals(i.getSimpleName(), "Event")).findFirst()
            .orElseThrow(() -> new IllegalStateException("Not an event:" + original));
}

From source file:net.audumla.climate.ClimateDataFactory.java

/**
 * Replaces the climate observation with a readonly version.
 *
 * @param cd the existing climate data bean
 * @return the climate data/*from   w w w  . java2  s.c  o  m*/
 */
public static WritableClimateObservation convertToWritableClimateObservation(ClimateObservation cd) {
    if (cd == null) {
        return null;
    }
    Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
    interfaces.addAll(ClassUtils.getAllInterfaces(cd.getClass()));
    return BeanUtils.convertBean(cd, WritableClimateObservation.class,
            interfaces.toArray(new Class<?>[interfaces.size()]));
}

From source file:net.audumla.climate.ClimateDataFactory.java

/**
 * Replaces the climate data with a writable version.
 *
 * @param cd the existing climate data bean
 * @return the climate data/*w ww  .  j  a  v a 2 s  .com*/
 */
public static WritableClimateData convertToWritableClimateData(ClimateData cd) {
    if (cd == null) {
        return null;
    }
    Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
    interfaces.addAll(ClassUtils.getAllInterfaces(cd.getClass()));
    return BeanUtils.convertBean(cd, WritableClimateData.class,
            interfaces.toArray(new Class<?>[interfaces.size()]));
}

From source file:fr.inria.atlanmod.neoemf.core.PersistentEObjectAdapter.java

/**
 * Create an adapter for the given {@code object} in a specific {@code type}.
 *
 * @param adaptableObject the object to adapt
 * @param adapterType     the class in which the object must be adapted
 *
 * @return an adapted object in the given {@code type}
 *///from  w w  w  .  jav a  2 s .co m
private static Object createAdapter(Object adaptableObject, Class<?> adapterType) {
    /*
     * Compute the interfaces that the proxy has to implement
    * These are the current interfaces + PersistentEObject
    */
    List<Class<?>> interfaces = ClassUtils.getAllInterfaces(adaptableObject.getClass());
    interfaces.add(PersistentEObject.class);

    // Create the proxy
    Enhancer proxy = new Enhancer();

    /*
       * Use the ClassLoader of the type, otherwise it will cause OSGI troubles (like project trying to
     * create an PersistentEObject while it does not have a dependency to NeoEMF core)
     */
    proxy.setClassLoader(adapterType.getClassLoader());
    proxy.setSuperclass(adaptableObject.getClass());
    proxy.setInterfaces(interfaces.toArray(new Class[interfaces.size()]));
    proxy.setCallback(new PersistentEObjectProxyHandler());

    return proxy.create();
}

From source file:net.audumla.climate.ClimateDataFactory.java

/**
 * Replaces the climate observation with a readonly version.
 *
 * @param cd the existing climate data bean
 * @return the climate data//from w w w .  j a  v  a2  s . c  om
 */
public static ClimateObservation convertToReadOnlyClimateObservation(ClimateObservation cd) {
    if (cd == null) {
        return null;
    }
    Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
    interfaces.addAll(ClassUtils.getAllInterfaces(cd.getClass()));
    interfaces.remove(WritableClimateObservation.class);
    return BeanUtils.convertBean(cd, interfaces.toArray(new Class<?>[interfaces.size()]));
}

From source file:com.threewks.thundr.introspection.ClassIntrospector.java

public List<Class<?>> listImplementedTypes(Class<?> type) {
    EList<Class<?>> types = Expressive.<Class<?>>list(type);
    types.addItems(ClassUtils.getAllSuperclasses(type));
    types.addItems(ClassUtils.getAllInterfaces(type));
    return types;
}

From source file:de.metanome.backend.algorithm_loading.AlgorithmAnalyzer.java

protected Set<Class<?>> extractInterfaces(Object object) {
    return new HashSet<>(ClassUtils.getAllInterfaces(object.getClass()));
}