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.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java

/**
 * Obtain a list of field declared by a bean class and superclasses
 * /*from  w ww.  j  a  v  a 2  s  .com*/
 * @param bean
 *            Bean to get the fields
 * @return Array of fields declared by bean class and superclasses
 * @throws Exception
 */
private static Field[] getBeanFields(AbstractBean bean) throws Exception {
    // Fields added to AbstractBean by ConverterAspect
    String[] ignoreFields = ((Detachable) bean).getDetachableFields();
    Class<?> clazz = bean.getClass();
    // Get the all fields declared by the class
    Field[] fields = clazz.getDeclaredFields();
    // For all superclasses until arrive to AbstractBean (including
    // AbstractBean)...
    while (clazz.getSuperclass() != null && AbstractBean.class.isAssignableFrom(clazz.getSuperclass())) {
        clazz = clazz.getSuperclass();
        // Add the clazz declared fields to field list
        fields = (Field[]) ArrayUtils.addAll(fields, clazz.getDeclaredFields());
    }
    // Remove from the list fields added by the converters aspect to
    // Abstract
    // bean
    for (Field field : fields) {
        if (ArrayUtils.contains(ignoreFields, field.getName())) {
            fields = (Field[]) ArrayUtils.removeElement(fields, field);
        }
    }
    return fields;
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)/*from  w w  w.j av a2s .c  o  m*/
 * 
 * @param clazz
 *            class
 * @return List<Field>
 */
@SuppressWarnings({ "rawtypes" })
public static Map<Class, Field[]> getAllFields(Class clazz) {
    Map<Class, Field[]> map = new LinkedHashMap<Class, Field[]>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        map.put(searchClazz, fields);
        searchClazz = searchClazz.getSuperclass();
    }
    return map;
}

From source file:com.manydesigns.portofino.logic.SecurityLogic.java

public static boolean satisfiesRequiresAdministrator(HttpServletRequest request, ActionBean actionBean,
        Method handler) {//w  w w  .j a v a 2  s.  co m
    logger.debug("Checking if action or method required administrator");
    boolean requiresAdministrator = false;
    if (handler.isAnnotationPresent(RequiresAdministrator.class)) {
        logger.debug("Action method requires administrator: {}", handler);
        requiresAdministrator = true;
    } else {
        Class actionClass = actionBean.getClass();
        while (actionClass != null) {
            if (actionClass.isAnnotationPresent(RequiresAdministrator.class)) {
                logger.debug("Action class requires administrator: {}", actionClass);
                requiresAdministrator = true;
                break;
            }
            actionClass = actionClass.getSuperclass();
        }
    }

    boolean isNotAdmin = !isAdministrator(request);
    boolean doesNotSatisfy = requiresAdministrator && isNotAdmin;
    if (doesNotSatisfy) {
        logger.info("User is not an administrator");
        return false;
    }
    return true;
}

From source file:Main.java

public static Field[] getAllFiedFromClassAndSuper(Class clazz, boolean needStatic) {
    ArrayList<Field> fields = new ArrayList<>();
    if (clazz != null) {
        Field[] classFields = clazz.getDeclaredFields();
        if (classFields != null) {
            for (Field field : classFields) {
                boolean isStatic = Modifier.isStatic(field.getModifiers());
                if (isStatic && !needStatic) {
                    continue;
                }/*from www . ja  v  a2 s  .  c  o  m*/
                fields.add(field);
            }
        }

        Field[] superFields = getAllFiedFromClassAndSuper(clazz.getSuperclass(), needStatic);
        if (superFields != null) {
            for (Field field : superFields) {
                boolean isStatic = Modifier.isStatic(field.getModifiers());
                if (isStatic && !needStatic) {
                    continue;
                }
                fields.add(field);
            }
        }
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:com.ocs.dynamo.test.MockUtil.java

/**
 * Registers all fields that are annotated with "@Mock" as beans in the Spring context
 * //from   ww  w  .  ja  v  a2  s.  co m
 * @param factory
 * @param subject
 * @param clazz
 */
public static void registerMocks(ConfigurableListableBeanFactory factory, Object subject, Class<?> clazz) {
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getAnnotation(Mock.class) != null) {
                factory.registerSingleton(field.getName(), field.get(subject));
            }
        }
        if (clazz.getSuperclass() != null) {
            registerMocks(factory, subject, clazz.getSuperclass());
        }
    } catch (Exception e) {
        throw new OCSRuntimeException(e.getMessage(), e);
    }
}

From source file:com.qualogy.qafe.core.errorhandling.ErrorProcessor.java

private static Map<String, Class<?>> resolveExceptionClassHierarchy(ExternalException externalException) {
    Map<String, Class<?>> exceptionClassHierarchy = new LinkedHashMap<String, Class<?>>();
    if (externalException != null) {
        Class<?> exceptionClass = externalException.getCause().getClass();
        while (exceptionClass != null) {
            String qualifiedClassName = exceptionClass.getName();
            exceptionClassHierarchy.put(qualifiedClassName, exceptionClass);
            exceptionClass = exceptionClass.getSuperclass();
            if (exceptionClass.equals(Object.class)) {
                exceptionClass = null;/*from w  w  w  .  ja v  a2s  .  co  m*/
            }
        }
    }
    return exceptionClassHierarchy;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Gets all fields from a class. Keys are the field names as strings.
 *
 * @param type the type//  w  w  w . ja v a  2s .  co  m
 * @return the all fields
 */
//CHECKSTYLE.OFF FinalParameter Precondition cast.
public static Map<String, Field> getAllFields(final Class<?> type) {

    Map<String, Field> fieldMap = new HashMap<>();
    for (Field field : type.getDeclaredFields()) {
        fieldMap.put(field.getName(), field);
    }
    Class<?> nextType = type.getSuperclass();

    while (nextType != Object.class) {
        for (Field field : nextType.getDeclaredFields()) {
            fieldMap.put(field.getName(), field);
        }
        nextType = nextType.getSuperclass();
    }
    //CHECKSTYLE.ON
    return fieldMap;
}

From source file:com.nortal.petit.beanmapper.BeanMappingReflectionUtils.java

private static void readAnnotations(List<Annotation> l, Class<?> type, String name) {
    Column ao = getAttributeOverride(type, name);
    if (ao != null) {
        l.add(ao);//w  w  w .j av  a2 s . c o m
    }
    Field field = FieldUtils.getDeclaredField(type, name, true);
    if (field != null) {
        addAll(l, field.getAnnotations());
    }
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name);
    if (pd != null) {
        if (pd.getReadMethod() != null) {
            addAll(l, pd.getReadMethod().getAnnotations());
        }
    }
    if (type.getSuperclass() != null) {
        readAnnotations(l, type.getSuperclass(), name);
    }
}

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

/**
 * This checks class name to determine if a given class is subtype of the
 * class name;/* ww w.  j  a va  2 s  .c  om*/
 *
 * @param className
 * @param entityClass
 * @return
 */
public static boolean isSubClass(String className, Class entityClass) {
    if (entityClass == null) {
        return false;
    }

    if (className.equals(entityClass.getSimpleName())) {
        return true;
    } else {
        return isSubClass(className, entityClass.getSuperclass());
    }
}

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

/**
 * obtain constructor list of specified class If recursively is true, obtain
 * constructor from all class hierarchy/*from w w  w .  j  a  va 2s .  c  o  m*/
 * 
 * 
 * @param clazz class
 *            where fields are searching
 * @param recursively
 *            param
 * @return array of constructors
 */
public static Constructor<?>[] getDeclaredConstructors(Class<?> clazz, boolean recursively) {
    List<Constructor<?>> constructors = new LinkedList<Constructor<?>>();
    Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
    Collections.addAll(constructors, declaredConstructors);

    Class<?> superClass = clazz.getSuperclass();

    if (superClass != null && recursively) {
        Constructor<?>[] declaredConstructorsOfSuper = getDeclaredConstructors(superClass, true);
        if (declaredConstructorsOfSuper.length > 0)
            Collections.addAll(constructors, declaredConstructorsOfSuper);
    }
    return constructors.toArray(new Constructor<?>[constructors.size()]);
}