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:jef.tools.reflect.ClassEx.java

/**
 * ??/*from  ww  w .  java2 s  .  c o  m*/
 */
public static List<Class<?>> withSupers(Class<?> c) {
    List<Class<?>> supers = new ArrayList<Class<?>>();
    supers.add(c);
    c = c.getSuperclass();
    while (c != null) {
        supers.add(c);
        c = c.getSuperclass();
    }
    return supers;
}

From source file:com.qingstor.sdk.utils.QSJSONUtil.java

private static void setParameterToMap(Method m, Object source, Field f, Object data) {
    if (data != null) {
        try {/* w  w  w  .  j  a  v  a2 s .  c o m*/
            if (data instanceof JSONArray || data instanceof JSONObject) {
                Class fClass = f.getType();
                if (fClass.equals(List.class)) {
                    List invokeData = new ArrayList();
                    ParameterizedType stringListType = (ParameterizedType) f.getGenericType();
                    Class<?> cls = (Class<?>) stringListType.getActualTypeArguments()[0];

                    if (cls.equals(String.class) || cls.equals(Integer.class) || cls.equals(Double.class)
                            || cls.equals(Long.class) || cls.equals(Float.class)) {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {

                                Object o = toObject(jsonData, i);
                                invokeData.add(o);
                            }
                        }
                    } else {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {
                                Object fObject = cls.newInstance();
                                JSONObject o = toJSONObject(jsonData, i);
                                Class tmpClass = fObject.getClass();
                                while (tmpClass != Object.class) {
                                    Field[] fields = tmpClass.getDeclaredFields();
                                    initParameter(o, fields, tmpClass, fObject);
                                    tmpClass = tmpClass.getSuperclass();
                                }
                                invokeData.add(fObject);
                            }
                        }
                    }

                    m.invoke(source, invokeData);

                } else if (fClass.equals(Map.class)) {
                    Map invokeData = new HashMap();
                    if (data instanceof JSONObject) {
                        JSONObject jsonData = (JSONObject) data;
                        for (int i = 0; i < jsonData.length(); i++) {
                            String key = toJSONObject(jsonData, i) + "";
                            Object value = toJSONObject(jsonData, key);
                            invokeData.put(key, value);
                        }
                    }
                    m.invoke(source, invokeData);
                } else {

                    Object invokeData = f.getType().newInstance();
                    Class tmpClass = invokeData.getClass();
                    while (tmpClass != Object.class) {
                        Field[] fields = tmpClass.getDeclaredFields();
                        initParameter((JSONObject) data, fields, tmpClass, invokeData);
                        tmpClass = tmpClass.getSuperclass();
                    }
                    m.invoke(source, invokeData);
                }

            } else {
                if (f.getType().equals(data.getClass())) {
                    m.invoke(source, data);
                } else {
                    m.invoke(source, getParseValue(f.getType(), data));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:hermes.impl.LoaderSupport.java

/**
 * Indicates if a class or interface implements or extends the specified
 * interfaces or classes. If the specified <CODE>Class</CODE> is a class,
 * this method will recursively test if this class, its superclass or one of
 * the implemented interfaces of this class implements the specified
 * interface.<BR>//from  w  w w .java  2  s.  com
 * If the specified <CODE>Class</CODE> is an interface, this method will
 * recursively test if this interface or one of the implemented interfaces of
 * this interface implements the specified interface.<BR>
 * 
 * @param clazz
 *           the class or interface in question
 * @param testInterface
 *           the class or interface to test against
 * @return <CODE>true</CODE> if the specified interfaces is implemented by
 *         this class or one of its super-classes or interfaces
 */
public static boolean implementsOrExtends(Class clazz, Class testInterface) {
    Class[] implementedInterfaces = clazz.getInterfaces();

    // test interface
    if (clazz.equals(testInterface)) {
        return true; // possibly the end of the recursion
    }

    for (int i = 0; i < implementedInterfaces.length; i++) {
        if (implementsOrExtends(implementedInterfaces[i], testInterface)) {
            return true; // recursion
        }
    }

    // maybe the superclass implements this interface ?
    Class superClass = clazz.getSuperclass();
    if (superClass != null && implementsOrExtends(superClass, testInterface)) {
        return true; // recursion
    }

    return false;
}

From source file:de.alpharogroup.lang.ClassExtensions.java

/**
 * Returns the name of the given class or null if the given class is null. If the given flag
 * 'simple' is true the simple name (without the package) will be returned.
 *
 * @param clazz/*from w w  w  . jav  a2s. c  o m*/
 *            The class
 * @param simple
 *            The flag if the simple name should be returned.
 *
 * @return The name of the given class or if the given flag 'simple' is true the simple name
 *         (without the package) will be returned.
 */
public static String getName(Class<?> clazz, final boolean simple) {
    String name = null;
    if (clazz != null) {
        while (clazz.isAnonymousClass()) {
            clazz = clazz.getSuperclass();
        }
        if (simple) {
            name = clazz.getSimpleName();
        } else {
            name = clazz.getName();
        }
    }
    return name;
}

From source file:Main.java

static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Method finalMethod = null;/*from  ww  w  . j  a  va2  s  .c o  m*/
    while (objectClass != null && finalMethod == null) {
        for (Method method : objectClass.getDeclaredMethods()) {
            if (method.getName().equals(fieldName)) {
                Class<?>[] paramsType = method.getParameterTypes();
                if (paramsType.length == 0) {
                    finalMethod = method;
                    break;
                } else if (paramsType.length == 1) {
                    if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                        finalMethod = method;
                        break;
                    }
                }

            }
        }
        if (finalMethod == null) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (finalMethod == null) {
        throw new NoSuchFieldException(fieldName);
    }
    return finalMethod;
}

From source file:com.jeroensteenbeeke.hyperion.events.DefaultEventDispatcher.java

@SuppressWarnings("unchecked")
static Class<? extends Event<?>> getEventClass(Class<?> handlerClass) {

    for (Class<?> i : handlerClass.getInterfaces()) {
        if (EventHandler.class.equals(i)) {
            for (Type t : handlerClass.getGenericInterfaces()) {
                if (t instanceof ParameterizedType) {
                    ParameterizedType pt = (ParameterizedType) t;
                    if (EventHandler.class.equals(pt.getRawType())) {

                        return (Class<? extends Event<?>>) pt.getActualTypeArguments()[0];

                    }//from w  w  w  .j ava2s  .  co  m
                }
            }
        } else if (EventHandler.class.isAssignableFrom(i)) {
            return getEventClass((Class<? extends EventHandler<?>>) i);
        }
    }

    if (EventHandler.class.isAssignableFrom(handlerClass.getSuperclass())) {
        return getEventClass((Class<?>) handlerClass.getSuperclass());
    }

    return null;

}

From source file:com.zlfun.framework.excel.ExcelUtils.java

public static <T> Map<String, String> genWebParamMap(T t) {
    Map<String, String> map = new HashMap<String, String>();
    try {/*from  ww w .j a v a 2  s  . c  om*/
        Field[] fs = t.getClass().getDeclaredFields();
        for (Field f : fs) {
            String data = get(t, f);
            if (data == null) {
                continue;
            }
            WebParam itemField = f.getAnnotation(WebParam.class);
            if (itemField == null) {
                continue;
            }
            if ("".equals(itemField.value())) {
                String fname = f.getName();
                if (!map.containsKey(fname)) {
                    map.put(fname, data);
                }
            } else {
                String fname = itemField.value();
                if (!map.containsKey(fname)) {
                    map.put(fname, data);
                }
            }
            // ?
            Class<?> parent = t.getClass().getSuperclass();
            while (parent != Object.class) {
                parent(t, parent, map);
                parent = parent.getSuperclass();
            }

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return map;
}

From source file:SystemUtils.java

public static boolean IsClassDervivedFrom(Class childClass, Class superClass) {
    boolean derived = false;
    Class prevSuper;//from   ww  w.ja v a 2s  . c o m

    if (childClass != null && superClass != null) {

        //Make sure this class is dervived from the Worker Class 
        prevSuper = childClass;
        superClass = childClass.getSuperclass();

        while (!superClass.getName().equals("java.lang.Object")) {
            prevSuper = superClass;
            superClass = superClass.getSuperclass();
        }

        derived = (prevSuper.getName().equals(superClass.getName()));
    } //End null class parameters check

    return derived;
}

From source file:com.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * This method will find a java Field for with a particular name.  If
 * needed, this method will search through super classes.  The field
 * does not need to be public./*  ww w  . jav a 2s.c  om*/
 *
 * @param cls the java Class to search.
 * @param fieldName the name of the field to find.
 * @return the java field.
 */
public static Field findField(final Class<?> cls, final String fieldName) {
    Class<?> currentClass = cls;
    while (currentClass != null) {
        Field[] fields = currentClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        currentClass = currentClass.getSuperclass();
    }

    return null;
}

From source file:org.opentides.util.CrudUtil.java

/**
 * Helper method to retrieve all fields of a class including
 * fields declared in its superclass.// w  ww . jav  a 2 s.  c  o  m
 * @param clazz
 * @param includeParent
 * @return
 */
@SuppressWarnings({ "rawtypes" })
public static List<Field> getAllFields(Class clazz, boolean includeParent) {
    List<Field> fields = new ArrayList<Field>();
    if (BaseEntity.class.isAssignableFrom(clazz) && includeParent)
        fields.addAll(getAllFields(clazz.getSuperclass(), includeParent));
    for (Field field : clazz.getDeclaredFields())
        fields.add(field);
    return fields;
}