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:architecture.common.util.ClassUtils.java

/**
 * Finds all super classes and interfaces for a given class
 * //from  w w  w. j  a  v a 2s  .c  o  m
 * @param cls
 *            The class to scan
 * @param types
 *            The collected related classes found
 */
public static void findAllTypes(Class cls, Set<Class> types) {
    if (cls == null) {
        return;
    }

    // check to ensure it hasn't been scanned yet
    if (types.contains(cls)) {
        return;
    }

    types.add(cls);

    findAllTypes(cls.getSuperclass(), types);
    for (int x = 0; x < cls.getInterfaces().length; x++) {
        findAllTypes(cls.getInterfaces()[x], types);
    }
}

From source file:com.abiquo.model.util.ModelTransformer.java

public static <T> void transform(final Class sourceClass, final Class<T> targetClass, final Object source,
        final T target) throws Exception {
    Field[] transportFields = sourceClass.getDeclaredFields();
    Class superClass = sourceClass.getSuperclass();
    while (!superClass.getSimpleName().equalsIgnoreCase("SingleResourceTransportDto")) {
        transportFields = (Field[]) ArrayUtils.addAll(transportFields, superClass.getDeclaredFields());
        superClass = superClass.getSuperclass();
    }//  w w w.j a  v  a 2s  . com

    for (Field field : transportFields) {

        int modifiers = field.getModifiers();
        if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
            String name = field.getName();
            try {
                if (fieldExist(name, targetClass) && fieldExist(name, source.getClass())
                        || getterExist(name, source.getClass())
                                && setterExist(name, targetClass, field.getType())) {
                    Object value = getter(name, source.getClass()).invoke(source, new Object[0]);

                    if (setterExist(name, targetClass, field.getType())) {
                        setter(name, targetClass, field.getType()).invoke(target, new Object[] { value });
                    }
                }
            } catch (InvocationTargetException e) {
                // Ignore invalid field
            }
        }

    }

}

From source file:com.github.juanmf.java2plant.Parser.java

protected static void addExtensions(Set<Relation> relations, Class<?> fromType) {
    Class<?> superclass = fromType.getSuperclass();
    if (null == superclass || Object.class.equals(superclass)) {
        return;/*from  ww  w.  j a v a  2  s.  c  o m*/
    }
    Relation extension = new Extension(fromType, superclass.getName());
    relations.add(extension);
}

From source file:com.evolveum.midpoint.prism.xml.XsdTypeMapper.java

public static QName getJavaToXsdMapping(Class<?> type) {
    if (javaToXsdTypeMap.containsKey(type)) {
        return javaToXsdTypeMap.get(type);
    }/*  w w w  . j  a  v  a 2  s . co  m*/
    Class<?> superType = type.getSuperclass();
    if (superType != null) {
        return getJavaToXsdMapping(superType);
    }
    return null;
}

From source file:Main.java

public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> aClazz) {
    //Check class hierarchy
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        T anno = c.getAnnotation(aClazz);
        if (anno != null) {
            return anno;
        }/*from  ww w  .  j  a  v  a 2 s .  c  o m*/
    }

    //Check interfaces (breadth first)
    Queue<Class<?>> q = new LinkedList<Class<?>>();
    q.add(clazz);
    while (!q.isEmpty()) {
        Class<?> c = q.remove();
        if (c != null) {
            if (c.isInterface()) {
                T anno = c.getAnnotation(aClazz);
                if (anno != null) {
                    return anno;
                }
            } else {
                q.add(c.getSuperclass());
            }
            q.addAll(Arrays.asList(c.getInterfaces()));
        }
    }

    return null;
}

From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java

public static List<EntityDocModel> processEntites(List<Class> entities) {
    List<EntityDocModel> entityDocModels = new ArrayList<>();

    log.log(Level.FINEST, "Construct Entities");
    for (Class entity : entities) {
        EntityDocModel docModel = createEntityModel(entity);
        addSuperClass(entity.getSuperclass(), docModel);
        for (Class interfaceClass : entity.getInterfaces()) {
            docModel.getImplementedEntities().add(createEntityModel(interfaceClass));
        }//from   ww  w  .  j  ava  2  s  .c  o  m
        entityDocModels.add(docModel);
    }

    entityDocModels.sort((EntityDocModel o1, EntityDocModel o2) -> o1.getName().compareTo(o2.getName()));
    return entityDocModels;
}

From source file:com.smartitengineering.util.bean.BeanFactoryRegistrar.java

private static boolean aggregate(Class<? extends Object> aggregatorClass, Object aggregator)
        throws SecurityException {
    if (aggregatorClass.equals(Object.class)) {
        return true;
    }//w w w  .j  av a  2s.co  m
    Class<? extends Object> superClass = aggregatorClass.getSuperclass();
    if (superClass != null) {
        aggregate(superClass, aggregator);
    }
    Aggregator aggregatorAnnotation = aggregatorClass.getAnnotation(Aggregator.class);
    if (aggregatorAnnotation == null || StringUtils.isBlank(aggregatorAnnotation.contextName())) {
        return true;
    }
    BeanFactory beanFactory = getBeanFactorForContext(aggregatorAnnotation.contextName());
    if (beanFactory == null) {
        return true;
    }
    Field[] declaredFields = aggregatorClass.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        InjectableField injectableField = declaredField.getAnnotation(InjectableField.class);
        if (injectableField == null) {
            continue;
        }
        String beanName = StringUtils.isBlank(injectableField.beanName()) && beanFactory.isNameMandatory()
                ? declaredField.getName()
                : injectableField.beanName();
        if (StringUtils.isBlank(beanName) && beanFactory.isNameMandatory()) {
            return true;
        }
        try {
            declaredField.setAccessible(true);
            final Class<?> fieldType = declaredField.getType();
            if (beanFactory.containsBean(beanName, fieldType)) {
                declaredField.set(aggregator, beanFactory.getBean(beanName, fieldType));
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

    }
    return false;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static List<Method> getMothds(Class clazz, boolean includeParentClass) {

    List<Method> list = new ArrayList<Method>();

    Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {

        list.add(method);/*from  ww w.  j  av  a  2 s . co  m*/

    }

    if (includeParentClass) {

        getParentClassMothds(list, clazz.getSuperclass());

    }

    return list;

}

From source file:io.wcm.caravan.commons.httpasyncclient.impl.HttpClientTestUtils.java

private static Object getField(Object object, Class clazz, String fieldName) {
    Field field;/*from   ww  w.j a  v  a 2 s  .  c o  m*/
    try {
        field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(object);
    } catch (NoSuchFieldException ex) {
        Class superClazz = clazz.getSuperclass();
        if (superClazz != null) {
            return getField(object, superClazz, fieldName);
        } else {
            throw new RuntimeException("Unable to get field value for '" + fieldName + "'.", ex);
        }
    } catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        throw new RuntimeException("Unable to get field value for '" + fieldName + "'.", ex);
    }
}

From source file:Main.java

/**
 * get field form object and it's parent
 */// w ww  . j  a v  a 2 s  .  c  o  m
public static Field getDeclaredField(Object object, String fieldName) {
    Field field = null;

    Class<?> clazz = object.getClass();

    for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
        try {
            field = clazz.getDeclaredField(fieldName);
            return field;
        } catch (Exception e) {

        }
    }

    return null;
}