Example usage for java.lang Class getGenericSuperclass

List of usage examples for java.lang Class getGenericSuperclass

Introduction

In this page you can find the example usage for java.lang Class getGenericSuperclass.

Prototype

public Type getGenericSuperclass() 

Source Link

Document

Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class .

Usage

From source file:jp.terasoluna.fw.util.GenericsUtil.java

/**
 * ??????? <code>ParameterizedType</code>???
 * @param <T> ???//from   ww  w. ja  v a2s.  c  om
 * @param genericClass ??
 * @param descendantClass <code>genericsClass</code>?? ???
 * @return ??????? <code>ParameterizedType</code>?
 * @throws IllegalStateException <code>descendantClass</code>? ??????????? <code>genercClass</code>
 *             ????? ?????
 */
protected static <T> List<ParameterizedType> getAncestorTypeList(Class<T> genericClass,
        Class<? extends T> descendantClass) throws IllegalStateException {
    List<ParameterizedType> ancestorTypeList = new ArrayList<ParameterizedType>();
    Class<?> clazz = descendantClass;
    boolean isInterface = genericClass.isInterface();

    while (clazz != null) {
        Type type = clazz.getGenericSuperclass();
        if (checkParameterizedType(type, genericClass, ancestorTypeList)) {
            break;
        }

        // ??????
        // ??????
        if (!isInterface) {
            clazz = clazz.getSuperclass();
            continue;
        }
        if (checkInterfaceAncestors(genericClass, ancestorTypeList, clazz)) {
            break;
        }

        // ???????????
        // ????????
        // ?????????????
        // ??????????
        // ???Generics?API?????????????
        // ?????????
        clazz = clazz.getSuperclass();
    }

    // ?????
    // AbstractBLogic<P, R>
    if (ancestorTypeList.isEmpty()) {
        throw new IllegalStateException(
                "Argument 'genericClass'(" + genericClass.getName() + ") does not declare type parameter");
    }

    // ???????????????
    // ??????????
    // ???Generics?API?????????????
    // ?????????
    ParameterizedType targetType = ancestorTypeList.get(ancestorTypeList.size() - 1);
    if (!targetType.getRawType().equals(genericClass)) {
        throw new IllegalStateException("Class(" + descendantClass.getName()
                + ") is not concrete class of Class(" + genericClass.getName() + ")");
    }
    return ancestorTypeList;
}

From source file:org.jiemamy.utils.reflect.GenericUtil.java

/**
 * ??(???)??????{@link Map}??// w w w. j a  va  2 s .  c o m
 * 
 * @param clazz ??(???)
 * @return ????????{@link Map}
 * @throws IllegalArgumentException ?{@code null}???
 */
public static Map<TypeVariable<?>, Type> getTypeVariableMap(Class<?> clazz) {
    Validate.notNull(clazz);
    Map<TypeVariable<?>, Type> map = Maps.newLinkedHashMap();

    Class<?> superClass = clazz.getSuperclass();
    Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }

    Class<?>[] interfaces = clazz.getInterfaces();
    Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }

    return map;
}

From source file:org.wso2.carbon.registry.eventing.services.EventingServiceImpl.java

private static void storeHandlerConfig() {
    String configPath = CarbonUtils.getRegistryXMLPath();
    Set<String> hashSet = new HashSet<>();
    if (configPath != null) {
        File registryXML = new File(configPath);
        try {/* w  ww . j av a 2s  . c  o  m*/
            InputStream configInputStream = new FileInputStream(registryXML);
            StAXOMBuilder builder = new StAXOMBuilder(
                    CarbonUtils.replaceSystemVariablesInXml(configInputStream));
            OMElement configElement = builder.getDocumentElement();
            Iterator<OMElement> handlerConfigs = configElement.getChildrenWithName(new QName("handler"));
            while (handlerConfigs.hasNext()) {
                OMElement handlerConfigElement = handlerConfigs.next();
                OMElement filter = handlerConfigElement.getFirstChildWithName(new QName("filter"));
                String filterClassName = filter.getAttributeValue(new QName("class"));
                Class filterClass = Class.forName(filterClassName);
                Class mediaTypeMatcher = Class.forName(MEDIA_TYPE_MATCHER_FILTER_CLASS);
                if (filterClass.getGenericSuperclass().equals(mediaTypeMatcher)) {
                    Method method = filterClass.getMethod("getMediaType", null);
                    Object returnValue = method.invoke(filterClass.newInstance(), null);
                    listOfMediaTypes.add((String) returnValue);
                }
                OMElement property = filter.getFirstChildWithName(new QName("property"));
                if (property != null) {
                    if (MEDIA_TYPE_MATCHER_FILTER_CLASS.equals(filterClassName)
                            && "mediaType".equals(property.getAttributeValue(new QName("name")))) {
                        listOfMediaTypes.add(property.getText());
                    }
                }
            }
            hashSet.addAll(listOfMediaTypes);
            listOfMediaTypes.clear();
            listOfMediaTypes.addAll(hashSet);
        } catch (FileNotFoundException e) {
            log.error("registry.xml file not found", e);
        } catch (CarbonException e) {
            log.error("Error in converting registry xml inputstream", e);
        } catch (XMLStreamException e) {
            log.error("Error in registry xml stream", e);
        } catch (NoSuchMethodException e) {
            log.error("No such method to invoke", e);
        } catch (IllegalAccessException e) {
            log.error("Error when creating new instance", e);
        } catch (InstantiationException e) {
            log.error("Error when creating new instance", e);
        } catch (InvocationTargetException e) {
            log.error("Error when invoking the java reflection method", e);
        } catch (ClassNotFoundException e) {
            log.error("Error in Class.forName method", e);
        }
    }

}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static Collection<Type> getParameterTypesForCollection(Class<?> type) {
    if (isCollectionOrSequence(type)) {
        Map<TypeVariable<?>, Type> m = TypeUtils
                .getTypeArguments((ParameterizedType) type.getGenericSuperclass());
        return m.values();
    }//from   w w w  .  j a v a  2 s  .com

    return null;
}

