All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lwapp.notification.utils.Generics Maven / Gradle / Ivy

Go to download

Lwapp notification is a utility jar file with most common notification features(email, sms, twitter) needed in daily java programming.

The newest version!
package org.lwapp.notification.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Helper methods for class type parameters.
 * @see Super Type Tokens
 */
public class Generics {
    private Generics() { /* singleton */ }

    /**
     * Finds the type parameter for the given class.
     *
     * @param klass    a parameterized class
     * @return the class's type parameter
     */
    public static Class getTypeParameter(Class klass) {
        return getTypeParameter(klass, Object.class);
    }

    /**
     * Finds the type parameter for the given class which is assignable to the bound class.
     *
     * @param klass    a parameterized class
     * @param bound    the type bound
     * @param       the type bound
     * @return the class's type parameter
     */
    public static  Class getTypeParameter(Class klass, Class bound) {
        Type t = checkNotNull(klass);
        while (t instanceof Class) {
            t = ((Class) t).getGenericSuperclass();
        }
        /* This is not guaranteed to work for all cases with convoluted piping
         * of type parameters: but it can at least resolve straight-forward
         * extension with single type parameter (as per [Issue-89]).
         * And when it fails to do that, will indicate with specific exception.
         */
        if (t instanceof ParameterizedType) {
            // should typically have one of type parameters (first one) that matches:
            for (Type param : ((ParameterizedType) t).getActualTypeArguments()) {
                if (param instanceof Class) {
                    final Class cls = determineClass(bound, param);
                    if (cls != null) { return cls; }
                }
                else if (param instanceof TypeVariable) {
                    for (Type paramBound : ((TypeVariable) param).getBounds()) {
                        if (paramBound instanceof Class) {
                            final Class cls = determineClass(bound, paramBound);
                            if (cls != null) { return cls; }
                        }
                    }
                }
            }
        }
        throw new IllegalStateException("Cannot figure out type parameterization for " + klass.getName());
    }

    @SuppressWarnings("unchecked")
    private static  Class determineClass(Class bound, Type candidate) {
        if (candidate instanceof Class) {
            final Class cls = (Class) candidate;
            if (bound.isAssignableFrom(cls)) {
                return (Class) cls;
            }
        }

        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy