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.expressui.core.util.ReflectionUtil.java

/**
 * Finds a method on a class./*  w w  w .  ja v a  2s  .c  om*/
 *
 * @param type           class containing the method
 * @param methodName     name of the method to search for
 * @param parameterTypes parameter types declared in the method signature
 * @return found method
 */
public static Method getMethod(Class type, String methodName, Class<?>... parameterTypes) {
    Method method = null;
    Class currentType = type;
    while (method == null && !currentType.equals(Object.class)) {
        try {
            method = currentType.getDeclaredMethod(methodName, parameterTypes);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            currentType = currentType.getSuperclass();
        }
    }

    return method;
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

private static Method getMethodFromClassWithInheritance(Class<?> cls, String methodName, Class<?> argClass) {

    Method theMethod = getMethodFromClass(cls, methodName, argClass, false);
    while (theMethod == null && (!cls.getName().equals("java.lang.Object"))) {
        cls = cls.getSuperclass();
        theMethod = getMethodFromClass(cls, methodName, argClass, true);
    }/*from w w w .j a  va2  s .  c  o m*/
    return theMethod;
}

From source file:com.mawujun.utils.AnnotationUtils.java

/**
 * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method},
 * traversing its super methods if no annotation can be found on the given method itself.
 * <p>Annotations on methods are not inherited by default, so we need to handle this explicitly.
 * @param method the method to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or {@code null} if none found
 *///from   ww  w . ja  va 2s.c om
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
    A annotation = getAnnotation(method, annotationType);
    Class<?> cl = method.getDeclaringClass();
    if (annotation == null) {
        annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
    }
    while (annotation == null) {
        cl = cl.getSuperclass();
        if (cl == null || cl == Object.class) {
            break;
        }
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            annotation = getAnnotation(equivalentMethod, annotationType);
        } catch (NoSuchMethodException ex) {
            // No equivalent method found
        }
        if (annotation == null) {
            annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
        }
    }
    return annotation;
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;//from ww  w. ja v  a  2 s  . com
    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) {
            //Logger.d(methodName + " not exist");
        }
    }

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

From source file:cn.afterturn.easypoi.util.PoiPublicUtil.java

/**
 * ?class /*w  w  w  . j  a  va2s  .  com*/
 *
 * @param clazz
 * @return
 */
public static Field[] getClassFields(Class<?> clazz) {
    List<Field> list = new ArrayList<Field>();
    Field[] fields;
    do {
        fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            list.add(fields[i]);
        }
        clazz = clazz.getSuperclass();
    } while (clazz != Object.class && clazz != null);
    return list.toArray(fields);
}

From source file:Main.java

public static Method getColumnGetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method getMethod = null;//from   www.j av  a 2  s.  c o m
    if (field.getType() == boolean.class) {
        getMethod = getBooleanColumnGetMethod(entityType, fieldName);
    }
    if (getMethod == null) {
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            getMethod = entityType.getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            Log.d("getColumnGetMethod", methodName + " not exist");
        }
    }

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

From source file:com.opensource.frameworks.processframework.utils.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.
 * @param paramTypes the parameter types to match
 * @param args the arguments to match//from  w w w .  j a v  a 2 s . co m
 * @return the accumulated weight for all arguments
 */
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:ml.shifu.shifu.container.meta.MetaFactory.java

/**
 * Iterate each property of Object, get the value and validate
 * /*ww  w.j  av a2s  . c om*/
 * @param isGridSearch
 *            - if grid search, ignore validation in train#params as they are set all as list
 * @param ptag
 *            - the prefix of key to search @MetaItem
 * @param obj
 *            - the object to validate
 * @return ValidateResult
 *         If all items are OK, the ValidateResult.status will be true;
 *         Or the ValidateResult.status will be false, ValidateResult.causes will contain the reasons
 * @throws Exception
 *             any exception in validaiton
 */
public static ValidateResult iterateCheck(boolean isGridSearch, String ptag, Object obj) throws Exception {
    ValidateResult result = new ValidateResult(true);
    if (obj == null) {
        return result;
    }

    Class<?> cls = obj.getClass();
    Field[] fields = cls.getDeclaredFields();

    Class<?> parentCls = cls.getSuperclass();
    if (!parentCls.equals(Object.class)) {
        Field[] pfs = parentCls.getDeclaredFields();
        fields = (Field[]) ArrayUtils.addAll(fields, pfs);
    }

    for (Field field : fields) {
        if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !isJsonIngoreField(field)) {
            Method method = cls.getMethod("get" + getMethodName(field.getName()));
            Object value = method.invoke(obj);

            encapsulateResult(result,
                    validate(isGridSearch, ptag + ITEM_KEY_SEPERATOR + field.getName(), value));
        }
    }

    return result;
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

private static void processIntegrationService(Object targetObject, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;//from   w w w  .  jav a  2s . com
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(targetObject, field);
        }
    }
    processIntegrationService(targetObject, clazz.getSuperclass());
}

From source file:IntrospectionUtil.java

protected static Field findInheritedField(Package pack, Class clazz, String fieldName, Class fieldType,
        boolean strictType) throws NoSuchFieldException {
    if (clazz == null)
        throw new NoSuchFieldException("No class");
    if (fieldName == null)
        throw new NoSuchFieldException("No field name");
    try {/* ww  w.ja v  a2s.  co  m*/
        Field field = clazz.getDeclaredField(fieldName);
        if (isInheritable(pack, field) && isTypeCompatible(fieldType, field.getType(), strictType))
            return field;
        else
            return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType,
                    strictType);
    } catch (NoSuchFieldException e) {
        return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType, strictType);
    }
}