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:com.hortonworks.registries.common.util.ReflectionHelper.java

/**
 * Given a class, this method returns a map of names of all the instance (non static) fields to type.
 * if the class has any super class it also includes those fields.
 * @param clazz , not null/*  w  w  w .j  a  va2s.  c  o  m*/
 * @return
 */
public static Map<String, Class> getFieldNamesToTypes(Class clazz) {
    Field[] declaredFields = clazz.getDeclaredFields();
    Map<String, Class> instanceVariableNamesToTypes = new HashMap<>();
    for (Field field : declaredFields) {
        if (!Modifier.isStatic(field.getModifiers())) {
            LOG.trace("clazz {} has field {} with type {}", clazz.getName(), field.getName(),
                    field.getType().getName());
            instanceVariableNamesToTypes.put(field.getName(), field.getType());
        } else {
            LOG.trace("clazz {} has field {} with type {}, which is static so ignoring", clazz.getName(),
                    field.getName(), field.getType().getName());
        }
    }

    if (!clazz.getSuperclass().equals(Object.class)) {
        instanceVariableNamesToTypes.putAll(getFieldNamesToTypes(clazz.getSuperclass()));
    }
    return instanceVariableNamesToTypes;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Field findField(Class<?> clazz, String propName) {
    Class<?> c = clazz;
    while (c != null) {
        for (Field f : c.getDeclaredFields()) {
            if (!f.getName().equals(propName)) {
                continue;
            }/*w  ww.ja v a2  s . co m*/
            int modifiers = f.getModifiers();
            if (Modifier.isStatic(modifiers)) {
                continue;
            }
            return f;
        }
        c = c.getSuperclass();
    }
    return null;
}

From source file:antre.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*w  w  w  .j  a  v a 2  s.c o  m*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        if (cacheEnabled)
            typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:com.qingstor.sdk.utils.QSJSONUtil.java

public static boolean jsonObjFillValue2Object(JSONObject o, Object targetObj) {
    boolean hasParam = false;
    if (targetObj != null) {
        try {//  w  w w  . ja v a  2  s  .  com
            Class tmpClass = targetObj.getClass();
            while (tmpClass != Object.class) {
                Field[] fields = tmpClass.getDeclaredFields();
                if (initParameter(o, fields, tmpClass, targetObj)) {
                    hasParam = true;
                }
                tmpClass = tmpClass.getSuperclass();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return hasParam;
}

From source file:Mopex.java

/**
 * Finds the first (from the bottom of the inheritance hierarchy) field with
 * the specified name. Note that Class.getField returns only public fields.
 * //from  w  w  w  .ja va  2s. c o m
 * @return Field
 * @param cls
 *            java.lang.Class
 * @param name
 *            String
 */
//start extract findField
public static Field findField(Class cls, String name) throws NoSuchFieldException {
    if (cls != null) {
        try {
            return cls.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            return findField(cls.getSuperclass(), name);
        }
    } else {
        throw new NoSuchFieldException();
    }
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

/**
 * Find the first {@link Class} in the inheritance hierarchy of the specified
 * {@code clazz} (including the specified {@code clazz} itself) which declares
 * at least one of the specified {@code annotationTypes}, or {@code null} if
 * none of the specified annotation types could be found.
 * <p>If the supplied {@code clazz} is {@code null}, {@code null} will be
 * returned.//w  w  w .  ja v  a2s  .  c  om
 * <p>If the supplied {@code clazz} is an interface, only the interface itself
 * will be checked; the inheritance hierarchy for interfaces will not be traversed.
 * <p>The standard {@link Class} API does not provide a mechanism for determining
 * which class in an inheritance hierarchy actually declares one of several
 * candidate {@linkplain java.lang.annotation.Annotation annotations}, so we need to handle this
 * explicitly.
 * @param annotationTypes the list of Class objects corresponding to the
 * annotation types
 * @param clazz the Class object corresponding to the class on which to check
 * for the annotations, or {@code null}
 * @return the first {@link Class} in the inheritance hierarchy of the specified
 * {@code clazz} which declares an annotation of at least one of the specified
 * {@code annotationTypes}, or {@code null} if not found
 * @since 3.2.2
 * @see Class#isAnnotationPresent(Class)
 * @see Class#getDeclaredAnnotations()
 * @see #findAnnotationDeclaringClass(Class, Class)
 * @see #isAnnotationDeclaredLocally(Class, Class)
 */
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes,
        Class<?> clazz) {
    if (clazz == null || clazz.equals(Object.class)) {
        return null;
    }
    for (Class<? extends Annotation> annotationType : annotationTypes) {
        if (isAnnotationDeclaredLocally(annotationType, clazz)) {
            return clazz;
        }
    }
    return findAnnotationDeclaringClassForTypes(annotationTypes, clazz.getSuperclass());
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

@SuppressWarnings("unchecked")
private static final <T extends AbstractPageObject> void processCCPageFragment(T entity,
        Class<? extends AbstractPageObject> clazz) throws AutomationFrameworkException {
    if (clazz == null)
        return;//w  w w .jav a2 s.  c  om
    Field[] fields = clazz.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        fields[i].setAccessible(true);
        if (fields[i].getAnnotation(CCPageFragment.class) != null) {
            entity.addPageFragment(createPageObjectField(entity, fields[i]));
        }
    }
    processCCPageFragment(entity, (Class<? extends AbstractPageObject>) clazz.getSuperclass());
}

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

public static Class getClassOfTableAnnotation(Class type) {
    do {//from www  .j  av  a 2  s.  c  om
        if (type.isAnnotationPresent(javax.persistence.Table.class)) {
            return type;
        }
    } while ((type = type.getSuperclass()) != null);

    return null;
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

private static Field getFieldFromBean(Class<?> clazz, String fieldName, final Class<?> originalClass) {
    try {/* ww w .j  av  a2  s .  co m*/
        Field field = clazz.getDeclaredField(fieldName);
        // Allow access to private instance var's that dont have public setter.
        field.setAccessible(true);
        return field;
    } catch (NoSuchFieldException e) {
        if (clazz.getSuperclass() != null) {
            return getFieldFromBean(clazz.getSuperclass(), fieldName, originalClass);
        }
        throw new MappingException("No such field found " + originalClass.getName() + "." + fieldName, e);
    }
}

From source file:cc.kave.commons.utils.json.JsonUtils.java

private static Set<Class<?>> getAllTypesFromHierarchyExceptObject(Class<?> elem, boolean includeElem) {
    Set<Class<?>> hierarchy = Sets.newHashSet();
    if (elem == null || elem.equals(Object.class)) {
        return hierarchy;
    }/*from  w  w  w.j a va2 s .c o  m*/

    if (includeElem) {
        hierarchy.add(elem);
    }

    for (Class<?> i : elem.getInterfaces()) {
        hierarchy.addAll(getAllTypesFromHierarchyExceptObject(i, true));
    }
    hierarchy.addAll(getAllTypesFromHierarchyExceptObject(elem.getSuperclass(), true));

    return hierarchy;
}