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

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

Introduction

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

Prototype

public static Iterable<Class<?>> hierarchy(final Class<?> type) 

Source Link

Document

Get an Iterable that can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.

Usage

From source file:therian.Operation.java

/**
 * Learn whether {@code operator} seems to implement {@code this}.
 *
 * @param operator to check//from www.j  a va  2 s  . co  m
 * @return boolean
 */
public boolean matches(Operator<?> operator) {
    final Type expectedType = TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(operator.getClass(), Operator.class),
            Operator.class.getTypeParameters()[0]);

    if (!TypeUtils.isInstance(this, expectedType)) {
        return false;
    }

    final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(expectedType, Operation.class);
    for (Class<?> c : ClassUtils.hierarchy(TypeUtils.getRawType(expectedType, operator.getClass()))) {
        if (c.equals(Operation.class)) {
            break;
        }
        for (TypeVariable<?> var : c.getTypeParameters()) {
            Type type = Types.resolveAt(this, var, typeArguments);
            if (type == null || typeArguments == null) {
                continue;
            }
            if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
                type = ClassUtils.primitiveToWrapper((Class<?>) type);
            }
            if (!TypeUtils.isAssignable(type, TypeUtils.unrollVariables(typeArguments, var))) {
                return false;
            }
        }
    }
    return true;
}

From source file:therian.OperatorManager.java

private static void validate(Set<Operator<?>> operators) {
    final Set<Class<?>> operatorsPresent = new HashSet<>();
    final Set<Class<?>> operatorsNeeded = new HashSet<>();

    for (Operator<?> operator : operators) {
        Operators.validateImplementation(operator);

        final Class<?> opType = operator.getClass();
        operatorsPresent.add(opType);//from  w w w .j a  v a2  s .  c o  m

        for (Class<?> c : ClassUtils.hierarchy(opType)) {
            final DependsOn dependsOn = c.getAnnotation(DependsOn.class);
            if (dependsOn != null) {
                Collections.addAll(operatorsNeeded, dependsOn.value());
            }
        }
    }
    operatorsNeeded.removeAll(operatorsPresent);
    Validate.isTrue(operatorsNeeded.isEmpty(), "Missing required operators: %s", operatorsNeeded);
}