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.beetle.framework.util.ObjectUtil.java

private static Field[] getObjAllFields(Object object) {
    Class<?> clazz = object.getClass();
    List<Field> fieldList = new ArrayList<>();
    while (clazz != null) {
        fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
        clazz = clazz.getSuperclass();
    }/* www .j  ava2s.  c  o  m*/
    Field[] fields = new Field[fieldList.size()];
    fieldList.toArray(fields);
    return fields;
}

From source file:Main.java

public static long JavaToUnoType(Object obj, long fallbackTypePtr, boolean typeHasFallbackClass)
        throws ClassNotFoundException {
    Class<?> firstCls = obj.getClass();
    Long unoTypePtr = unoTypes.get(firstCls.getName());
    if (unoTypePtr != null) {
        return (long) unoTypePtr;
    } else {/*  w  w w  .ja va2 s.  c o  m*/
        if (typeHasFallbackClass) {
            Class<?> itf = unoFallbacks.get(fallbackTypePtr);
            if (itf == null) {
                throw new ClassNotFoundException(
                        "BINDING CLASS NOT FOUND (unoFallbacks): Not found for unoTypePtr:" + fallbackTypePtr);
            }
            Class<?> currentCls = firstCls;
            while (true) {
                if ((!itf.equals(currentCls)) && itf.isAssignableFrom(currentCls)) {
                    Long potential = unoTypes.get(currentCls.getName());
                    if (potential != null) {
                        unoTypes.put(firstCls.getName(), potential);
                        return (long) potential;
                    }
                } else {
                    unoTypes.put(firstCls.getName(), fallbackTypePtr);
                    return fallbackTypePtr;
                }
                currentCls = currentCls.getSuperclass();
                if (currentCls == null) {
                    unoTypes.put(firstCls.getName(), fallbackTypePtr);
                    return fallbackTypePtr;
                }
            }
        } else {
            Class<?> currentCls = firstCls;
            while (true) {
                currentCls = currentCls.getSuperclass();
                if (currentCls == null) {
                    unoTypes.put(firstCls.getName(), fallbackTypePtr);
                    return fallbackTypePtr;
                } else {
                    Long potential = unoTypes.get(currentCls.getName());
                    if (potential != null) {
                        if (Modifier.isAbstract(currentCls.getModifiers())) {
                            Long fallbackClassPtr = unoFallbacksClassToPtr.get(currentCls);
                            if (fallbackClassPtr != null) {
                                unoTypes.put(firstCls.getName(), fallbackClassPtr);
                                return fallbackClassPtr;
                            }
                        } else {
                            unoTypes.put(firstCls.getName(), potential);
                            return (long) potential;
                        }
                    }
                }
            }
        }
    }
}

From source file:net.kamhon.ieagle.util.VoUtil.java

private static Field getFieldForNotNestedProperty(Object obj, String propertyName) {
    Field field = null;// ww  w.  jav a 2  s  . co  m

    Class<?> tmpClazz = obj.getClass();
    // looping up to superclass to get decleared field
    do {
        try {
            field = tmpClazz.getDeclaredField(propertyName);
        } catch (Exception ex) {
        }

        if (field != null) {
            break;
        }

        tmpClazz = tmpClazz.getSuperclass();
    } while (tmpClazz != null);

    return field;
}

From source file:io.codis.nedis.util.NedisUtils.java

public static EventExecutor getEventExecutor(Future<?> future) {
    Class<?> clazz = future.getClass();
    for (;;) {//  ww w. j av a  2 s.  c  om
        try {
            Method method = clazz.getDeclaredMethod("executor");
            method.setAccessible(true);
            return (EventExecutor) method.invoke(future);
        } catch (NoSuchMethodException e) {
        } catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        clazz = clazz.getSuperclass();
        if (clazz == null) {
            return null;
        }
    }
}

From source file:net.kamhon.ieagle.util.ReflectionUtil.java

/**
 * different with class.getDeclaredField(does not include the field in super class). this one include the field in
 * super class also./*from  w  w w.  j  a  va  2 s.c  o m*/
 * 
 * @return
 * @throws NoSuchFieldException
 */
