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

@SuppressWarnings("unchecked")
public static int indexOfLike(Class[] a, Class key) {
    if (null == a || a.length == 0 || null == key) {
        return -1;
    }//from   ww w  . ja  va2 s.  c o m
    for (int i = 0; i < a.length; i++) {
        final Class item = a[i];
        // is 'item' equal or super-class of 'key'?
        if (key.equals(item) || item.isAssignableFrom(key)) {
            return i;
        }
    }
    return -1;
}

From source file:Main.java

private static List<Object> createParamsArray(Method m, List<Object> availableParams) {
    List<Object> params = new ArrayList<Object>();
    Class<?>[] paramTypes = m.getParameterTypes();

    for (Class<?> type : paramTypes) {
        for (Object param : availableParams) {
            if (param != null && type.isAssignableFrom(param.getClass())) {
                params.add(param);/*from   w w w  . ja  v a2 s  .com*/
            }
        }
    }

    return params;
}

From source file:com.aqnote.shared.cryptology.util.ProviderUtil.java

@SuppressWarnings("unchecked")
public static void addProvider(String className) throws Exception {
    if (StringUtils.isBlank(className))
        return;/*  ww  w .  java 2s.c o  m*/
    Class<?> clazz = Class.forName(className);
    if (clazz == null || !clazz.isAssignableFrom(Provider.class))
        return;
    addProvider((Class<Provider>) clazz);
}

From source file:com.aqnote.shared.cryptology.util.ProviderUtil.java

@SuppressWarnings("unchecked")
public static void removeProvider(String className) throws Exception {
    if (StringUtils.isBlank(className))
        return;/*from  w ww .j a v a  2  s.  c o m*/
    Class<?> clazz = Class.forName(className);
    if (clazz == null || !clazz.isAssignableFrom(Provider.class))
        return;
    removeProvider((Class<Provider>) clazz);
}

From source file:cz.cuni.mff.d3s.been.mapstore.mongodb.SpringMongoDBConverter.java

/**
 * Determine whether a class is of a well-known type
 *
 * @param clazz Class to inspect/*from  w ww .  j a v a 2 s . c o  m*/
 *
 * @return <code>true</code> if the class is of a well-known type, <code>false</code> otherwise
 */
public static boolean isStandardClass(Class clazz) {
    if (clazz.isAssignableFrom(Date.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Number.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(String.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(ObjectId.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(BSONObject.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Boolean.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Double.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Integer.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Long.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(Pattern.class)) // standard, pass
        return true;
    else if (clazz.isAssignableFrom(UUID.class)) // standard, pass
        return true;

    return false;
}

From source file:Main.java

public static boolean isAssignable(Class<?> clazz1, Class<?> clazz2) {
    if (clazz1 == null || clazz2 == null)
        return false;
    return clazz1.isAssignableFrom(clazz2) || clazz2.isAssignableFrom(clazz1);
}

From source file:Main.java

public static String getTypeForField(Field field) {
    Class type = field.getType();
    // Animation//from w  w w  .  ja v a2 s  .  com
    if (type.isAssignableFrom(Animation.class))
        return "anim";
    // Color State List
    else if (type.isAssignableFrom(ColorStateList.class))
        return "color";
    // Drawable
    else if (type.isAssignableFrom(Drawable.class))
        return "drawable";
    // String
    else if (type.isAssignableFrom(String.class))
        return "string";
    // String Array
    else if (type.isArray())
        return "array";
    // TODO: Recognize plural strings if possible
    //        else if (type.isAssignableFrom(String.class))
    //            return "plural";
    // Boolean
    else if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return "bool";
    // Integer
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class))
        return "integer";
    // Dimen
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return "dimen";

    throw new RuntimeException("No suitable type found for " + field.getName() + "of class " + type.getName());
}

From source file:net.codestory.http.convert.TypeConvert.java

@SuppressWarnings("unchecked")
public static <T> T convert(Context context, Class<T> type) {
    if (type.isAssignableFrom(Context.class)) {
        return (T) context;
    }/*w  w  w .j a  v a 2  s  .c  o  m*/
    if (type.isAssignableFrom(Map.class)) {
        return (T) context.keyValues();
    }
    if (isUrlEncodedForm(context)) {
        return convertValue(context.keyValues(), type);
    }

    String json;
    try {
        json = context.request().getContent();
    } catch (IOException e) {
        throw new IllegalArgumentException("Unable read request content", e);
    }

    return fromJson(json, type);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T objectCast(Object obj, Class<T> classToCast) {
    if (obj == null)
        return null;
    Class<? extends Object> curClass = obj.getClass();
    if (classToCast.isAssignableFrom(curClass))
        return (T) obj;
    return null;/*from  ww w. ja v  a 2  s .c o  m*/
}

From source file:Main.java

/**
 * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super
 * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned,
 * otherwise null. For example ://from w ww  .j a  v a  2  s. co m
 * 
 * <pre>
 * abstract class MyClass implements Serializer&lt;Number&gt; {
 * 
 * }
 * 
 * // type value will be the parameterized type Serializer&lt;Number&gt;
 * Type type = lookupGenericType(Serializer.class, MyClass.class);
 * </pre>
 */
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) {
    if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass))
        return null;
    if (ofClass.equals(inClass))
        return inClass;

    if (ofClass.isInterface()) {
        // lets look if the interface is directly implemented by fromClass
        Class<?>[] interfaces = inClass.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            // do they match?
            if (ofClass.equals(interfaces[i])) {
                return inClass.getGenericInterfaces()[i];
            } else {
                Type superType = lookupGenericType(ofClass, interfaces[i]);
                if (superType != null)
                    return superType;
            }
        }
    }

    // ok it's not one of the directly implemented interfaces, lets try extended class
    Class<?> superClass = inClass.getSuperclass();
    if (ofClass.equals(superClass))
        return inClass.getGenericSuperclass();
    return lookupGenericType(ofClass, inClass.getSuperclass());
}