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:Mopex.java

/**
 * Returns an array of the instance variablies of the the specified class.
 * An instance variable is defined to be a non-static field that is declared
 * by the class or inherited.//from ww  w  .j  a  va2  s .  c o m
 * 
 * @return java.lang.Field[]
 * @param cls
 *            java.lang.Class
 */
//start extract getInstanceVariables
public static Field[] getInstanceVariables(Class cls) {
    List accum = new LinkedList();
    while (cls != null) {
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if (!Modifier.isStatic(fields[i].getModifiers())) {
                accum.add(fields[i]);
            }
        }
        cls = cls.getSuperclass();
    }
    Field[] retvalue = new Field[accum.size()];
    return (Field[]) accum.toArray(retvalue);
}

From source file:Main.java

private final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type,
        Class<?> declaringClass, Class<?> inClass) {

    if (inClass == null)
        return null;

    Class<?> superClass = null;
    Type resolvedType = null;//from www  .j ava2s.  co m
    Type genericSuperClass = null;

    if (!declaringClass.equals(inClass)) {
        if (declaringClass.isInterface()) {
            // the declaringClass is an interface
            Class<?>[] interfaces = inClass.getInterfaces();
            for (int i = 0; i < interfaces.length && resolvedType == null; i++) {
                superClass = interfaces[i];
                resolvedType = resolveTypeVariable(type, declaringClass, superClass);
                genericSuperClass = inClass.getGenericInterfaces()[i];
            }
        }

        if (resolvedType == null) {
            superClass = inClass.getSuperclass();
            resolvedType = resolveTypeVariable(type, declaringClass, superClass);
            genericSuperClass = inClass.getGenericSuperclass();
        }
    } else {
        resolvedType = type;
        genericSuperClass = superClass = inClass;

    }

    if (resolvedType != null) {
        // if its another type this means we have finished
        if (resolvedType instanceof TypeVariable<?>) {
            type = (TypeVariable<?>) resolvedType;
            TypeVariable<?>[] parameters = superClass.getTypeParameters();
            int positionInClass = 0;
            for (; positionInClass < parameters.length
                    && !type.equals(parameters[positionInClass]); positionInClass++) {
            }

            // we located the position of the typevariable in the superclass
            if (positionInClass < parameters.length) {
                // let's look if we have type specialization information in the current class
                if (genericSuperClass instanceof ParameterizedType) {
                    ParameterizedType pGenericType = (ParameterizedType) genericSuperClass;
                    Type[] args = pGenericType.getActualTypeArguments();
                    return positionInClass < args.length ? args[positionInClass] : null;
                }
            }

            // we didnt find typevariable specialization in the class, so it's the best we can
            // do, lets return the resolvedType...
        }
    }

    return resolvedType;
}

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

public static Field findField(Class<?> type, String fieldName) throws NoSuchFieldException, SecurityException {

    if (type.equals(Object.class)) {
        return type.getDeclaredField(fieldName);
    }//  ww w.  j a va 2s  . co m

    // now that's an ugly construct :)
    Field field = null;
    try {
        field = type.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        field = findField(type.getSuperclass(), fieldName);
    }

    return field;

}

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

public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(name, "Method name must not be null");
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName()) && (paramTypes == null || paramTypes.length == 0
                    || paramTypes[0] == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }//from w w  w  . j a  v  a2 s . c o m
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java

/**
 * ?Field/*from  w w  w.  ja  v  a  2  s  .  co  m*/
 * 
 * @param entityClass
 * @param fieldList
 * @return
 */
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
    if (fieldList == null) {
        fieldList = new ArrayList<Field>();
    }
    if (entityClass.equals(Object.class)) {
        return fieldList;
    }
    Field[] fields = entityClass.getDeclaredFields();
    for (Field field : fields) {
        // ??bug#2
        if (!Modifier.isStatic(field.getModifiers())) {
            fieldList.add(field);
        }
    }
    if (entityClass.getSuperclass() != null && !entityClass.getSuperclass().equals(Object.class)
            && !Map.class.isAssignableFrom(entityClass.getSuperclass())
            && !Collection.class.isAssignableFrom(entityClass.getSuperclass())) {
        return getAllField(entityClass.getSuperclass(), fieldList);
    }
    return fieldList;
}

From source file:Main.java