public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {

    Field field = null;
    while (clazz != null && field == null) {
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (Exception e) {
        }
        clazz = clazz.getSuperclass();
    }
    if (clazz == null && field == null)
        throw new NoSuchFieldException(clazz + ". Field name=" + fieldName);
    return field;
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;// w  w  w. j av  a 2 s  . c om
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            setMethod = entityType.getDeclaredMethod(methodName, field.getType());
        } catch (NoSuchMethodException e) {
            Log.d("getColumnSetMethod", methodName + " not exist");
        }
    }

    if (setMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnSetMethod(entityType.getSuperclass(), field);
    }
    return setMethod;
}

From source file:com.xiongyingqi.util.MethodInvoker.java

/**
 * Algorithm that judges the match between the declared parameter types of a candidate method
 * and a specific list of arguments that this method is supposed to be invoked with.
 * <p>Determines a weight that represents the class hierarchy difference between types and
 * arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase
 * the result - all direct matches means weight 0. A match between type Object and arg of
 * class Integer would increase the weight by 2, due to the superclass 2 steps up in the
 * hierarchy (i.e. Object) being the last one that still matches the required type Object.
 * Type Number and class Integer would increase the weight by 1 accordingly, due to the
 * superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number.
 * Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a
 * constructor (Number) which would in turn be preferred to a constructor (Object).
 * All argument weights get accumulated.
 * <p>Note: This is the algorithm used by MethodInvoker itself and also the algorithm
 * used for constructor and factory method selection in Spring's bean container (in case
 * of lenient constructor resolution which is the default for regular bean definitions).
 *
 * @param paramTypes the parameter types to match
 * @param args       the arguments to match
 * @return the accumulated weight for all arguments
 *///from w  ww  .  ja  v a2 s  . com
public static int getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args) {
    int result = 0;
    for (int i = 0; i < paramTypes.length; i++) {
        if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) {
            return Integer.MAX_VALUE;
        }
        if (args[i] != null) {
            Class<?> paramType = paramTypes[i];
            Class<?> superClass = args[i].getClass().getSuperclass();
            while (superClass != null) {
                if (paramType.equals(superClass)) {
                    result = result + 2;
                    superClass = null;
                } else if (ClassUtils.isAssignable(paramType, superClass)) {
                    result = result + 2;
                    superClass = superClass.getSuperclass();
                } else {
                    superClass = null;
                }
            }
            if (paramType.isInterface()) {
                result = result + 1;
            }
        }
    }
    return result;
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * If clazz implements genericInterface<T, U, ...>, this method returns the parameterized type
 * with the given index from that interface. This method will recursively look at superclasses
 * until it finds one implementing the requested interface
 * /*ww w  . java2 s  .  c  om*/
 * @should find genericInterface on a superclass if clazz does not directly implement it
 * @should ignore type variables on the declaring interface
 * @should not inspect superclasses of the specified genericInterface
 */
@SuppressWarnings("rawtypes")
public static Class getParameterizedTypeFromInterface(Class<?> clazz, Class<?> genericInterface, int index) {
    for (Type t : clazz.getGenericInterfaces()) {
        if (t instanceof ParameterizedType
                && ((Class) ((ParameterizedType) t).getRawType()).equals(genericInterface)) {
            //if we have reached the base interface that declares the type variable T, ignore it
            Type pType = ((ParameterizedType) t).getActualTypeArguments()[index];
            if (!(pType instanceof TypeVariable)) {
                return (Class) pType;
            }
        }
    }
    if (clazz.getSuperclass() != null && genericInterface.isAssignableFrom(clazz.getSuperclass())) {
        return getParameterizedTypeFromInterface(clazz.getSuperclass(), genericInterface, index);
    }
    return null;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {
    try {//  w  w w. j a  v  a  2  s.com
        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
        return method;
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        if (clazz == Object.class) {
            return null;
        } else {
            return getDeclaredMethod(clazz.getSuperclass(), methodName, parameterTypes);
        }
    }
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Collect all super implementing.// w w  w . j a v  a  2s.  c o  m
 *
 * @param <T> the generic type
 * @param clazz the clazz
 * @param implementing the implementing
 * @param list the list
 */
public static <T> void collectAllSuperImplementing(Class<?> clazz, Class<T> implementing, //
        Set<Class<? extends T>> list) {
    if (clazz == null) {
        return;
    }
    if (implementing.isAssignableFrom(clazz) == false) {
        return;
    }
    list.add((Class<? extends T>) clazz);
    collectAllSuperImplementing(clazz.getSuperclass(), implementing, list);
    for (Class<?> ifaces : clazz.getInterfaces()) {
        collectAllSuperImplementing(ifaces, implementing, list);
    }
}