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:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Set all String field to empty on current class.
 *
 * @param object the object/*  ww w.j a  v  a  2 s.co m*/
 * @param exceptClass the except class
 * @throws IllegalArgumentException the illegal argument exception
 * @throws IllegalAccessException the illegal access exception
 */
// CHECKSTYLE.OFF com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck Trivial code.
public static void fillDefaultEmptyFieldsIfEmpty(Object object, Class<?>... exceptClass) // NOSONAR "Methods should not be too complex" trivial
        throws IllegalArgumentException, IllegalAccessException {
    Class<?> currentClazz = object.getClass();
    List exClazzes = Arrays.asList(exceptClass);
    while (currentClazz.getSuperclass() != null) {
        if (exClazzes.contains(currentClazz) == false) {
            Field[] fields = currentClazz.getDeclaredFields();
            for (Field field : fields) {
                if (String.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, "");
                    }
                }
                if (Integer.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, 0);
                    }
                }
                if (BigDecimal.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, BigDecimal.ZERO);
                    }
                }
                if (Date.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, new Date());
                    }
                }
            }
        }
        currentClazz = currentClazz.getSuperclass();
    }
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find getter method in handler class or any of its superclasses
 * /*from ww w.j a  v  a2 s  .  co  m*/
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertyGetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = getterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertyGetter ann = method.getAnnotation(PropertyGetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    getterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find setter method in handler class or any of its superclasses
 * /*ww  w  .  j ava 2s .c om*/
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertySetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = setterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertySetter ann = method.getAnnotation(PropertySetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    setterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain field If recursively is true, obtain fields from all class
 * hierarchy/* w w w. ja v a  2s .c om*/
 * 
 * @param clazz class
 *            where fields are searching
 * @param fieldName
 *            field name
 * @param recursively
 *            param
 * @return list of fields
 */
public static Field getDeclaredField(Class<?> clazz, String fieldName, boolean recursively) {
    try {
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && recursively) {
            return getDeclaredField(superClass, fieldName, true);
        }
    } catch (SecurityException e) {
        log.error("{}", e.getMessage(), e);
    }
    return null;

}

From source file:edu.usu.sdl.openstorefront.util.ReflectionUtil.java

/**
 * This gets all declared field of the whole object hierarchy
 *
 * @param typeClass/*from   www  .  ja  v a 2 s .com*/
 * @return
 */
public static List<Field> getAllFields(Class typeClass) {
    Objects.requireNonNull(typeClass, "Class is required");

    List<Field> fields = new ArrayList<>();
    if (typeClass.getSuperclass() != null) {
        fields.addAll(getAllFields(typeClass.getSuperclass()));
    }
    for (Field field : typeClass.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers()) == false
                && Modifier.isFinal(field.getModifiers()) == false) {
            fields.add(field);
        }
    }
    return fields;
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

/**
 * http://stackoverflow.com/questions/1042798/retrieving-the-inherited-attribute-names-values-using-java-reflection
 * @param fields/*from   ww w .j a  v a  2 s  .  c o  m*/
 * @param c
 * @return
 */
public static List<Field> getAllFields(List<Field> fields, Class<?> c) {
    fields.addAll(Arrays.asList(c.getDeclaredFields()));

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

    return fields;
}

From source file:org.mybatisorm.annotation.handler.TableHandler.java

public static List<Field> getFields(Class<?> clazz) {
    List<Field> fields = new ArrayList<Field>();
    Class<?> superClass = clazz.getSuperclass();
    if (TableHandler.hasAnnotation(superClass)) {
        fields.addAll(getFields(clazz.getSuperclass()));
    }/*from  w w w.  j a v  a 2 s.  com*/
    fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    return fields;
}

From source file:app.commons.ReflectionUtils.java

/**
 * We cannot use <code>clazz.getMethod(String name, Class<?>... parameterTypes)</code> because it only returns public methods.
 *//*ww w  . j av  a  2 s .com*/
private static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes) {
    Class currentClass = clazz;
    while (currentClass != null) {
        try {
            Method method = currentClass.getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            return method;
        } catch (NoSuchMethodException e) {
            currentClass = currentClass.getSuperclass();
        }
    }
    throw new RuntimeException(new NoSuchMethodException(methodName));
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Returns the annotation on the given class or the package of the class. This searchs up the
 * class hierarchy and the package hierarchy for the closest match.
 *
 * @param   <T> class type/*from w w  w .jav  a2 s  .  c om*/
 * @param   clazz The class to search for the annotation.
 * @param   annotationClass The Class of the annotation.
 * @return  The annotation or null.
 */
public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
    T ann = clazz.getAnnotation(annotationClass);
    while (ann == null && clazz != null) {
        ann = clazz.getAnnotation(annotationClass);
        if (ann == null) {
            ann = clazz.getPackage().getAnnotation(annotationClass);
        }
        if (ann == null) {
            clazz = clazz.getSuperclass();
            if (clazz != null) {
                ann = clazz.getAnnotation(annotationClass);
            }
        }
    }

    return ann;
}

From source file:com.evolveum.midpoint.schema.constants.ObjectTypes.java

@SuppressWarnings("unchecked")
public static ObjectTypes getObjectType(Class<? extends ObjectType> objectType) {
    for (ObjectTypes type : values()) {
        if (type.getClassDefinition().equals(objectType)) {
            return type;
        }/*from w  w  w. j a  v  a 2 s  .  com*/
    }
    // No match. Try with superclass.
    Class<?> superclass = objectType.getSuperclass();
    if (superclass != null && !superclass.equals(ObjectType.class)) {
        return getObjectType((Class<? extends ObjectType>) superclass);
    }

    throw new IllegalArgumentException("Unsupported object type " + objectType);
}