Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

In this page you can find the example usage for java.lang Class isAssignableFrom.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

From source file:com.chiorichan.factory.EvalFactory.java

/**
 * //www.ja  va  2s .c  o  m
 * @param orig
 *            , The original class you would like to override.
 * @param replace
 *            , An instance of the class you are overriding with. Must extend the original class.
 */
public static boolean overrideProcessor(Class<? extends PreProcessor> orig, PreProcessor replace) {
    if (!orig.isAssignableFrom(replace.getClass()))
        return false;

    for (PreProcessor p : preProcessors)
        if (p.getClass().equals(orig))
            preProcessors.remove(p);
    register(replace);

    return true;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findCIDistance(Class<?> lower, Class<?> upper) {
    if (!upper.isInterface() || lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : upper=%s lower=%s", upper, lower));
    }//from  w  w  w  .j a  v  a  2 s. c  om

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = -1;
    int offset = 0;
    do {
        int tmp = findCIOneLevelDistance(lower, upper);
        if (tmp >= 0) {
            tmp += offset + 1;
            distance = distance < 0 ? tmp : Math.min(distance, tmp);
        }

        if (distance == 0) {
            break;
        }

        lower = lower.getSuperclass();
        offset++;
    } while (lower != null);

    return distance;

}

From source file:com.chiorichan.factory.EvalFactory.java

/**
 * /* w w w.  ja  v a2 s.  co  m*/
 * @param orig
 *            The original class you would like to override.
 * @param replace
 *            An instance of the class you are overriding with. Must extend the original class.
 */
public static boolean overrideProcessor(Class<? extends PostProcessor> orig, PostProcessor replace) {
    if (!orig.isAssignableFrom(replace.getClass()))
        return false;

    for (PostProcessor p : postProcessors)
        if (p.getClass().equals(orig))
            postProcessors.remove(p);
    register(replace);

    return true;
}

From source file:com.github.rinde.rinsim.scenario.generator.ScenarioGenerator.java

@SuppressWarnings("unchecked")
static <T extends Model<?>> List<ModelBuilder<T, ?>> findBuildersThatBuild(
        Iterable<? extends ModelBuilder<?, ?>> builders, Class<T> type) {
    final List<ModelBuilder<T, ?>> foundBuilders = new ArrayList<>();
    for (final ModelBuilder<?, ?> b : builders) {
        if (type.isAssignableFrom(b.getModelType())) {
            foundBuilders.add((ModelBuilder<T, ?>) b);
        }//from   w  ww .  j  a v a 2s .c  o m
    }
    return foundBuilders;
}

From source file:com.netflix.spinnaker.halyard.config.model.v1.node.Node.java

/**
 * @param clazz the class to check against.
 * @return a NodeMatcher that matches all nodes of given clazz.
 *///  www . j  a va2  s  .  c  o  m
static public NodeMatcher thisNodeAcceptor(Class clazz) {
    return new NodeMatcher() {
        @Override
        public boolean matches(Node n) {
            return clazz.isAssignableFrom(n.getClass());
        }

        @Override
        public String getName() {
            return "Match against [" + clazz.getSimpleName() + ":*]";
        }
    };
}

From source file:io.appium.java_client.events.DefaultAspect.java

private static Class<?> getClassForProxy(Class<?> classOfObject) {
    Class<?> returnStatement = null;
    for (Class<?> c : listenable) {
        if (!c.isAssignableFrom(classOfObject)) {
            continue;
        }/*from   ww  w  .  j  a v a  2s.c om*/
        returnStatement = c;
    }
    return returnStatement;
}

From source file:jp.terasoluna.fw.util.GenericPropertyUtil.java

/**
 * ??????? <code>Generics</code>?????
 * @param genericClass <code>Generics</code>? ???
 * @param clazz ???/*from  w w  w. jav  a 2  s. co  m*/
 * @param type ????? <code>Type</code>
 * @param index ??
 * @return <code>Generics</code>??? ???????<code>Object</code>???
 * @throws IllegalArgumentException <code>genericClass</code>? <code>null</code>?? <code>clazz</code>?
 *             <code>null</code>?? <code>index</code>?<code>0</code>???????? ????
 * @throws IllegalStateException ?<code>WildCardType</code>???
 */
@SuppressWarnings("unchecked")
protected static Class<?> resolveType(Class<?> genericClass, @SuppressWarnings("rawtypes") Class clazz,
        Type type, int index) throws IllegalArgumentException, IllegalStateException {
    if (genericClass == null) {
        throw new IllegalArgumentException("Argument 'genericsClass' (" + Class.class.getName() + ") is null");
    }
    if (clazz == null || !genericClass.isAssignableFrom(clazz)) {
        throw new IllegalStateException(genericClass + " is not assignable from " + clazz);
    }

    List<ParameterizedType> ancestorTypeList = null;
    try {
        ancestorTypeList = GenericsUtil.getAncestorTypeList(genericClass, clazz);
    } catch (IllegalStateException e) {
        if (log.isTraceEnabled()) {
            log.trace(e.getMessage());
        }
    }
    if (ancestorTypeList == null) {
        ancestorTypeList = new ArrayList<ParameterizedType>();
    }
    if (type instanceof ParameterizedType) {
        ancestorTypeList.add(0, (ParameterizedType) type);
    }
    if (ancestorTypeList.size() <= 0) {
        throw new IllegalStateException("No parameterizedType was detected.");
    }
    ParameterizedType parameterizedType = ancestorTypeList.get(ancestorTypeList.size() - 1);
    Type[] actualTypes = parameterizedType.getActualTypeArguments();

    // ??????
    if (index < 0 || index >= actualTypes.length) {
        throw new IllegalArgumentException("Argument 'index'(" + Integer.toString(index)
                + ") is out of bounds of" + " generics parameters");
    }

    Class<?> resolved = Object.class;
    try {
        resolved = GenericsUtil.resolveTypeVariable(actualTypes[index], ancestorTypeList);
    } catch (IllegalStateException e) {
        if (log.isTraceEnabled()) {
            log.trace(e.getMessage());
        }
    }
    return resolved;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findCCDistance(Class<?> lower, Class<?> upper) {
    if (upper.isInterface() || lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : cannot be interfaces. upper=%s lower=%s", upper, lower));
    }/*from   w ww  .  j a v a2 s.com*/

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = 0;
    while (lower != null && lower != upper) {
        lower = lower.getSuperclass();
        distance++;
    }

    if (lower == null) {
        throw new RuntimeException("BUG");
    }

    return distance;

}

From source file:org.web4thejob.util.CoreUtil.java

public static Set<String> getSubClasses(Class<? extends Entity> clazz) {
    Set<String> subs = new HashSet<String>(1);

    for (EntityMetadata em : ContextUtil.getMRS().getEntityMetadatas()) {
        if (clazz.isAssignableFrom(em.getEntityType())) {
            subs.add(em.getMappedClass().getCanonicalName());
        }//from  www .j a va 2 s  .c  o  m
    }

    return subs;
}

From source file:com.espertech.esper.util.MethodResolver.java

private static boolean isIdentityConversion(Class declarationType, Class invocationType) {
    if (wrappingConversions.containsKey(declarationType)) {
        return wrappingConversions.get(declarationType).contains(invocationType)
                || declarationType.isAssignableFrom(invocationType);
    } else {//from w w w  .  j av a  2s .c  om
        if (invocationType == null) {
            return !declarationType.isPrimitive();
        }
        return declarationType.isAssignableFrom(invocationType);
    }

}