Example usage for java.lang Class getSuperclass

List of usage examples for java.lang Class getSuperclass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native Class<? super T> getSuperclass();

Source Link

Document

Returns the Class representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class .

Usage

From source file:edu.mayo.cts2.framework.webapp.rest.view.jsp.Beans.java

/**
 * Inspect./*ww w. ja  va2 s  .  c om*/
 *
 * @param bean the bean
 * @return the list
 */
public static List<Map.Entry<String, Object>> inspect(Object bean) {
    Map<String, Object> props = new LinkedHashMap<String, Object>();

    Class<?> clazz = bean.getClass();

    while (clazz != null) {
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String name = field.getName();

            Object value;
            try {
                value = field.get(bean);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            if (value != null) {
                props.put(name, value);
            }
        }
        clazz = clazz.getSuperclass();
    }

    List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(props.entrySet());

    Collections.sort(list, BEAN_COMPARATOR);

    return list;
}

From source file:Mopex.java

/**
 * Return an array of the supported instance variables of this class. A
 * supported instance variable is not static and is either declared or
 * inherited from a superclass.// w ww.j a  va  2  s . c  o m
 * 
 * @return java.lang.reflect.Field[]
 * @param cls
 *            java.lang.Class
 */
//start extract getSupportedIVS
public static Field[] getSupportedIVs(Class cls) {
    if (cls == null) {
        return new Field[0];
    } else {
        Field[] inheritedIVs = getSupportedIVs(cls.getSuperclass());
        Field[] declaredIVs = getDeclaredIVs(cls);
        Field[] supportedIVs = new Field[declaredIVs.length + inheritedIVs.length];
        for (int i = 0; i < declaredIVs.length; i++) {
            supportedIVs[i] = declaredIVs[i];
        }
        for (int i = 0; i < inheritedIVs.length; i++) {
            supportedIVs[i + declaredIVs.length] = inheritedIVs[i];
        }
        return supportedIVs;
    }
}

From source file:ReflectUtil.java

/**
 * Returns a set of all interfaces implemented by class supplied. This includes all
 * interfaces directly implemented by this class as well as those implemented by
 * superclasses or interface superclasses.
 * /* w w  w.  j  a  v a  2 s  . c  om*/
 * @param clazz
 * @return all interfaces implemented by this class
 */
public static Set<Class<?>> getImplementedInterfaces(Class<?> clazz) {
    Set<Class<?>> interfaces = new HashSet<Class<?>>();

    if (clazz.isInterface())
        interfaces.add(clazz);

    while (clazz != null) {
        for (Class<?> iface : clazz.getInterfaces())
            interfaces.addAll(getImplementedInterfaces(iface));
        clazz = clazz.getSuperclass();
    }

    return interfaces;
}

From source file:com.mastfrog.acteur.ClasspathResourcePage.java

private static Method findMethod(Class<?> on, String name, Class<?>... params) throws SecurityException {
    Class<?> curr = on;
    //NOTE:  Does not check interfaces
    while (curr != Object.class) {
        try {//from   w ww  .j a  v  a2  s .  c  o  m
            return curr.getDeclaredMethod(name, params);
        } catch (NoSuchMethodException ex) {
            //                Logger.getLogger(ClasspathResourcePage.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            curr = curr.getSuperclass();
        }
    }
    return null;
}

From source file:ReflectUtils.java

private static synchronized Method findMethod(Object obj, String property, Object value) {
    Method m = null;//from   w ww . j av a  2  s .c  om
    Class<?> theClass = obj.getClass();
    String setter = String.format("set%C%s", property.charAt(0), property.substring(1));
    Class paramType = value.getClass();
    while (paramType != null) {
        try {
            m = theClass.getMethod(setter, paramType);
            return m;
        } catch (NoSuchMethodException ex) {
            // try on the interfaces of this class
            for (Class iface : paramType.getInterfaces()) {
                try {
                    m = theClass.getMethod(setter, iface);
                    return m;
                } catch (NoSuchMethodException ex1) {
                }
            }
            paramType = paramType.getSuperclass();
        }
    }
    return m;
}

