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

/**
 * Returns an array of the superclasses of cls.
 * /*from   w  ww  .  j  a  va2 s.  co m*/
 * @return java.lang.Class[]
 * @param cls
 *            java.lang.Class
 */
//start extract getSuperclasses
public static Class[] getSuperclasses(Class cls) {
    int i = 0;
    for (Class x = cls.getSuperclass(); x != null; x = x.getSuperclass())
        i++;
    Class[] result = new Class[i];
    i = 0;
    for (Class x = cls.getSuperclass(); x != null; x = x.getSuperclass())
        result[i++] = x;
    return result;
}

From source file:Main.java

public static Field getDeclaredField(Object object, String fieldName) {
    Class<?> clz = object.getClass();
    for (; clz != Object.class; clz = clz.getSuperclass()) {
        try {//w  ww  .  ja  v  a2  s .  c o m
            Field field = clz.getDeclaredField(fieldName);
            return field;
        } catch (Exception e) {
        }
    }
    return null;
}

From source file:Main.java

private static void printAncestor(Class<?> c, List<Class> l) {
    Class<?> ancestor = c.getSuperclass();
    if (ancestor != null) {
        l.add(ancestor);/*  w  w  w  . j ava  2  s.c o m*/
        printAncestor(ancestor, l);
    }
}

From source file:Main.java

public static Method getMethod(Class<?> classObject, String methodName, Class... parametersType) {
    Class sCls = classObject.getSuperclass();

    while (sCls != Object.class) {
        try {//from  w  ww.j  av a 2s  .com
            return sCls.getDeclaredMethod(methodName, parametersType);
        } catch (NoSuchMethodException var5) {
            sCls = sCls.getSuperclass();
        }
    }

    throw new RuntimeException("Method not found " + methodName);
}

From source file:Util.java

private static void fillSetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
    if (pojoClass.getSuperclass() != Object.class)
        fillSetterMethods(pojoClass.getSuperclass(), baseMap);

    Method[] methods = pojoClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];/*  w  ww  .j av  a2  s .  co  m*/
        if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 1
                && m.getName().startsWith(SET) && Modifier.isPublic(m.getModifiers())) {
            baseMap.put(toProperty(SET.length(), m.getName()), m);
        }
    }
}

From source file:Main.java

/**
 * Extract all the fields of an object and its parent classes' fields iterative.
 * @param fields that belong to the class declaration.
 * @param type of the class from where extract the fields.
 * @return list of the fields fo the type class.
 *//*from   w w  w  .  j av a 2s .c  o m*/
private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
    Collections.addAll(fields, type.getDeclaredFields());

    if (type.getSuperclass() != null) {
        fields = getAllFields(fields, type.getSuperclass());
    }

    return fields;
}

From source file:Util.java

private static void fillGetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
    if (pojoClass.getSuperclass() != Object.class)
        fillGetterMethods(pojoClass.getSuperclass(), baseMap);

    Method[] methods = pojoClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];//from w ww .j  a v  a 2s . c  om
        if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0
                && m.getReturnType() != null && Modifier.isPublic(m.getModifiers())) {
            String name = m.getName();
            if (name.startsWith(IS))
                baseMap.put(toProperty(IS.length(), name), m);
            else if (name.startsWith(GET))
                baseMap.put(toProperty(GET.length(), name), m);
        }
    }
}

From source file:Main.java

/**
 * Checks if a class is a subclass of a class with the specified name. Used
 * as an instanceOf without having to load the class, useful when trying to
 * check for classes that might not be available in the runtime JRE.
 * /*from   w ww  .j ava 2 s  .  co m*/
 * @param clazz
 *            The class to check
 * @param className
 *            The class name to look for in the super classes
 * @return true if the class extends a class by the specified name.
 */
public static boolean extendsClass(final Class<?> clazz, String className) {
    Class<?> superClass = clazz.getSuperclass();

    while (superClass != null) {
        if (superClass.getName().equals(className)) {
            return true;
        }
        superClass = superClass.getSuperclass();

    }
    return false;
}

From source file:Main.java

public static Method safelyGetSuperclassMethod(Class<?> cls, String methodName, Class<?>... parametersType) {
    Class<?> sCls = cls.getSuperclass();
    while (sCls != Object.class) {
        try {//  w w w .  j a v  a2s. c o m
            return sCls.getDeclaredMethod(methodName, parametersType);
        } catch (NoSuchMethodException e) {
            // Just super it again
        }
        sCls = sCls.getSuperclass();
    }
    throw new RuntimeException("Method not found " + methodName);
}

From source file:Main.java

/**
 * Determine the common ancestor of the given classes, if any.
 *
 * @param clazz1 the class to introspect
 * @param clazz2 the other class to introspect
 * @return the common ancestor (i.e. common superclass, one interface
 * extending the other), or {@code null} if none found. If any of the
 * given classes is {@code null}, the other class will be returned.
 * @since 3.2.6//from  w  w w  . j  a v  a 2 s  .c  o m
 */
public static Class<?> determineCommonAncestor(Class<?> clazz1, Class<?> clazz2) {
    if (clazz1 == null) {
        return clazz2;
    }
    if (clazz2 == null) {
        return clazz1;
    }
    if (clazz1.isAssignableFrom(clazz2)) {
        return clazz1;
    }
    if (clazz2.isAssignableFrom(clazz1)) {
        return clazz2;
    }
    Class<?> ancestor = clazz1;
    do {
        ancestor = ancestor.getSuperclass();
        if (ancestor == null || Object.class.equals(ancestor)) {
            return null;
        }
    } while (!ancestor.isAssignableFrom(clazz2));
    return ancestor;
}