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:com.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 *  {@link java.lang.reflect.Field}(? private, inherited ).
 * /*from  w w  w.j  a va2  s  . co m*/
 * @param obj
 *            the obj
 * @return the declared fields
 * @see java.lang.Class#getDeclaredFields()
 * @see java.lang.Class#getSuperclass()
 * @see java.lang.reflect.Field
 * @see org.apache.commons.lang3.ArrayUtils#addAll(boolean[], boolean...)
 */
private static Field[] getDeclaredFields(Object obj) {
    Class<?> klass = obj.getClass();
    Class<?> superClass = klass.getSuperclass();

    //Class????(??public)
    Field[] fields = klass.getDeclaredFields();
    do {
        if (log.isDebugEnabled()) {
            log.debug("current class:[{}],super class:[{}]", klass.getName(), superClass.getName());
        }
        fields = ArrayUtils.addAll(fields, superClass.getDeclaredFields());
        superClass = superClass.getSuperclass();

    } while (null != superClass && superClass != Object.class);

    return fields;
}

From source file:com.junly.common.util.ReflectUtils.java

/**
 * <p class="detail">// ww w  .j  a  v a 2 s. c  o  m
 * ?
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj   
 * @param name   ??
 * @return
 */
public static Object getProperty(Object obj, String name) {
    if (obj != null) {
        Class<?> clazz = obj.getClass();
        while (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
                continue;
            }
            try {
                field.setAccessible(true);
                return field.get(obj);
            } catch (Exception e) {
                return null;
            } finally {
                field.setAccessible(false);
            }
        }
    }
    return null;
}

From source file:com.luna.common.utils.ReflectUtils.java

/**
 * ??// w ww .  j a  v a  2s  .  c o  m
 *
 * @param clazz
 * @param index
 * @param <T>
 * @return
 */
public static <T> Class<T> findParameterizedType(Class<?> clazz, int index) {
    Type parameterizedType = clazz.getGenericSuperclass();
    //CGLUB subclass target object()
    if (!(parameterizedType instanceof ParameterizedType)) {
        parameterizedType = clazz.getSuperclass().getGenericSuperclass();
    }
    if (!(parameterizedType instanceof ParameterizedType)) {
        return null;
    }
    Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
    if (actualTypeArguments == null || actualTypeArguments.length == 0) {
        return null;
    }
    return (Class<T>) actualTypeArguments[0];
}

From source file:Main.java

/**
 * Get the interfaces for the specified class.
 *
 * @param cls  the class to look up, may be {@code null}
 * @param interfacesFound the {@code Set} of interfaces for the class
 *//*from  w  ww .j a  v  a 2  s  . co m*/
private static void getAllInterfaces(Class<?> cls, HashSet<Class<?>> interfacesFound) {
    while (cls != null) {
        Class<?>[] interfaces = cls.getInterfaces();

        for (Class<?> i : interfaces) {
            if (interfacesFound.add(i)) {
                getAllInterfaces(i, interfacesFound);
            }
        }

        cls = cls.getSuperclass();
    }
}

From source file:com.jigsforjava.util.ClassUtils.java

private static void getAllDeclaredFields(Class<?> aClass, UnaryFunction function) {
    for (Field field : aClass.getDeclaredFields()) {
        try {//from ww w  . j  a  va 2 s.co m
            function.perform(field);
        } catch (FunctionException e) {
            e.printStackTrace();
        }
    }

    if (aClass.getSuperclass() != null) {
        getAllDeclaredFields(aClass.getSuperclass(), function);
    }
}

From source file:Main.java

public static boolean implementsInterface(Class<?> type, Class<?> interfaceType, boolean searchInSuperclasses) {
    boolean found = false;
    for (Class<?> item : type.getInterfaces()) {
        if (item.equals(interfaceType)) {
            found = true;/*from  w w w  . ja  v a2  s .c o  m*/
            break;
        }
    }

    if (found) {
        return true;
    } else {
        if (searchInSuperclasses && type.getSuperclass() != null) {
            return implementsInterface(type.getSuperclass(), interfaceType, searchInSuperclasses);
        }
    }

    return false;
}

From source file:com.hurence.logisland.classloading.PluginProxy.java

/**
 * Creates a proxy. Useful tu kind of 'tunnel' object from a classloader to another one.
 * Please beware linkage errors./*from w  w w  . j ava 2  s  . c  o m*/
 *
 * @param object the object to proxy.
 * @return the proxied object.
 */
public static <T> T create(T object) {

    Class<?> superClass = null;

    // Check class
    Class<?> currentCls = object.getClass();
    while (superClass == null) {
        try {
            Class.forName(currentCls.getSuperclass().getName());
            superClass = currentCls.getSuperclass();
        } catch (ClassNotFoundException e) {
            currentCls = currentCls.getSuperclass();
        }
    }

    Class[] interfaces = getAllInterfaces(object);

    List<Class<?>> il = new ArrayList<>();

    // Check available interfaces
    for (Class<?> i : interfaces) {
        try {
            Class.forName(i.getClass().getName());
            il.add(i);
        } catch (ClassNotFoundException e) {
        }
    }

    if (superClass == null && il.size() == 0) {
        return object;
    }

    return (T) createProxy(object, superClass, il.toArray(new Class[il.size()]), null);
}

From source file:Main.java

public static Field getField(Class<?> sourceClass, String fieldName, boolean isFindDeclaredField,
        boolean isUpwardFind) {
    Field field = null;//from  w  w  w.  j  a  v a  2  s .c om
    try {
        field = isFindDeclaredField ? sourceClass.getDeclaredField(fieldName) : sourceClass.getField(fieldName);
    } catch (NoSuchFieldException e1) {
        if (isUpwardFind) {
            Class<?> classs = sourceClass.getSuperclass();
            while (field == null && classs != null) {
                try {
                    field = isFindDeclaredField ? classs.getDeclaredField(fieldName)
                            : classs.getField(fieldName);
                } catch (NoSuchFieldException e11) {
                    classs = classs.getSuperclass();
                }
            }
        }
    }
    return field;
}

From source file:com.examples.with.different.packagename.ClassWithPrivateInterfaces.java

private static void getAllInterfaces(Class<?> cls, final HashSet<Class<?>> interfacesFound) {
    while (cls != null) {
        final Class<?>[] interfaces = cls.getInterfaces();

        for (final Class<?> i : interfaces) {
            if (interfacesFound.add(i)) {
                getAllInterfaces(i, interfacesFound);
            }//from   w w  w. ja  va 2s.c  o m
        }

        cls = cls.getSuperclass();
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Searches for a field in the given class and all of its super classes.
 * @param clazz Class to start the search for the field
 * @param name Name of the field//w w  w  . j  a  va  2  s  . co m
 * @return The field that was found
 * @throws NoSuchFieldException
 */
public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException {
    Class<?> searchClass = clazz;
    Field field = null;
    while (field == null && searchClass != null) {
        try {
            field = searchClass.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            searchClass = searchClass.getSuperclass();
        }
    }
    if (field == null) {
        throw new NoSuchFieldException(clazz.getSimpleName() + "." + name); //$NON-NLS-1$
    }
    return field;
}