From source file:org.jdbcluster.JDBClusterUtil.java

/**
 * tries to validate class instances through Super Classes
 * /*from  ww  w.j av a2  s  . c om*/
 * @param count
 *            recursive parameter. Use 0 for the first call
 * @param superClass
 *            the Superclass to validate against
 * @param clazz
 *            the class instance
 * @return -1 for not found. Counter for supercasses above parameter
 *         superClass
 */
static private int countSuperClass(int count, Class<?> superClass, Class<?> clazz) {
    if (clazz == null)
        return -1;
    if (superClass == clazz)
        return count;
    return countSuperClass(++count, superClass, clazz.getSuperclass());
}

From source file:com.bosscs.spark.commons.utils.Utils.java

/**
 * Returns an instance clone.//from  ww w  . j  a  v  a  2s.co m
 * this method gets every class property by reflection, including its parents properties
 * @param t
 * @param <T>
 * @return T object.
 */
public static <T> T cloneObjectWithParents(T t) throws IllegalAccessException, InstantiationException {
    T clone = (T) t.getClass().newInstance();

    List<Field> allFields = new ArrayList<>();

    Class parentClass = t.getClass().getSuperclass();

    while (parentClass != null) {
        Collections.addAll(allFields, parentClass.getDeclaredFields());
        parentClass = parentClass.getSuperclass();
    }

    Collections.addAll(allFields, t.getClass().getDeclaredFields());

    for (Field field : allFields) {
        int modifiers = field.getModifiers();
        //We skip final and static fields
        if ((Modifier.FINAL & modifiers) != 0 || (Modifier.STATIC & modifiers) != 0) {
            continue;
        }
        field.setAccessible(true);

        Object value = field.get(t);

        if (Collection.class.isAssignableFrom(field.getType())) {
            Collection collection = (Collection) field.get(clone);
            if (collection == null) {
                collection = (Collection) field.get(t).getClass().newInstance();
            }
            collection.addAll((Collection) field.get(t));
            value = collection;
        } else if (Map.class.isAssignableFrom(field.getType())) {
            Map clonMap = (Map) field.get(t).getClass().newInstance();
            clonMap.putAll((Map) field.get(t));
            value = clonMap;
        }
        field.set(clone, value);
    }

    return clone;
}

From source file:org.hawkular.bus.common.AbstractMessage.java

private static Method findBuildObjectMapperForDeserializationMethod(Class<? extends BasicMessage> clazz) {
    try {//from   w ww  . j a va 2 s  .  c o m
        Method m = clazz.getDeclaredMethod("buildObjectMapperForDeserialization");
        return m;
    } catch (NoSuchMethodException e) {
        // the given subclass doesn't have a method to build a mapper, maybe its superclass does.
        // eventually we'll get to the AbstractMessage class and we know it does have one.
        return findBuildObjectMapperForDeserializationMethod(
                (Class<? extends BasicMessage>) clazz.getSuperclass());
    }
}

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));
    }// w  w  w.ja  v  a  2s . 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:gov.nih.nci.iso21090.hibernate.property.CollectionPropertyAccessor.java

/**
 * @param theClass Target class in which the value is to be set
 * @param propertyName Target property name
 * @return returns Setter class instance
 *//*from   www  . j  a va 2 s  . co  m*/
@SuppressWarnings("PMD.AccessorClassGeneration")
private static CollectionPropertySetter getSetterOrNull(Class theClass, String propertyName) {

    if (theClass == Object.class || theClass == null) {
        return null;
    }

    Method method = setterMethod(theClass, propertyName);

    if (method != null) {
        if (!ReflectHelper.isPublic(theClass, method)) {
            method.setAccessible(true);
        }
        return new CollectionPropertySetter(theClass, method, propertyName);
    } else {
        CollectionPropertySetter setter = getSetterOrNull(theClass.getSuperclass(), propertyName);
        if (setter == null) {
            Class[] interfaces = theClass.getInterfaces();
            for (int i = 0; setter == null && i < interfaces.length; i++) {
                setter = getSetterOrNull(interfaces[i], propertyName);
            }
        }
        return setter;
    }
}