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:io.github.pellse.decorator.util.reflection.ReflectionUtils.java

public static void copyFields(Object src, Object target) {

    Class<?> targetClass = target.getClass();
    do {/*from  ww  w  . j  a  va2  s.c o  m*/
        stream(targetClass.getDeclaredFields())
                .forEach(field -> CheckedRunnable.of(() -> field.set(target, field.get(src))).run());
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);
}

From source file:tools.xor.util.ClassUtil.java

public static Class<?> getUnEnhanced(Class<?> clazz) {
    if (isEnhanced(clazz))
        return clazz.getSuperclass();

    return clazz;
}

From source file:com.groupon.jenkins.util.ResourceUtils.java

private static String getTemplateFile(Class<?> resourceClass, String resourceName) {
    while (resourceClass != Object.class && resourceClass != null) {
        String name = resourceClass.getName().replace('.', '/').replace('$', '/') + "/" + resourceName;
        if (resourceClass.getClassLoader().getResource(name) != null)
            return '/' + name;
        resourceClass = resourceClass.getSuperclass();
    }/*  w  w  w. j  av a  2s .com*/
    return null;
}

From source file:Main.java

/**
 * Attempt to find a {@link Field field} on the supplied {@link Class} with the
 * supplied <code>name</code>. Searches all superclasses up to {@link Object}.
 *
 * @param clazz the class to introspect// w  w w .j  a  v  a 2  s  . c om
 * @param name  the name of the field
 * @return the corresponding Field object, or <code>null</code> if not found
 */
public static Field findField(Class<?> clazz, String name) {
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        for (Field field : fields) {
            if (name.equals(field.getName())) {
                field.setAccessible(true);
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

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
 */// w  w w  .  j a  v a  2  s  .  c o  m
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);
            }
        }

        cls = cls.getSuperclass();
    }
}

From source file:ShowClass.java

/**
 * Display the modifiers, name, superclass and interfaces of a class or
 * interface. Then go and list all constructors, fields, and methods.
 *//*  w  ww  .  j  a  va 2s .  c om*/
public static void print_class(Class c) {
    // Print modifiers, type (class or interface), name and superclass.
    if (c.isInterface()) {
        // The modifiers will include the "interface" keyword here...
        System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
                + typename(c.getSuperclass()));
    } else {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    // Print interfaces or super-interfaces of the class or interface.
    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (c.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typename(interfaces[i]));
        }
    }

    System.out.println(" {"); // Begin class member listing.

    // Now look up and display the members of the class.
    System.out.println("  // Constructors");
    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        // Display constructors.
        print_method_or_constructor(constructors[i]);

    System.out.println("  // Fields");
    Field[] fields = c.getDeclaredFields(); // Look up fields.
    for (int i = 0; i < fields.length; i++)
        // Display them.
        print_field(fields[i]);

    System.out.println("  // Methods");
    Method[] methods = c.getDeclaredMethods(); // Look up methods.
    for (int i = 0; i < methods.length; i++)
        // Display them.
        print_method_or_constructor(methods[i]);

    System.out.println("}"); // End class member listing.
}

From source file:ReflectionUtils.java

public static Field getPropertyField(Class<?> beanClass, String property) throws NoSuchFieldException {
    if (beanClass == null)
        throw new IllegalArgumentException("beanClass cannot be null");

    Field field = null;//from ww  w. ja v a2s. c  om
    try {
        field = beanClass.getDeclaredField(property);
    } catch (NoSuchFieldException e) {
        if (beanClass.getSuperclass() == null)
            throw e;
        // look for the field in the superClass
        field = getPropertyField(beanClass.getSuperclass(), property);
    }
    return field;
}

From source file:org.red5.server.api.persistence.PersistenceUtils.java

/**
 * Returns persistence store object. Persistence store is a special object that stores persistence objects and provides methods to manipulate them (save, load, remove, list).
 * //from  ww w.j ava 2 s . co  m
 * @param resolver
 *            Resolves connection pattern into Resource object
 * @param className
 *            Name of persistence class
 * @return IPersistence store object that provides methods for persistence object handling
 * @throws Exception
 *             if error
 */
public static IPersistenceStore getPersistenceStore(ResourcePatternResolver resolver, String className)
        throws Exception {
    Class<?> persistenceClass = Class.forName(className);
    Constructor<?> constructor = getPersistenceStoreConstructor(persistenceClass,
            resolver.getClass().getInterfaces());
    if (constructor == null) {
        // Search in superclasses of the object.
        Class<?> superClass = resolver.getClass().getSuperclass();
        while (superClass != null) {
            constructor = getPersistenceStoreConstructor(persistenceClass, superClass.getInterfaces());
            if (constructor != null) {
                break;
            }
            superClass = superClass.getSuperclass();
        }
    }
    if (constructor == null) {
        throw new NoSuchMethodException();
    }
    return (IPersistenceStore) constructor.newInstance(new Object[] { resolver });
}

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

/**
 * <p class="detail">/*w ww  .  j a va2  s  .  c o m*/
 * 
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj 
 * @param name    ??
 * @param value  (?)??false
 * @return
 */
public static boolean setProperty(Object obj, String name, Object value) {
    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 {
                Class<?> type = field.getType();
                if (type.isPrimitive() == true && value != null) {
                    if (value instanceof String) {
                        if (type.equals(int.class) == true) {
                            value = Integer.parseInt((String) value);
                        } else if (type.equals(double.class) == true) {
                            value = Double.parseDouble((String) value);
                        } else if (type.equals(boolean.class) == true) {
                            value = Boolean.parseBoolean((String) value);
                        } else if (type.equals(long.class) == true) {
                            value = Long.parseLong((String) value);
                        } else if (type.equals(byte.class) == true) {
                            value = Byte.parseByte((String) value);
                        } else if (type.equals(char.class) == true) {
                            value = Character.valueOf(((String) value).charAt(0));
                        } else if (type.equals(float.class) == true) {
                            value = Float.parseFloat((String) value);
                        } else if (type.equals(short.class) == true) {
                            value = Short.parseShort((String) value);
                        }
                    }
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                if (value == null || type.equals(value.getClass()) == true) {
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
    return false;
}

From source file:com.amalto.core.metadata.ClassRepository.java

private static boolean isClassMethod(Class clazz, Method declaredMethod) {
    Class superClass = clazz.getSuperclass();
    if (!Object.class.equals(superClass)) {
        try {//from w ww . ja v  a  2 s  .c o  m
            return superClass.getMethod(declaredMethod.getName(), declaredMethod.getParameterTypes()) != null;
        } catch (NoSuchMethodException e) {
            return true;
        }
    }
    return true;
}