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:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

private static void addFields(Object target, Class<?> startClass, Class<?> stopClass,
        LinkedHashMap<String, Object> map) {

    if (startClass != stopClass) {
        addFields(target, startClass.getSuperclass(), stopClass, map);
    }//from w w w. j  av  a 2  s  .c  o m

    Field[] fields = startClass.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())
                || Modifier.isPrivate(field.getModifiers())) {
            continue;
        }

        try {
            field.setAccessible(true);
            Object o = field.get(target);
            if (o != null && o.getClass().isArray()) {
                try {
                    o = Arrays.asList((Object[]) o);
                } catch (Throwable e) {
                }
            }
            map.put(field.getName(), o);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}

From source file:io.logspace.it.InfrastructureRule.java

private static Field getField(Class<?> type, String fieldName) {
    Class<?> currentType = type;

    while (currentType != null) {
        try {//from ww  w .  j  a  v  a  2  s . c  o  m
            return currentType.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            // nothing to do
        } catch (SecurityException e) {
            e.printStackTrace();
        }

        currentType = currentType.getSuperclass();
    }

    return null;
}

From source file:com.github.vanroy.springdata.jest.MappingBuilder.java

private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
    // Create list of fields.
    List<java.lang.reflect.Field> fields = new ArrayList<java.lang.reflect.Field>();

    // Keep backing up the inheritance hierarchy.
    Class targetClass = clazz;
    do {/*from   w ww .j  av a  2  s. c  o m*/
        fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return fields.toArray(new java.lang.reflect.Field[fields.size()]);
}

From source file:com.micmiu.mvc.ferriswheel.utils.ReflectionUtils.java

/**
 * ??//from   w w w.  ja v  a  2  s .c  om
 *
 * @param clazz
 * @param index
 * @param <T>
 * @return Class<T>
 */
public static <T> Class<T> findParameterizedType(Class<?> clazz, int index) {
    Type parameterizedType = clazz.getGenericSuperclass();
    //CGLUB subclass target object()
    if (!(parameterizedType instanceof ParameterizedType)) {
        parameterizedType = clazz.getSuperclass().getGenericSuperclass();
    }
    if (!(parameterizedType instanceof ParameterizedType)) {
        return null;
    }
    Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
    if (actualTypeArguments == null || actualTypeArguments.length == 0) {
        return null;
    }
    return (Class<T>) actualTypeArguments[index];
}

From source file:Main.java

/**
 * Finds a method in the given class or any super class with the name {@code prefix + methodName} that accepts 0
 * parameters.//w  w w.  ja  va 2 s .  com
 * 
 * @param aClass
 *          Class to search for method in
 * @param methodName
 *          Camelcase'd method name to search for with any of the provided prefixes
 * @param parameterTypes
 *          The parameter types the method signature must match.
 * @param prefixes
 *          Prefixes to prepend to {@code methodName} when searching for method names, e.g. "get", "is"
 * @return The first method found to match the format {@code prefix + methodName}
 */
public static Method findMethod(Class<?> aClass, String methodName, Class<?>[] parameterTypes,
        String... prefixes) {
    for (String prefix : prefixes) {
        try {
            return aClass.getDeclaredMethod(prefix + methodName, parameterTypes);
        } catch (NoSuchMethodException ex) {
            // ignore, continue searching prefixes
        }
    }
    // If no method found with any prefixes search the super class
    aClass = aClass.getSuperclass();
    return aClass == null ? null : findMethod(aClass, methodName, parameterTypes, prefixes);
}

From source file:Main.java

public static void doGetAllField(Class<?> type, List<Field> fields, boolean ignoreSuperClasses) {
    for (Field newField : type.getDeclaredFields()) {
        boolean found = false;
        for (Field oldField : fields) {
            if (oldField.getName().equals(newField.getName())) {
                found = true;/*ww w.  j  av  a 2s .  c  om*/
                break;
            }
        }
        if (!found) {
            fields.add(newField);
        }
    }

    if (type.getSuperclass() != null && !ignoreSuperClasses) {
        doGetAllField(type.getSuperclass(), fields, ignoreSuperClasses);
    }
}

From source file:org.mayocat.application.AbstractService.java

private static List<Field> getAllFields(Class<?> type) {
    List<Field> fields = new ArrayList<Field>();

    fields.addAll(Arrays.asList(type.getDeclaredFields()));

    if (type.getSuperclass() != null) {
        fields.addAll(getAllFields(type.getSuperclass()));
    }/*from  ww w.  j ava 2s .c  om*/

    return fields;
}

From source file:com.infinira.aerospike.dataaccess.util.Utils.java

public static boolean set(Object object, String fieldName, Object fieldValue) {
    Assert.notNull(object, "Object cannot be null.");
    Assert.notNull(fieldName, "Fieldname cannot be null.");
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {//from   ww  w .j a  va  2s  . c  o m
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(object, fieldValue);
            return true;
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return false;
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Returns list of all methods in a class
 * /*from ww  w  .j  a  v a  2  s .  c  om*/
 * @param entityClass class
 * @return list of all methods in a class
 */
private static List<Method> getMethods(Class<?> entityClass) {
    List<Method> result = new ArrayList<Method>(Arrays.asList(entityClass.getDeclaredMethods()));
    if (!entityClass.getSuperclass().equals(Object.class)) {
        result.addAll(getMethods(entityClass.getSuperclass()));
    }

    return result;
}

From source file:ch.algotrader.util.FieldUtil.java

/**
 * Returns all non-static / non-transient Fields including Fields defined by superclasses of the defined {@code type}
 *///from  w  w w  .j a va  2s. c  o  m
public static List<Field> getAllFields(Class<?> type) {

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

    while (true) {

        for (Field field : type.getDeclaredFields()) {
            if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) {
                setAccessible(field);
                fields.add(field);
            }
        }

        type = type.getSuperclass();
        if (type == Object.class || type == null) {
            break;
        }
    }

    return fields;
}