/**
 * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super
 * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned,
 * otherwise null. For example :/*  ww w .  j a  v a  2s .  c  o m*/
 * 
 * <pre>
 * abstract class MyClass implements Serializer&lt;Number&gt; {
 * 
 * }
 * 
 * // type value will be the parameterized type Serializer&lt;Number&gt;
 * Type type = lookupGenericType(Serializer.class, MyClass.class);
 * </pre>
 */
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) {
    if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass))
        return null;
    if (ofClass.equals(inClass))
        return inClass;

    if (ofClass.isInterface()) {
        // lets look if the interface is directly implemented by fromClass
        Class<?>[] interfaces = inClass.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            // do they match?
            if (ofClass.equals(interfaces[i])) {
                return inClass.getGenericInterfaces()[i];
            } else {
                Type superType = lookupGenericType(ofClass, interfaces[i]);
                if (superType != null)
                    return superType;
            }
        }
    }

    // ok it's not one of the directly implemented interfaces, lets try extended class
    Class<?> superClass = inClass.getSuperclass();
    if (ofClass.equals(superClass))
        return inClass.getGenericSuperclass();
    return lookupGenericType(ofClass, inClass.getSuperclass());
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static void collectMethods(Class<?> cls, Map<String, Method> collected) {
    for (Method m : cls.getDeclaredMethods()) {
        if ((m.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
            continue;
        }//from   ww  w . j  a v a2  s  . c o  m
        if ((m.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
            continue;
        }
        if (m.getAnnotation(TpsbIgnore.class) != null) {
            continue;
        }
        if (ignoreMethods.contains(m.getName()) == true) {
            continue;
        }
        if (m.getReturnType().isPrimitive() == true) {
            continue;
        }
        String sm = methodToString(m);
        if (collected.containsKey(sm) == true) {
            continue;
        }
        collected.put(sm, m);
    }
    Class<?> scls = cls.getSuperclass();
    if (scls == Object.class) {
        return;
    }
    collectMethods(scls, collected);
}

From source file:com.espertech.esper.event.bean.BeanEventType.java

private static EventType[] getSuperTypes(Class clazz, BeanEventTypeFactory beanEventTypeFactory) {
    List<Class> superclasses = new LinkedList<Class>();

    // add superclass
    Class superClass = clazz.getSuperclass();
    if (superClass != null) {
        superclasses.add(superClass);//  w ww.ja  v a  2  s . c  om
    }

    // add interfaces
    Class interfaces[] = clazz.getInterfaces();
    superclasses.addAll(Arrays.asList(interfaces));

    // Build event types, ignoring java language types
    List<EventType> superTypes = new LinkedList<EventType>();
    for (Class superclass : superclasses) {
        if (!superclass.getName().startsWith("java")) {
            EventType superType = beanEventTypeFactory.createBeanType(superclass.getName(), superclass, false,
                    false, false);
            superTypes.add(superType);
        }
    }

    return superTypes.toArray(new EventType[superTypes.size()]);
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Returns the getter method of the field. It is recursive method.
 *
 * @param instanceClass The bean class// www .  j  av  a  2s  . c o m
 * @param instance The bean instance
 * @param fieldName The field name
 * @return The getter method, if it is exists
 * null, if the getter method is not exists
 */
protected static Method getGetterMethod(final Class instanceClass, final Object instance,
        final String fieldName) {
    if ("java.lang.Object".equals(instanceClass.getName())) {
        return null;
    }

    try {
        StringBuilder sb = new StringBuilder(fieldName.length() + GET_WORD.length());
        sb.append(GET_WORD);
        sb.append(fieldName);
        sb.setCharAt(GET_WORD.length(), Character.toUpperCase(sb.charAt(GET_WORD.length())));
        return instanceClass.getDeclaredMethod(sb.toString());
    } catch (NoSuchMethodException except) {
        return getGetterMethod(instanceClass.getSuperclass(), instance, fieldName);
    }
}

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

private static <T extends Object> void processCCFeatureForObject(T entity, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;/*ww w.jav  a2  s  .  co  m*/
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCFeature.class) != null) {
            processCCFeatureField(entity, field);
        } else if (field.getAnnotation(CCIntegrationService.class) != null) {
            createIntegrationService(field, entity);
        } else if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(entity, field);
        }
    }
    processCCFeatureForObject(entity, (Class<? extends Object>) clazz.getSuperclass());
}