Example usage for java.lang.reflect ParameterizedType getRawType

List of usage examples for java.lang.reflect ParameterizedType getRawType

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getRawType.

Prototype

Type getRawType();

Source Link

Document

Returns the Type object representing the class or interface that declared this type.

Usage

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

/**
 * ??<code>Type</code>??/*from  w  w w .j av a  2  s  .c  om*/
 * @param type ????<code>Type</code>
 * @param ancestorTypeList <code>type</code>??? ??????<code>ParameterizedType</code>?
 * @return ?
 * @throws IllegalStateException <code>type</code>? <code>Class</code>???? <code>TypeVariable</code>????? 
 *             <code>type</code>?? ????????? <code>type</code>???<code>Class</code>????
 *             (??)?
 */
protected static Class<? extends Object> resolveTypeVariable(Type type,
        List<ParameterizedType> ancestorTypeList) throws IllegalStateException {

    if (isNotTypeVariable(type)) {
        return getRawClass(type);
    }

    // TypeVariable:?
    TypeVariable<?> targetType = (TypeVariable<?>) type;
    Type actualType = null;
    for (int i = ancestorTypeList.size() - 1; i >= 0; i--) {
        ParameterizedType ancestorType = ancestorTypeList.get(i);

        // ?????
        GenericDeclaration declaration = targetType.getGenericDeclaration();
        if (!(declaration instanceof Class)) {
            throw new IllegalStateException("TypeVariable(" + targetType.getName()
                    + " is not declared at Class " + "(ie. is declared at Method or Constructor)");
        }

        // ?Generics????????
        Class<?> declaredClass = (Class<?>) declaration;
        if (declaredClass != ancestorType.getRawType()) {
            continue;
        }

        // ????????
        // ConcreteAbstractBLogic<R,P> extends AbstractBLogic<P,R>
        // ????????type????
        Type[] parameterTypes = declaredClass.getTypeParameters();
        int index = ArrayUtils.indexOf(parameterTypes, targetType);
        if (index == -1) {
            // ???????????????
            // ??????????
            // ???Generics?API?????????????
            // ?????????
            throw new IllegalStateException("Class(" + declaredClass.getName()
                    + ") does not declare TypeValidable(" + targetType.getName() + ")");
        }
        actualType = ancestorType.getActualTypeArguments()[index];
        if (log.isDebugEnabled()) {
            log.debug("resolved " + targetType.getName() + " -> " + actualType);
        }

        if (isNotTypeVariable(actualType)) {
            return getRawClass(actualType);
        }
        targetType = (TypeVariable<?>) actualType;
    }

    throw new IllegalStateException(
            "Concrete type of Type(" + type + ") was not found in ancestorList(" + ancestorTypeList + ")");
}

From source file:org.lunarray.model.descriptor.builder.annotation.base.listener.property.collection.SetCollectionTypeListener.java

/** {@inheritDoc} */
@Override/*from  w  ww .  j a va  2  s . c  om*/
public void handleEvent(final UpdatedPropertyTypeEvent<P, E, B> event) throws EventException {
    SetCollectionTypeListener.LOGGER.debug("Handle event {}", event);
    Validate.notNull(event, "Event may not be null.");
    @SuppressWarnings("unchecked")
    final AnnotationCollectionPropertyDescriptorBuilder<C, P, E, B> builder = (AnnotationCollectionPropertyDescriptorBuilder<C, P, E, B>) event
            .getBuilder();
    event.getProperty();
    final Class<?> collectionType;
    final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0,
            builder.getBuilderContext().getAccessorContext().getDescribedProperties());
    if (tmpType instanceof Class) {
        collectionType = (Class<?>) tmpType;
    } else if (tmpType instanceof ParameterizedType) {
        final ParameterizedType ptype = (ParameterizedType) tmpType;
        final Type rawType = ptype.getRawType();
        if (rawType instanceof Class) {
            collectionType = (Class<?>) rawType;
        } else {
            collectionType = Object.class;
        }
    } else {
        collectionType = Object.class;
    }
    builder.collectionType(collectionType);
}

From source file:org.lunarray.model.descriptor.builder.annotation.base.listener.operation.parameter.collection.SetCollectionTypeListener.java

