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.base.dao.sql.ReflectionUtils.java

public static BigInteger bigIntValue(Object value) throws NumberFormatException {
    if (value == null)
        return BigInteger.valueOf(0L);
    Class c = value.getClass();
    if (c == BigInteger.class)
        return (BigInteger) value;
    if (c == BigDecimal.class)
        return ((BigDecimal) value).toBigInteger();
    if (c.getSuperclass() == Number.class)
        return BigInteger.valueOf(((Number) value).longValue());
    if (c == Boolean.class)
        return BigInteger.valueOf(((Boolean) value).booleanValue() ? 1 : 0);
    if (c == Character.class)
        return BigInteger.valueOf(((Character) value).charValue());
    return new BigInteger(stringValue(value, true));
}

From source file:com.base.dao.sql.ReflectionUtils.java

public static BigDecimal bigDecValue(Object value) throws NumberFormatException {
    if (value == null)
        return BigDecimal.valueOf(0L);
    Class c = value.getClass();
    if (c == BigDecimal.class)
        return (BigDecimal) value;
    if (c == BigInteger.class)
        return new BigDecimal((BigInteger) value);
    if (c.getSuperclass() == Number.class)
        return BigDecimal.valueOf(processDecimal(value));

    if (c == Boolean.class)
        return BigDecimal.valueOf(((Boolean) value).booleanValue() ? 1 : 0);
    if (c == Character.class)
        return BigDecimal.valueOf(((Character) value).charValue());
    return new BigDecimal(stringValue(value, true));
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain constructor list of specified class If recursively is true, obtain
 * constructor from all class hierarchy//from   ww w  .  ja v a  2s . com
 * 
 * 
 * @param clazz class
 *            where fields are searching
 * @param recursively
 *            param
 * @param parameterTypes parameter types
 * @return constructor
 */
public static Constructor<?> getDeclaredConstructor(Class<?> clazz, boolean recursively,
        Class<?>... parameterTypes) {

    try {
        return clazz.getDeclaredConstructor(parameterTypes);
    } catch (NoSuchMethodException e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && recursively) {
            return getDeclaredConstructor(superClass, true, parameterTypes);
        }
    } catch (SecurityException e) {
        log.error("{}", e.getMessage(), e);
    }
    return null;
}

From source file:ReflectionUtils.java

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied name
 * and parameter types. Searches all superclasses up to <code>Object</code>.
 * <p>Returns <code>null</code> if no {@link Method} can be found.
 * @param clazz the class to introspect/*w  w w  .  j a  v a  2  s  .c o m*/
 * @param name the name of the method
 * @param paramTypes the parameter types of the method
 * @return the Method object, or <code>null</code> if none found
 */
public static Method findMethod(Class clazz, String name, Class[] paramTypes) {

    Class searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Adds all methods with the specified Annotation of class clazz and its superclasses to allFields
 *
 * @param annotationClass the {@link Annotation}s to find
 * @param clazz The {@link Class} to inspect
 * @param allMethods list of all methods
 *///from  w ww . java  2 s. c o  m
public static void addAllMethods(Class<? extends Annotation> annotationClass, Class clazz,
        List<Method> allMethods) {

    if (clazz == null) {
        return;
    }

    Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {
        Annotation ann = method.getAnnotation(annotationClass);
        if (ann != null) {
            allMethods.add(method);
        }
    }
    addAllMethods(annotationClass, clazz.getSuperclass(), allMethods);
}

From source file:ml.shifu.shifu.util.ClassUtils.java

@SuppressWarnings("unchecked")
public static List<Field> getAllFields(Class<?> clazz) {
    if (clazz == null || clazz.equals(Object.class)) {
        return Collections.EMPTY_LIST;
    }//w w w  . j  a va2s .  c o m

    List<Field> result = new ArrayList<Field>();
    for (Field field : clazz.getDeclaredFields()) {
        result.add(field);
    }
    Class<?> tmpClazz = clazz.getSuperclass();
    while (!Object.class.equals(tmpClazz)) {
        result.addAll(getAllFields(tmpClazz));
        tmpClazz = tmpClazz.getSuperclass();
    }

    return result;
}

From source file:com.dragome.callbackevictor.serverside.utils.ReflectionUtils.java

public static Map<String, Object> discoverMethods(final Class<?> pClazz, final Matcher pMatcher,
        final Indexer pIndexer) {

    log.debug("discovering methods on " + pClazz.getName());

    final Map<String, Object> result = new HashMap<String, Object>();

    Class<?> current = pClazz;
    do {/*w w w. j  av a  2  s . com*/
        final Method[] methods = current.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            final String mname = methods[i].getName();
            if (pMatcher.matches(mname)) {
                pIndexer.put(result, mname, methods[i]);

                log.debug("discovered method " + mname + " -> " + methods[i]);
            }
        }
        current = current.getSuperclass();
    } while (current != null);

    return result;
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain methods list of specified class If recursively is true, obtain
 * methods from all class hierarchy//from w ww  . ja  va  2  s  .co m
 * 
 * @param clazz class
 *            where fields are searching
 * @param recursively
 *            param
 * @return array of methods
 */
public static Method[] getDeclaredMethods(Class<?> clazz, boolean recursively) {
    List<Method> methods = new LinkedList<Method>();
    Method[] declaredMethods = clazz.getDeclaredMethods();
    Collections.addAll(methods, declaredMethods);

    Class<?> superClass = clazz.getSuperclass();

    if (superClass != null && recursively) {
        Method[] declaredMethodsOfSuper = getDeclaredMethods(superClass, true);
        if (declaredMethodsOfSuper.length > 0)
            Collections.addAll(methods, declaredMethodsOfSuper);
    }
    return methods.toArray(new Method[methods.size()]);
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Get a single {@link Annotation} of <code>annotationType</code> from the supplied {@link Method}, traversing its
 * super methods if no annotation can be found on the given method itself.
 * <p>//from www .  j  a  v a  2 s.  c  om
 * 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</code> if none found
 */
public static <A extends Annotation> A findAnnotation(final Method method, final 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 {
            final Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            annotation = getAnnotation(equivalentMethod, annotationType);
            if (annotation == null) {
                annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces());
            }
        } catch (final NoSuchMethodException ex) {
            // We're done...
        }
    }
    return annotation;
}

From source file:unquietcode.tools.beanmachine.AnnotationUtils.java

/**
 * Find the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code>
 * (including the specified <code>clazz</code> itself) which declares an annotation for the
 * specified <code>annotationType</code>, or <code>null</code> if not found. If the supplied
 * <code>clazz</code> is <code>null</code>, <code>null</code> will be returned.
 * <p>If the supplied <code>clazz</code> is an interface, only the interface itself will be checked;
 * the inheritance hierarchy for interfaces will not be traversed.
 * <p>The standard {@link Class} API does not provide a mechanism for determining which class
 * in an inheritance hierarchy actually declares an {@link java.lang.annotation.Annotation}, so we need to handle
 * this explicitly./*  ww w  .  j  ava 2  s.  co m*/
 * @param annotationType the Class object corresponding to the annotation type
 * @param clazz the Class object corresponding to the class on which to check for the annotation,
 * or <code>null</code>
 * @return the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code>
 * which declares an annotation for the specified <code>annotationType</code>, or <code>null</code>
 * if not found
 * @see Class#isAnnotationPresent(Class)
 * @see Class#getDeclaredAnnotations()
 */
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType,
        Class<?> clazz) {
    Assert.notNull(annotationType, "Annotation type must not be null");
    if (clazz == null || clazz.equals(Object.class)) {
        return null;
    }
    return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz
            : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass());
}