From source file:net.ymate.platform.commons.util.ClassUtils.java

/**
 * @param clazz //  ww  w .  j  av a  2  s . c om
 * @return ????, ??RawType
 */
public static List<Class<?>> getParameterizedTypes(Class<?> clazz) {
    List<Class<?>> _clazzs = new ArrayList<Class<?>>();
    Type _types = clazz.getGenericSuperclass();
    if (ParameterizedType.class.isAssignableFrom(_types.getClass())) {
        for (Type _type : ((ParameterizedType) _types).getActualTypeArguments()) {
            if (ParameterizedType.class.isAssignableFrom(_type.getClass())) {
                _clazzs.add((Class<?>) ((ParameterizedType) _type).getRawType());
            } else {
                _clazzs.add((Class<?>) _type);
            }
        }
    } else {
        _clazzs.add((Class<?>) _types);
    }
    return _clazzs;
}

From source file:org.entando.entando.aps.system.services.api.UnmarshalUtils.java

public static Object unmarshal(ApplicationContext applicationContext, Class expectedType,
        InputStream bodyStream, MediaType contentType) throws Throwable {
    Object bodyObject = null;//from   w  w w  .j a  v a 2s  .  c  o m
    try {
        String body = IOUtils.toString(bodyStream, "UTF-8");
        InputStream stream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
        if (MediaType.APPLICATION_JSON_TYPE.equals(contentType)) {
            JSONProvider jsonProvider = (null != applicationContext)
                    ? (JSONProvider) applicationContext.getBean("jsonProvider")
                    : new JSONProvider();
            bodyObject = jsonProvider.readFrom(expectedType, expectedType.getGenericSuperclass(),
                    expectedType.getAnnotations(), contentType, null, stream/*bodyStream*/);
        } else {
            JAXBContext context = JAXBContext.newInstance(expectedType);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            bodyObject = (Object) unmarshaller.unmarshal(stream/*bodyStream*/);
        }
    } catch (Throwable t) {
        _logger.error("Error unmarshalling request body", t);
        throw new ApsSystemException("Error unmarshalling request body", t);
    }
    return bodyObject;
}

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 .  j a v  a2s  .c  o  m*/

        // 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:org.jiemamy.utils.reflect.GenericUtil.java

/**
 * ??(???)???????{@code map}??/*from w w w  . ja  va  2 s . c om*/
 * 
 * @param clazz 
 * @param type 
 * @param map ????????{@link Map}
 */
protected static void gatherTypeVariables(Class<?> clazz, Type type, Map<TypeVariable<?>, Type> map) {
    if (clazz == null) {
        return;
    }
    gatherTypeVariables(type, map);

    Class<?> superClass = clazz.getSuperclass();
    Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }

    Class<?>[] interfaces = clazz.getInterfaces();
    Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
}

From source file:antre.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 w  w w .  j  a va2 s .com

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

            type = type.getEnclosingClass();
        }

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

    return map;
}

From source file:eu.stratosphere.api.java.typeutils.TypeExtractor.java

private static Type getParameterType(Class<?> baseClass, ArrayList<Type> typeHierarchy, Class<?> clazz,
        int pos) {
    Type t = clazz.getGenericSuperclass();

    // check if type is child of the base class
    if (!(t instanceof Class<?> && baseClass.isAssignableFrom((Class<?>) t)) && !(t instanceof ParameterizedType
            && baseClass.isAssignableFrom((Class<?>) ((ParameterizedType) t).getRawType()))) {
        throw new IllegalArgumentException("A generic function base class must be a super class.");
    }/*from   w w w .ja v  a 2 s  . com*/
    if (typeHierarchy != null) {
        typeHierarchy.add(t);
    }

    Type curT = t;
    // go up the hierarchy until we reach the base class (with or without generics)
    // collect the types while moving up for a later top-down 
    while (!(curT instanceof ParameterizedType
            && ((Class<?>) ((ParameterizedType) curT).getRawType()).equals(baseClass))
            && !(curT instanceof Class<?> && ((Class<?>) curT).equals(baseClass))) {
        if (typeHierarchy != null) {
            typeHierarchy.add(curT);
        }

        // parameterized type
        if (curT instanceof ParameterizedType) {
            curT = ((Class<?>) ((ParameterizedType) curT).getRawType()).getGenericSuperclass();
        }
        // class
        else {
            curT = ((Class<?>) curT).getGenericSuperclass();
        }
    }

    // check if immediate child of base class has generics
    if (curT instanceof Class<?>) {
        throw new InvalidTypesException("Function needs to be parameterized by using generics.");
    }

    if (typeHierarchy != null) {
        typeHierarchy.add(curT);
    }

    ParameterizedType baseClassChild = (ParameterizedType) curT;

    return baseClassChild.getActualTypeArguments()[pos];
}