/** {@inheritDoc} */
@Override/* w ww  . j a va  2s . c  om*/
public void handleEvent(final UpdatedParameterEvent<P, B> event) throws EventException {
    SetCollectionTypeListener.LOGGER.debug("Handling event {}", event);
    Validate.notNull(event, "Event may not be null.");
    @SuppressWarnings("unchecked")
    final AnnotationCollectionParameterDescriptorBuilder<C, P, B> builder = (AnnotationCollectionParameterDescriptorBuilder<C, P, B>) event
            .getBuilder();
    final Class<?> collectionType;
    final Deque<DescribedParameter<?>> params = new LinkedList<DescribedParameter<?>>();
    params.add(event.getParameter());
    final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0, params);
    if (tmpType instanceof Class) {
        collectionType = (Class<?>) tmpType;
    } else if (tmpType instanceof ParameterizedType) {
        final ParameterizedType ptype = (ParameterizedType) tmpType;
        final Type rawType = ptype.getRawType();
        if (rawType instanceof Class) {
            collectionType = (Class<?>) rawType;
        } else {
            collectionType = Object.class;
        }
    } else {
        collectionType = Object.class;
    }
    builder.collectionType(collectionType);
}

From source file:org.lunarray.model.descriptor.builder.annotation.base.listener.operation.result.collection.SetCollectionTypeListener.java

/** {@inheritDoc} */
@Override/*from   www.ja  va 2  s. c o  m*/
public void handleEvent(final UpdatedCollectionResultTypeEvent<C, R, E, B> event) throws EventException {
    SetCollectionTypeListener.LOGGER.debug("Handling event {}", event);
    Validate.notNull(event, "Event may not be null.");
    final AnnotationCollectionResultDescriptorBuilder<C, R, E, B> builder = event.getBuilder();
    final Deque<Member> members = new LinkedList<Member>();
    members.addAll(builder.getBuilderContext().getAccessorContext().getDescribedProperties());
    members.add(builder.getOperation());
    final Class<?> collectionType;
    final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0, members);
    if (tmpType instanceof Class) {
        collectionType = (Class<?>) tmpType;
    } else if (tmpType instanceof ParameterizedType) {
        final ParameterizedType ptype = (ParameterizedType) tmpType;
        final Type rawType = ptype.getRawType();
        if (rawType instanceof Class) {
            collectionType = (Class<?>) rawType;
        } else {
            collectionType = Object.class;
        }
    } else {
        collectionType = Object.class;
    }
    builder.collectionType(collectionType);
}

From source file:io.github.theangrydev.yatspeczohhakplugin.json.JsonCollectionsParameterCoercer.java

@Override
public Object coerceParameter(Type type, String stringToParse) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        return coerceParameterizedType(stringToParse, actualTypeArguments, rawType);
    } else if (type instanceof Class) {
        Class<?> targetType = ClassUtils.primitiveToWrapper((Class<?>) type);
        if (targetType.isArray()) {
            return coerceCollection(stringToParse, targetType.getComponentType(), ArrayBuilder::new);
        }/*from  w ww.j  a  v a2 s  . co  m*/
        return defaultParameterCoercer.coerceParameter(targetType, stringToParse);
    } else {
        throw new IllegalArgumentException(format("Cannot interpret '%s' as a '%s'", stringToParse, type));
    }
}

From source file:com.github.cherimojava.data.spring.EntityConverter.java

