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:ch.ralscha.extdirectspring.util.MethodInfo.java

/**
 * Find a method that is annotated with a specific annotation. Starts with the method
 * and goes up to the superclasses of the class.
 *
 * @param method the starting method/*  w  ww.j  a va 2 s .c om*/
 * @param annotation the annotation to look for
 * @return the method if there is a annotated method, else null
 */
public static Method findMethodWithAnnotation(Method method, Class<? extends Annotation> annotation) {
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    Class<?> cl = method.getDeclaringClass();
    while (cl != null && cl != Object.class) {
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            if (equivalentMethod.isAnnotationPresent(annotation)) {
                return equivalentMethod;
            }
        } catch (NoSuchMethodException e) {
            // do nothing here
        }
        cl = cl.getSuperclass();
    }

    return null;
}

From source file:com.spotify.helios.client.DefaultHttpConnector.java

private static void setRequestMethod(final HttpURLConnection connection, final String method,
        final boolean isHttps) {
    // Nasty workaround for ancient HttpURLConnection only supporting few methods
    final Class<?> httpURLConnectionClass = connection.getClass();
    try {/*  w  w w  .j ava2  s  .c om*/
        Field methodField;
        HttpURLConnection delegate;
        if (isHttps) {
            final Field delegateField = httpURLConnectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            delegate = (HttpURLConnection) delegateField.get(connection);
            methodField = delegate.getClass().getSuperclass().getSuperclass().getSuperclass()
                    .getDeclaredField("method");
        } else {
            delegate = connection;
            methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
        }

        methodField.setAccessible(true);
        methodField.set(delegate, method);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.modelmapper.internal.util.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*from ww w .jav  a 2s .com*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:com.github.gekoh.yagen.util.FieldInfo.java

public static AccessibleObject getIdFieldOrMethod(Class entityClass) {
    for (Field field : entityClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            return field;
        }/*w ww.j  a v a  2 s.  c  o m*/
    }
    for (Method method : entityClass.getDeclaredMethods()) {
        if (method.isAnnotationPresent(Id.class)) {
            return method;
        }
    }
    return entityClass.getSuperclass() != null ? getIdFieldOrMethod(entityClass.getSuperclass()) : null;
}

From source file:com.dnw.json.J.java

/**
 * Finds the corresponding type converter.
 * //from   w  w  w  .  j  a v  a 2 s  .co  m
 * @author manbaum
 * @since Oct 14, 2014
 * @param type the given type.
 * @return the corresponding type converter.
 */
private final static K<?> findConverter(Class<?> type) {
    K<?> k = cache.get(type);
    if (k != null)
        return k;

    k = map.get(type);
    if (k != null) {
        cache.put(type, k);
        return k;
    }

    Class<?> s = type.getSuperclass();
    if (s != null) {
        k = findConverter(s);
        if (k != null)
            return k;
    }

    for (Class<?> i : type.getInterfaces()) {
        k = findConverter(i);
        if (k != null)
            return k;
    }
    return null;
}

From source file:gr.abiss.calipso.tiers.util.ModelContext.java

public static ModelContext from(Class<?> domainClass) {

    ModelResource ar = domainClass.getAnnotation(ModelResource.class);
    ModelRelatedResource anr = domainClass.getAnnotation(ModelRelatedResource.class);

    ModelContext wrapper = null;//from w  ww .j  a v a2 s . com
    if (ar != null) {
        wrapper = new ModelContext(ar, domainClass);
    } else if (anr != null) {
        wrapper = new ModelContext(anr, domainClass);
    } else {
        // look for an ancestor who might be a resource
        Class<?> superClass = domainClass.getSuperclass();
        if (superClass != null && !Object.class.equals(superClass)) {
            wrapper = from(domainClass.getSuperclass());
        }
    }
    wrapper.setModelType(domainClass);
    return wrapper;
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Finds a method annotated with the specified annotation. This method can
 * be defined in the specified class, or one of its parents.
 *
 * @return the matching method, or <tt>null</tt> if any
 *//*from w  w w . j av  a 2s.  c  o m*/
public static <T extends Annotation> Method findMethodWithAnnotation(Class<?> clazz, Class<T> annotationType) {
    Method annotatedMethod = null;
    for (Method method : clazz.getDeclaredMethods()) {
        T annotation = AnnotationUtils.findAnnotation(method, annotationType);
        if (annotation != null) {
            if (annotatedMethod != null) {
                throw new BeanCreationException("Only ONE method with @" + annotationType.getName()
                        + " is allowed on " + clazz.getName() + ".");
            }
            annotatedMethod = method;
        }
    }

    if ((annotatedMethod != null) || clazz.equals(Object.class)) {
        return annotatedMethod;
    } else {
        return findMethodWithAnnotation(clazz.getSuperclass(), annotationType);
    }
}

From source file:com.google.code.siren4j.util.ReflectionUtils.java

public static Field findField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
    Field field = null;/*from   w  w  w . ja va  2s. c o m*/
    try {
        field = clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {

    }
    if (field == null) {
        Class<?> superClazz = clazz.getSuperclass();
        if (superClazz != null) {
            field = findField(superClazz, fieldName);
        }
        if (field == null) {
            throw new NoSuchFieldException("Field: " + fieldName);
        } else {
            return field;
        }
    } else {
        return field;
    }
}

From source file:gr.abiss.calipso.tiers.specifications.GenericSpecifications.java

/**
 * Get a (cached) field for the given class' member name
 * @param clazz/* w w w  .j  a  va2  s .  c  o  m*/
 * @param fieldName the member name
 * @return
 */
public static Field getField(Class<?> clazz, String fieldName) {
    Field field = null;
    if (!IGNORED_FIELD_NAMES.contains(fieldName)) {

        String key = clazz.getName() + "#" + fieldName;
        field = FIELD_CACHE.get(key);

        // find it if not cached
        if (field == null && !FIELD_CACHE.containsKey(key)) {
            Class<?> tmpClass = clazz;
            do {
                for (Field tmpField : tmpClass.getDeclaredFields()) {
                    String candidateName = tmpField.getName();
                    if (candidateName.equals(fieldName)) {
                        // field.setAccessible(true);
                        FIELD_CACHE.put(key, tmpField);
                        field = tmpField;
                        break;
                    }
                }
                tmpClass = tmpClass.getSuperclass();
            } while (tmpClass != null && field == null);
        }
        if (field == null) {
            LOGGER.warn("Field '" + fieldName + "' not found on class " + clazz);
            // HashMap handles null values so we can use containsKey to cach
            // invalid fields and hence skip the reflection scan
            FIELD_CACHE.put(key, null);
        }
        // throw new RuntimeException("Field '" + fieldName +
        // "' not found on class " + clazz);
    }

    return field;
}

From source file:com.manydesigns.portofino.pageactions.PageActionLogic.java

public static String getScriptTemplate(Class<?> actionClass) {
    if (!PageAction.class.isAssignableFrom(actionClass)) {
        return null;
    }/*w ww.  j  a v a2 s. co  m*/
    ScriptTemplate scriptTemplate = actionClass.getAnnotation(ScriptTemplate.class);
    if (scriptTemplate != null) {
        String templateLocation = scriptTemplate.value();
        try {
            return IOUtils.toString(actionClass.getResourceAsStream(templateLocation));
        } catch (Exception e) {
            throw new Error(
                    "Can't load script template: " + templateLocation + " for class: " + actionClass.getName(),
                    e);
        }
    } else {
        String template = getScriptTemplate(actionClass.getSuperclass());
        if (template != null) {
            return template;
        } else {
            try {
                InputStream stream = PageActionLogic.class.getResourceAsStream(
                        "/com/manydesigns/portofino/pageactions/default_script_template.txt");
                return IOUtils.toString(stream);
            } catch (Exception e) {
                throw new Error("Can't load script template", e);
            }
        }
    }
}