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

public static <I, P extends I> Method findMethod(final Object obj, final String name, final P param,
        final Class<I> type) throws NoSuchMethodException {

    Class<?> c = obj.getClass();

    while (c != null && c != Object.class) {
        try {// w ww .ja v a 2 s.co  m
            return c.getDeclaredMethod(name, type);
        } catch (final Exception e) {
        }
        c = c.getSuperclass();
    }

    throw new NoSuchMethodException();

}

From source file:com.luna.common.utils.ReflectUtils.java

public static Class<?> getOriginalClass(Class<?> clz) {
    Class<?> superclass = clz;

    while (superclass.getName().indexOf("_$$_jvst") != -1) {
        superclass = superclass.getSuperclass();

        if (superclass == null) {
            return superclass;
        }//from  w ww.  j ava2 s. c o m
    }

    return superclass;
}

From source file:Main.java

public static Field getFieldRecursiveLy(Class<?> clazz, String fieldName) {
    Field f = null;//from  ww w.j a  va  2  s .  c  om
    try {
        f = clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superClazz;
        while ((superClazz = clazz.getSuperclass()) != null && superClazz != Object.class) {
            try {
                f = superClazz.getDeclaredField(fieldName);
                break;
            } catch (Exception e2) {
                //ignore
            }
        }
    }
    if (f == null) {
        throw new RuntimeException(
                "can't find the field , class = " + clazz.getName() + " , fieldName = " + fieldName);
    }
    f.setAccessible(true);
    return f;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getParentClassFields(Map<String, Class> map, Class clazz) {

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());

    }//from  www . java  2 s  .c  o  m

    if (clazz.getSuperclass() == null) {

        return map;

    }

    getParentClassFields(map, clazz.getSuperclass());

    return map;

}

From source file:com.github.yongchristophertang.config.PropertyHandler.java

private static <A extends Annotation> A[] getAnnotations(A[] annos, Class<?> testClass, Class<A> annoClass) {
    return testClass == Object.class ? annos
            : getAnnotations(ArrayUtils.addAll(annos, testClass.getAnnotationsByType(annoClass)),
                    testClass.getSuperclass(), annoClass);
}

From source file:jp.co.ctc_g.jfw.core.util.Classes.java

/**
 * ???????????????/*from   w w w  . ja v a2  s.c  o  m*/
 * ?????????
 * ??????
 * <pre class="brush:java">
 * Classes.howFar(Object.class, Object.class); // 0
 * Classes.howFar(String.class, Object.class); // 1
 * Classes.howFar(JPanel.class, Object.class); // 4
 * Classes.howFar(JPanel.class, JComponent.class); // 1
 * </pre>
 * ?????????<code>-1</code>?????
 * @param from ??
 * @param to ??
 * @return ??
 */
public static int howFar(Class<?> from, Class<?> to) {
    Args.checkNotNull(from);
    Args.checkNotNull(to);
    if (from == to)
        return 0;
    if (!to.isAssignableFrom(from))
        return -1;
    int distance = 0;
    Class<?> hierarchyExplorer = from;
    while (hierarchyExplorer != null && hierarchyExplorer != to) {
        hierarchyExplorer = hierarchyExplorer.getSuperclass();
        if (hierarchyExplorer != null) {
            distance++;
        } else {
            // ???????????????
            assert false;
        }
    }
    return distance;
}

From source file:Main.java

public static Field getField(Class<?> clazz, String fieldName) {
    final Field[] fields = clazz.getDeclaredFields();
    final Field field = getField(fields, fieldName);
    if (field == null) {
        final Class<?> superClass = clazz.getSuperclass();
        if (Object.class.equals(superClass)) {
            return null;
        }// ww w.  j a  va  2  s. c om
        return getField(superClass, fieldName);
    }
    return field;
}

From source file:Main.java

/**
 * Gets the given class's {@link Field}s marked with the annotation of the
 * specified class./*from  w  ww . j a  va  2 s  .  co m*/
 * <p>
 * Unlike {@link Class#getFields()}, the result will include any non-public
 * fields, including fields defined in supertypes of the given class.
 * </p>
 * 
 * @param c The class to scan for annotated fields.
 * @param annotationClass The type of annotation for which to scan.
 * @param fields The list to which matching fields will be added.
 */
public static <A extends Annotation> void getAnnotatedFields(final Class<?> c, final Class<A> annotationClass,
        final List<Field> fields) {
    if (c == null)
        return;

    // check supertypes for annotated fields first
    getAnnotatedFields(c.getSuperclass(), annotationClass, fields);
    for (final Class<?> iface : c.getInterfaces()) {
        getAnnotatedFields(iface, annotationClass, fields);
    }

    for (final Field f : c.getDeclaredFields()) {
        final A ann = f.getAnnotation(annotationClass);
        if (ann != null)
            fields.add(f);
    }
}

From source file:br.gov.frameworkdemoiselle.util.contrib.Reflections.java

/**
 * Build a array of super classes fields
 * //from w  w w .  j  av a 2s.c o m
 * @return Array of Super Classes Fields
 */
public static Field[] getSuperClassesFields(Class<?> entryClass) {
    Field[] fieldArray = null;
    fieldArray = (Field[]) ArrayUtils.addAll(fieldArray, entryClass.getDeclaredFields());
    Class<?> superClazz = entryClass.getSuperclass();
    while (superClazz != null && !"java.lang.Object".equals(superClazz.getName())) {
        fieldArray = (Field[]) ArrayUtils.addAll(fieldArray, superClazz.getDeclaredFields());
        superClazz = superClazz.getSuperclass();
    }
    return fieldArray;
}

From source file:io.fabric8.cxf.endpoint.BeanValidationAnnotationIntrospector.java

protected static Field findField(String fieldName, Class<?> declaringClass) {
    try {/*from  www .ja v  a  2s  . c om*/
        return declaringClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superclass = declaringClass.getSuperclass();
        if (superclass != null && superclass != declaringClass) {
            return findField(fieldName, superclass);
        } else {
            return null;
        }
    }
}