@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
    if (MediaType.APPLICATION_JSON.equals(mediaType)) {
        if (type instanceof Class) {
            // check if this is a simple entity class
            return Entity.class.isAssignableFrom((Class) type);
        }// ww  w  .  ja va 2s.c o  m
        if (type instanceof ParameterizedType) {
            // is this a parameterized type
            ParameterizedType pt = (ParameterizedType) type;
            if (pt.getRawType() instanceof Class
                    && Collection.class.isAssignableFrom((Class) pt.getRawType())) {
                // is this rawtype a class and is this class some collection
                Type generic = pt.getActualTypeArguments()[0];
                if (generic instanceof Class && Entity.class.isAssignableFrom((Class) generic)) {
                    // is this collection generic an entity
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.mousepilots.es.maven.model.generator.model.attribute.GenericMethodInfo.java

/**
 * Resolves the generic {@code type}/* w ww . ja  v  a 2  s .co  m*/
 * @param typeVariable2Value
 * @param type
 * @return 
 */
private String getGenericTypeString(Type type) {
    if (type instanceof Class) {
        //(raw) class
        return ((Class) type).getCanonicalName();
    } else if (type instanceof ParameterizedType) {
        // e.g. Map<K,V>
        final ParameterizedType parameterizedType = (ParameterizedType) type;
        return getGenericTypeString(parameterizedType.getRawType()) + "<"
                + StringUtils.join(Arrays.asList(parameterizedType.getActualTypeArguments()),
                        (Type actualTypeArgument) -> getGenericTypeString(actualTypeArgument), ",")
                + ">";

    } else if (type instanceof TypeVariable) {
        //e.g. T
        final TypeVariable<?> typeVariable = (TypeVariable) type;
        final Type typeVariableValue = typeVariable2Value.get(typeVariable);
        if (typeVariableValue == null) {
            return typeVariable.getName();
        } else {
            return getGenericTypeString(typeVariableValue);
        }
    } else if (type instanceof GenericArrayType) {
        //e.g. T[]
        final GenericArrayType genericArrayType = (GenericArrayType) type;
        return getGenericTypeString(genericArrayType.getGenericComponentType()) + "[]";
    } else {
        throw new IllegalArgumentException("unsupported type " + type);
    }
}

From source file:ca.uhn.fhir.context.RuntimePrimitiveDatatypeDefinition.java

private void determineNativeType(Class<? extends IPrimitiveType<?>> theImplementingClass) {
    Class<?> clazz = theImplementingClass;
    while (clazz.equals(Object.class) == false) {
        Type type = clazz.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType superPt = (ParameterizedType) type;
            Type rawType = superPt.getRawType();
            if (rawType instanceof Class) {
                Class<?> rawClass = (Class<?>) rawType;
                if (rawClass.getName().endsWith(".BasePrimitive")
                        || rawClass.getName().endsWith(".PrimitiveType")) {
                    Type typeVariable = superPt.getActualTypeArguments()[0];
                    if (typeVariable instanceof Class) {
                        myNativeType = (Class<?>) typeVariable;
                        break;
                    }/*  w  ww . j  av  a  2s  .  co  m*/
                }
            }
        }
        clazz = clazz.getSuperclass();
    }
}

From source file:com.azaptree.services.security.impl.SecurityCredentialsServiceImpl.java

@Override
public Map<String, Class<?>> getSupportedCredentials() throws SecurityServiceException {
    final Map<String, Class<?>> supportedCredentials = new HashMap<>();
    for (Map.Entry<String, CredentialToByteSourceConverter<?>> entry : credentialToByteSourceConverters
            .entrySet()) {//from  ww w.  j a v a  2 s. com
        final CredentialToByteSourceConverter<?> converter = entry.getValue();
        final Class<?> converterParamType;
        for (Type genericInterface : converter.getClass().getGenericInterfaces()) {
            if (genericInterface instanceof ParameterizedType) {
                final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
                if (((Class<?>) parameterizedType.getRawType())
                        .isAssignableFrom(CredentialToByteSourceConverter.class)) {
                    final Type type = parameterizedType.getActualTypeArguments()[0];
                    converterParamType = type instanceof Class<?> ? (Class<?>) type
                            : (Class<?>) ((ParameterizedType) type).getRawType();
                    supportedCredentials.put(entry.getKey(), converterParamType);
                    break;
                }
            }
        }
    }

    return supportedCredentials;
}

From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java

private Type resolveParameterizedType(ParameterizedType beanType, ParameterizedType parameterizedType) {
    Type rawType = parameterizedType.getRawType();
    Type[] actualTypes = parameterizedType.getActualTypeArguments();

    Type resolvedRawType = resolveType(beanType, beanType, rawType);
    Type[] resolvedActualTypes = new Type[actualTypes.length];

    for (int i = 0; i < actualTypes.length; i++) {
        resolvedActualTypes[i] = resolveType(beanType, beanType, actualTypes[i]);
    }/*from w  ww  .  jav  a2  s.  c o  m*/
    // reconstruct ParameterizedType by types resolved TypeVariable.
    return TypeUtils.parameterizeWithOwner(parameterizedType.getOwnerType(), resolvedRawType.getClass(),
            resolvedActualTypes);
}