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

/**
 * Returns all of the {@link TypeVariables}s implemented
 * by the given class.  If none are implemented then an array
 * of zero length is returned./*from w  w  w .j a va 2 s. c o m*/
 * @param clazz the class
 * @param genericClazz the generic class or interface to return
 * the TypeVariables from
 * @return an array of TypeVariable
 */
public static TypeVariable<?>[] getGenericTypeParameters(Class<?> clazz, Type genericClazz) {
    List<TypeVariable<?>> vars = new ArrayList<TypeVariable<?>>();

    // add superclass
    for (TypeVariable<?> var : clazz.getSuperclass().getTypeParameters()) {
        if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) {
            vars.add(var);
        }
    }

    // add interfaces
    for (Class<?> iface : clazz.getInterfaces()) {
        for (TypeVariable<?> var : iface.getTypeParameters()) {
            if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) {
                vars.add(var);
            }
        }
    }

    // return list
    return vars.toArray(new TypeVariable<?>[0]);
}

From source file:ClassUtil.java

/**
 * Retrieving fields list of specified class
 * If recursively is true, retrieving fields from all class hierarchy
 *
 * @param clazz where fields are searching
 * @param recursively param/* w  w  w .  ja  v  a 2s .c o  m*/
 * @return list of fields
 */
public static Field[] getDeclaredFields(Class clazz, boolean recursively) {
    List<Field> fields = new LinkedList<Field>();
    Field[] declaredFields = clazz.getDeclaredFields();
    Collections.addAll(fields, declaredFields);

    Class superClass = clazz.getSuperclass();

    if (superClass != null && recursively) {
        Field[] declaredFieldsOfSuper = getDeclaredFields(superClass, recursively);
        if (declaredFieldsOfSuper.length > 0)
            Collections.addAll(fields, declaredFieldsOfSuper);
    }

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

From source file:Main.java

private static void appendClassDerivation(Object object, StringBuilder builder) {
    builder.append(object.getClass().getSimpleName());
    Class<?> c = object.getClass().getSuperclass();
    String s = null;/* ww  w.jav a2 s  .  com*/
    while (c != null) {
        s = c.getSimpleName() + "." + s;
        c = c.getSuperclass();
    }
    builder.append(s);
}

From source file:com.activegrid.runtime.data.util.DataServiceUtils.java

public static Class<?> getEntityClass(Class<?> clazz) {

    // also see/*from  ww w  .ja  va  2 s . com*/
    // HibernateProxyHelper.getClassWithoutInitializingProxy(object)

    if (isProxy(clazz)) {
        return clazz.getSuperclass();
    }
    return clazz;
}

From source file:Main.java

public static boolean isImplement(Class<?> clazz, Class<?> interfaceClazz) {
    if (clazz.equals(interfaceClazz))
        return true;

    Class<?>[] ifs = clazz.getInterfaces();
    for (Class<?> i : ifs) {
        if (i.equals(interfaceClazz))
            return true;
    }/*from w  ww .  j a  v  a2 s  .  c om*/

    Class<?> s = clazz.getSuperclass();
    if (s == null || clazz.equals(s.getClass()))
        return false;

    return isImplement(s, interfaceClazz);
}

From source file:gpframework.RunExperiment.java

/**
 * Generates an object from a parameter value.
 * @param <T> type of the return parameter.
 * @param className name of the subclass to instantiate.
 * @return an instantiated object of type className.
 *//* w ww  .j  a v a  2 s. c  o  m*/
@SuppressWarnings("unchecked")
public synchronized static <T> T fromName(String className, Object... parameters) {

    // Get array of classes (to find right constructor)
    Class[] classes = new Class[parameters.length];
    for (int p = 0; p < parameters.length; p++) {
        // Integer must be transformed to int
        if (parameters[p].getClass() == Integer.class) {
            classes[p] = int.class;
        } else {
            // Find superclass
            Class currentClass = parameters[p].getClass();
            while (currentClass.getSuperclass() != Object.class)
                currentClass = currentClass.getSuperclass();

            classes[p] = currentClass;
        }
    }

    // Packages where to look for the class
    String[] packages = { "gpframework", "gpframework.algorithms", "gpframework.algorithms.components",
            "gpframework.algorithms.components.mutations", "gpframework.algorithms.components.selections",
            "gpframework.algorithms.components.selections", "gpframework.indicators",
            "gpframework.indicators.sorting", "gpframework.indicators.order", "gpframework.indicators.majority",
            "gpframework.program", "gpframework.program.sorting", "gpframework.program.ordermajority", };

    // Instantiated object
    T t = null;

    // Scan packages
    for (String packageName : packages) {
        try {
            Constructor<T> constructor = (Constructor<T>) Class.forName(packageName + "." + className)
                    .getConstructor(classes);
            t = constructor.newInstance(parameters);
            break;
        } catch (Exception e) {
            /* Keep going */ }
    }

    if (t == null)
        System.out.println("Object not created " + className
                + ", check your class name or look into fromClass() method for a solution.");

    return t;
}

From source file:Main.java

/**
 * @param clz class name/*from   www . j av  a 2 s .  c o m*/
 * @param funcName function name
 * @return true if the function is in the class, else false
 */
public static boolean existsFunc(Class clz, String funcName, Class parameterTypes) {
    if (null != clz) {
        try {
            return clz.getDeclaredMethod(funcName, parameterTypes) != null;
        } catch (Exception e) {
        }
        if (clz != Object.class) {
            return existsFunc(clz.getSuperclass(), funcName, parameterTypes);
        }
    }
    return false;
}

From source file:Main.java

private static Field getField(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Field field = null;/* w w w.j a  va 2s. c  om*/
    while (objectClass != null && field == null) {
        try {
            field = objectClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (field != null) {
        field.setAccessible(true);
    } else {
        throw new NoSuchFieldException(fieldName);
    }
    return field;
}

From source file:py.una.pol.karaku.util.ELHelper.java

private static Field getFieldFromCGEnhancedClass(String name, Class<?> clazz) {

    Class<?> real = clazz.getSuperclass();
    return ReflectionUtils.findField(real, name);
}

From source file:net.mojodna.sprout.support.SproutUtils.java

/**
 * Gets a collection of methods declared in a specified range of a given
 * class' hierarchy./*from w  w  w. j  av a  2 s.  c  om*/
 * 
 * @param clazz Class to inspect.
 * @param upto Methods declared in this class and its subclasses will be
 * included.  Any methods declared in superclasses will be ignored.
 * @return Collection of methods declared within the specified range.
 */
public static Collection<Method> getDeclaredMethods(Class clazz, final Class upto) {
    // collect methods to register (include methods for all classes up to and including this one)
    final Collection<Method> methods = new ArrayList();
    while (!clazz.equals(upto.getSuperclass())) {
        methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
        clazz = clazz.getSuperclass();
    }

    return methods;
}