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.francetelecom.clara.cloud.presentation.utils.GetObjectsUtils.java

/**
 * Util method, which does getDeclaredField() on the class, and on its inheritance hierarchy if needed. Allows to access private fields
 * @param objectClass class of the object which contains the field
 * @param fieldName name of the field to find
 * @return a Field if it exists//  w  w w . j  a  va  2  s  . c o  m
 * @throws NoSuchFieldException raised when no field was found
 */
public static Field getAnyField(Class objectClass, String fieldName) throws NoSuchFieldException {
    try {
        return objectClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class superClass = objectClass.getSuperclass();
        if (superClass == null) {
            throw e;
        } else {
            return getAnyField(superClass, fieldName);
        }
    }
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

public static Serializable extractPrimaryKey(Object object) {
    if (object != null) {
        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);/*  w w w  . java  2  s .  c  om*/

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //0
                    Object result = field.get(object);
                    if (result != null && "0".equals(result.toString())) {
                        return null;
                    }
                    return (Serializable) result;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

public static Object generatePrimaryKey(Object object, Serializable id) {
    if (object != null) {
        Assert.notNull(id, "generated id can not be null.");

        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);/*ww  w  .  j av  a2s .co m*/

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //set id
                    field.set(object, id);

                    return object;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:Main.java

private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
    fields.addAll(Arrays.asList(type.getDeclaredFields()));

    if (type.getSuperclass() != null) {
        fields = getAllFields(fields, type.getSuperclass());
    }/*from   w  ww. java  2  s  .  c o m*/

    return fields;
}

From source file:Main.java

private static Field prepareField(Class c, String fieldName) throws NoSuchFieldException {
    while (c != null) {
        try {//from   w ww  .j  a  v a  2  s .c om
            Field f = c.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (Exception e) {
        } finally {
            c = c.getSuperclass();
        }
    }
    throw new NoSuchFieldException();
}

From source file:com.edmunds.autotest.ClassUtil.java

public static Collection<Method> getAllDeclaredMethods(Class cls) {
    List<Method> methods = new ArrayList<Method>();

    do {/*from   ww w  . j  av a2  s.c  o  m*/
        Collections.addAll(methods, cls.getDeclaredMethods());
        cls = cls.getSuperclass();
    } while (cls != null);

    return methods;
}

From source file:Main.java

/**
 * Print class name and its parent name of an object
 *
 * @param obj//from  w  w  w  . ja v a2 s . com
 */
public final static String getClassInheritence(Object obj) {
    if (obj == null) {
        return null;
    }
    StringBuilder stb = new StringBuilder();
    Class<?> cls = obj.getClass();
    String name = cls.getSimpleName();
    stb.append("[");
    stb.append(String.valueOf(obj));
    stb.append(":");
    while (!name.toLowerCase().equals("object")) {
        stb.append(name);
        stb.append("<-");
        cls = cls.getSuperclass();
        name = cls.getSimpleName();
    }
    stb.append("]");
    return stb.toString();
}

From source file:Main.java

public static List<Field> getAllFieldsWithAnnotation(List<Field> fields, Class<?> type,
        Class<? extends Annotation> annotationType) {
    for (Field field : type.getDeclaredFields()) {
        if (annotationType == null || field.getAnnotation(annotationType) != null) {
            fields.add(field);// w  w  w . ja v  a 2 s .c  o  m
        }
    }

    if (type.getSuperclass() != null) {
        fields = getAllFieldsWithAnnotation(fields, type.getSuperclass(), annotationType);
    }

    return fields;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E getProperty(Object object, String fieldName) {
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {/*from   www  .  ja  va 2s .  c om*/
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (E) field.get(object);
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets all of the fields in this class and the super classes
 *
 * @param beanClass/*  w w w. j a  va2 s.c om*/
 * @return
 */
static private List<Field> getFields(final Class<?> beanClass) {
    // This class must remain private due to Java 2 Security concerns
    List<Field> fields = AccessController.doPrivileged(new PrivilegedAction<List<Field>>() {
        public List<Field> run() {
            List<Field> fields = new ArrayList<Field>();
            Class<?> cls = beanClass;
            while (cls != null) {
                Field[] fieldArray = cls.getDeclaredFields();
                for (Field field : fieldArray) {
                    fields.add(field);
                }
                cls = cls.getSuperclass();
            }
            return fields;
        }
    });

    return fields;
}