Java Code Examples for org.jboss.jandex.Type#asParameterizedType()
The following examples show how to use
org.jboss.jandex.Type#asParameterizedType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaFactory.java From smallrye-open-api with Apache License 2.0 | 6 votes |
static Type resolveAsyncType(IndexView index, Type type, List<AnnotationScannerExtension> extensions) { if (type.kind() == Type.Kind.PARAMETERIZED_TYPE) { ParameterizedType pType = type.asParameterizedType(); if (pType.arguments().size() == 1 && (TypeUtil.isA(index, type, JDKConstants.COMPLETION_STAGE_TYPE) || TypeUtil.isA(index, type, MutinyConstants.UNI_TYPE))) { return pType.arguments().get(0); } } for (AnnotationScannerExtension extension : extensions) { Type asyncType = extension.resolveAsyncType(type); if (asyncType != null) return asyncType; } return type; }
Example 2
Source File: Types.java From quarkus with Apache License 2.0 | 6 votes |
static Type resolveTypeParam(Type typeParam, Map<TypeVariable, Type> resolvedTypeParameters, IndexView index) { if (typeParam.kind() == Kind.TYPE_VARIABLE) { return resolvedTypeParameters.getOrDefault(typeParam, typeParam); } else if (typeParam.kind() == Kind.PARAMETERIZED_TYPE) { ParameterizedType parameterizedType = typeParam.asParameterizedType(); ClassInfo classInfo = getClassByName(index, parameterizedType.name()); if (classInfo != null) { List<TypeVariable> typeParameters = classInfo.typeParameters(); List<Type> arguments = parameterizedType.arguments(); Map<TypeVariable, Type> resolvedMap = buildResolvedMap(arguments, typeParameters, resolvedTypeParameters, index); Type[] typeParams = new Type[typeParameters.size()]; for (int i = 0; i < typeParameters.size(); i++) { typeParams[i] = resolveTypeParam(arguments.get(i), resolvedMap, index); } return ParameterizedType.create(parameterizedType.name(), typeParams, null); } } return typeParam; }
Example 3
Source File: InjectionPointInfo.java From quarkus with Apache License 2.0 | 6 votes |
private static Type resolveType(Type type, ClassInfo beanClass, BeanDeployment beanDeployment, Map<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariables) { if (type.kind() == org.jboss.jandex.Type.Kind.TYPE_VARIABLE) { if (resolvedTypeVariables.containsKey(beanClass)) { return resolvedTypeVariables.get(beanClass).getOrDefault(type.asTypeVariable(), type); } } else if (type.kind() == org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE) { ParameterizedType parameterizedType = type.asParameterizedType(); Type[] typeParams = new Type[parameterizedType.arguments().size()]; for (int i = 0; i < typeParams.length; i++) { Type argument = parameterizedType.arguments().get(i); if (argument.kind() == org.jboss.jandex.Type.Kind.TYPE_VARIABLE || argument.kind() == org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE) { typeParams[i] = resolveType(argument, beanClass, beanDeployment, resolvedTypeVariables); } else { typeParams[i] = argument; } } return ParameterizedType.create(parameterizedType.name(), typeParams, parameterizedType.owner()); } return type; }
Example 4
Source File: MethodNameParser.java From quarkus with Apache License 2.0 | 6 votes |
private List<ClassInfo> getMappedSuperClassInfos(IndexView indexView, ClassInfo entityClass) { List<ClassInfo> mappedSuperClassInfos = new ArrayList<>(3); Type superClassType = entityClass.superClassType(); while (superClassType != null && !superClassType.name().equals(DotNames.OBJECT)) { ClassInfo superClass = indexView.getClassByName(entityClass.superName()); if (superClass.classAnnotation(DotNames.JPA_MAPPED_SUPERCLASS) != null) { mappedSuperClassInfos.add(superClass); } if (superClassType.kind() == Kind.CLASS) { superClassType = indexView.getClassByName(superClassType.name()).superClassType(); } else if (superClassType.kind() == Kind.PARAMETERIZED_TYPE) { ParameterizedType parameterizedType = superClassType.asParameterizedType(); superClassType = parameterizedType.owner(); } } if (mappedSuperClassInfos.size() > 0) { return mappedSuperClassInfos; } return Collections.emptyList(); }
Example 5
Source File: Types.java From quarkus with Apache License 2.0 | 6 votes |
static Type resolveTypeParam(Type typeParam, Map<TypeVariable, Type> resolvedTypeParameters, IndexView index) { if (typeParam.kind() == Kind.TYPE_VARIABLE) { return resolvedTypeParameters.getOrDefault(typeParam, typeParam); } else if (typeParam.kind() == Kind.PARAMETERIZED_TYPE) { ParameterizedType parameterizedType = typeParam.asParameterizedType(); ClassInfo classInfo = index.getClassByName(parameterizedType.name()); if (classInfo != null) { List<TypeVariable> typeParameters = classInfo.typeParameters(); List<Type> arguments = parameterizedType.arguments(); Type[] typeParams = new Type[typeParameters.size()]; for (int i = 0; i < typeParameters.size(); i++) { typeParams[i] = resolveTypeParam(arguments.get(i), resolvedTypeParameters, index); } return ParameterizedType.create(parameterizedType.name(), typeParams, null); } } return typeParam; }
Example 6
Source File: EventBusCodecProcessor.java From quarkus with Apache License 2.0 | 6 votes |
private static Type extractPayloadTypeFromParameter(MethodInfo method) { List<Type> parameters = method.parameters(); if (parameters.isEmpty()) { return null; } Type param = method.parameters().get(0); if (param.kind() == Type.Kind.CLASS) { return param; } else if (param.kind() == Type.Kind.PARAMETERIZED_TYPE) { ParameterizedType parameterType = param.asParameterizedType(); if (isMessageClass(parameterType) && !parameterType.arguments().isEmpty()) { return parameterType.arguments().get(0); } else { return parameterType; } } return null; }
Example 7
Source File: RESTEasyExtension.java From quarkus with Apache License 2.0 | 6 votes |
private void scanAsyncResponseProviders(IndexView index) { for (ClassInfo providerClass : index.getAllKnownImplementors(DOTNAME_ASYNC_RESPONSE_PROVIDER)) { for (AnnotationInstance annotation : providerClass.classAnnotations()) { if (annotation.name().equals(DOTNAME_PROVIDER)) { for (Type interf : providerClass.interfaceTypes()) { if (interf.kind() == Type.Kind.PARAMETERIZED_TYPE && interf.name().equals(DOTNAME_ASYNC_RESPONSE_PROVIDER)) { ParameterizedType pType = interf.asParameterizedType(); if (pType.arguments().size() == 1) { Type asyncType = pType.arguments().get(0); asyncTypes.add(asyncType.name()); } } } } } } }
Example 8
Source File: HibernateSearchElasticsearchProcessor.java From quarkus with Apache License 2.0 | 6 votes |
private static void addReflectiveType(IndexView index, Set<DotName> reflectiveClassCollector, Set<Type> reflectiveTypeCollector, Type type) { if (type instanceof VoidType || type instanceof PrimitiveType || type instanceof UnresolvedTypeVariable) { return; } else if (type instanceof ClassType) { ClassInfo classInfo = index.getClassByName(type.name()); addReflectiveClass(index, reflectiveClassCollector, reflectiveTypeCollector, classInfo); } else if (type instanceof ArrayType) { addReflectiveType(index, reflectiveClassCollector, reflectiveTypeCollector, type.asArrayType().component()); } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = type.asParameterizedType(); addReflectiveType(index, reflectiveClassCollector, reflectiveTypeCollector, parameterizedType.owner()); for (Type typeArgument : parameterizedType.arguments()) { addReflectiveType(index, reflectiveClassCollector, reflectiveTypeCollector, typeArgument); } } }
Example 9
Source File: JandexUtil.java From quarkus with Apache License 2.0 | 6 votes |
private static boolean containsTypeParameters(Type type) { switch (type.kind()) { case ARRAY: return containsTypeParameters(type.asArrayType().component()); case PARAMETERIZED_TYPE: ParameterizedType parameterizedType = type.asParameterizedType(); if (parameterizedType.owner() != null && containsTypeParameters(parameterizedType.owner())) return true; return containsTypeParameters(parameterizedType.arguments()); case TYPE_VARIABLE: return true; default: return false; } }
Example 10
Source File: JandexUtil.java From quarkus with Apache License 2.0 | 6 votes |
private static Type mapGenerics(Type type, Map<String, Type> mapping) { switch (type.kind()) { case ARRAY: ArrayType arrayType = type.asArrayType(); return ArrayType.create(mapGenerics(arrayType.component(), mapping), arrayType.dimensions()); case CLASS: return type; case PARAMETERIZED_TYPE: ParameterizedType parameterizedType = type.asParameterizedType(); Type owner = null; if (parameterizedType.owner() != null) { owner = mapGenerics(parameterizedType.owner(), mapping); } return ParameterizedType.create(parameterizedType.name(), mapGenerics(parameterizedType.arguments(), mapping).toArray(new Type[0]), owner); case TYPE_VARIABLE: Type ret = mapping.get(type.asTypeVariable().identifier()); if (ret == null) { throw new IllegalArgumentException("Missing type argument mapping for " + type); } return ret; default: throw new IllegalArgumentException("Illegal type in hierarchy: " + type); } }
Example 11
Source File: ConfigPropertiesUtil.java From quarkus with Apache License 2.0 | 5 votes |
static Type determineSingleGenericType(Type type, DotName declaringClass) { if (!(type.kind() == Type.Kind.PARAMETERIZED_TYPE)) { throw new IllegalArgumentException("Type " + type.name().toString() + " which is used in class " + declaringClass + " must define a generic argument"); } ParameterizedType parameterizedType = type.asParameterizedType(); if (parameterizedType.arguments().size() != 1) { throw new IllegalArgumentException("Type " + type.name().toString() + " which is used in class " + declaringClass + " must define a single generic argument"); } return type.asParameterizedType().arguments().get(0); }
Example 12
Source File: EventBusCodecProcessor.java From quarkus with Apache License 2.0 | 5 votes |
private static Type extractPayloadTypeFromReturn(MethodInfo method) { Type returnType = method.returnType(); if (returnType.kind() == Type.Kind.CLASS) { return returnType; } else if (returnType.kind() == Type.Kind.PARAMETERIZED_TYPE) { ParameterizedType returnedParamType = returnType.asParameterizedType(); if (!returnedParamType.arguments().isEmpty() && (returnedParamType.name().equals(COMPLETION_STAGE) || returnedParamType.name().equals(UNI))) { return returnedParamType.arguments().get(0); } else { return returnedParamType; } } return null; }
Example 13
Source File: RESTEasyExtension.java From quarkus with Apache License 2.0 | 5 votes |
@Override public Type resolveAsyncType(Type type) { if (type.kind() == Type.Kind.PARAMETERIZED_TYPE && asyncTypes.contains(type.name())) { ParameterizedType pType = type.asParameterizedType(); if (pType.arguments().size() == 1) { return pType.arguments().get(0); } } return null; }
Example 14
Source File: HibernateSearchElasticsearchProcessor.java From quarkus with Apache License 2.0 | 5 votes |
private static void addReflectiveClass(IndexView index, Set<DotName> reflectiveClassCollector, Set<Type> reflectiveTypeCollector, ClassInfo classInfo) { if (skipClass(classInfo.name(), reflectiveClassCollector)) { return; } reflectiveClassCollector.add(classInfo.name()); for (ClassInfo subclass : index.getAllKnownSubclasses(classInfo.name())) { reflectiveClassCollector.add(subclass.name()); } for (ClassInfo implementor : index.getAllKnownImplementors(classInfo.name())) { reflectiveClassCollector.add(implementor.name()); } Type superClassType = classInfo.superClassType(); while (superClassType != null && !superClassType.name().toString().equals("java.lang.Object")) { reflectiveClassCollector.add(superClassType.name()); if (superClassType instanceof ClassType) { superClassType = index.getClassByName(superClassType.name()).superClassType(); } else if (superClassType instanceof ParameterizedType) { ParameterizedType parameterizedType = superClassType.asParameterizedType(); for (Type typeArgument : parameterizedType.arguments()) { addReflectiveType(index, reflectiveClassCollector, reflectiveTypeCollector, typeArgument); } superClassType = parameterizedType.owner(); } } }
Example 15
Source File: Types.java From quarkus with Apache License 2.0 | 4 votes |
static ResultHandle getTypeHandle(BytecodeCreator creator, Type type, ResultHandle tccl) { if (Kind.CLASS.equals(type.kind())) { String className = type.asClassType().name().toString(); return doLoadClass(creator, className, tccl); } else if (Kind.TYPE_VARIABLE.equals(type.kind())) { // E.g. T -> new TypeVariableImpl("T") TypeVariable typeVariable = type.asTypeVariable(); ResultHandle boundsHandle; List<Type> bounds = typeVariable.bounds(); if (bounds.isEmpty()) { boundsHandle = creator.newArray(java.lang.reflect.Type.class, creator.load(0)); } else { boundsHandle = creator.newArray(java.lang.reflect.Type.class, creator.load(bounds.size())); for (int i = 0; i < bounds.size(); i++) { creator.writeArrayValue(boundsHandle, i, getTypeHandle(creator, bounds.get(i), tccl)); } } return creator.newInstance( MethodDescriptor.ofConstructor(TypeVariableImpl.class, String.class, java.lang.reflect.Type[].class), creator.load(typeVariable.identifier()), boundsHandle); } else if (Kind.PARAMETERIZED_TYPE.equals(type.kind())) { // E.g. List<String> -> new ParameterizedTypeImpl(List.class, String.class) ParameterizedType parameterizedType = type.asParameterizedType(); List<Type> arguments = parameterizedType.arguments(); ResultHandle typeArgsHandle = creator.newArray(java.lang.reflect.Type.class, creator.load(arguments.size())); for (int i = 0; i < arguments.size(); i++) { creator.writeArrayValue(typeArgsHandle, i, getTypeHandle(creator, arguments.get(i), tccl)); } return creator.newInstance( MethodDescriptor.ofConstructor(ParameterizedTypeImpl.class, java.lang.reflect.Type.class, java.lang.reflect.Type[].class), doLoadClass(creator, parameterizedType.name().toString(), tccl), typeArgsHandle); } else if (Kind.ARRAY.equals(type.kind())) { Type componentType = type.asArrayType().component(); // E.g. String[] -> new GenericArrayTypeImpl(String.class) return creator.newInstance(MethodDescriptor.ofConstructor(GenericArrayTypeImpl.class, java.lang.reflect.Type.class), getTypeHandle(creator, componentType, tccl)); } else if (Kind.WILDCARD_TYPE.equals(type.kind())) { // E.g. ? extends Number -> WildcardTypeImpl.withUpperBound(Number.class) WildcardType wildcardType = type.asWildcardType(); if (wildcardType.superBound() == null) { return creator.invokeStaticMethod( MethodDescriptor.ofMethod(WildcardTypeImpl.class, "withUpperBound", java.lang.reflect.WildcardType.class, java.lang.reflect.Type.class), getTypeHandle(creator, wildcardType.extendsBound(), tccl)); } else { return creator.invokeStaticMethod( MethodDescriptor.ofMethod(WildcardTypeImpl.class, "withLowerBound", java.lang.reflect.WildcardType.class, java.lang.reflect.Type.class), getTypeHandle(creator, wildcardType.superBound(), tccl)); } } else if (Kind.PRIMITIVE.equals(type.kind())) { switch (type.asPrimitiveType().primitive()) { case INT: return creator.loadClass(int.class); case LONG: return creator.loadClass(long.class); case BOOLEAN: return creator.loadClass(boolean.class); case BYTE: return creator.loadClass(byte.class); case CHAR: return creator.loadClass(char.class); case DOUBLE: return creator.loadClass(double.class); case FLOAT: return creator.loadClass(float.class); case SHORT: return creator.loadClass(short.class); default: throw new IllegalArgumentException("Unsupported primitive type: " + type); } } else { throw new IllegalArgumentException("Unsupported bean type: " + type.kind() + ", " + type); } }
Example 16
Source File: TypesTest.java From quarkus with Apache License 2.0 | 4 votes |
@Test public void testGetTypeClosure() throws IOException { IndexView index = Basics.index(Foo.class, Baz.class, Producer.class, Object.class, List.class, Collection.class, Iterable.class); DotName bazName = DotName.createSimple(Baz.class.getName()); DotName fooName = DotName.createSimple(Foo.class.getName()); DotName producerName = DotName.createSimple(Producer.class.getName()); ClassInfo fooClass = index.getClassByName(fooName); Map<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariables = new HashMap<>(); BeanDeployment dummyDeployment = BeanProcessor.builder().setIndex(index).build().getBeanDeployment(); // Baz, Foo<String>, Object Set<Type> bazTypes = Types.getTypeClosure(index.getClassByName(bazName), null, Collections.emptyMap(), dummyDeployment, resolvedTypeVariables::put); assertEquals(3, bazTypes.size()); assertTrue(bazTypes.contains(Type.create(bazName, Kind.CLASS))); assertTrue(bazTypes.contains(ParameterizedType.create(fooName, new Type[] { Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS) }, null))); assertEquals(resolvedTypeVariables.size(), 1); assertTrue(resolvedTypeVariables.containsKey(fooClass)); assertEquals(resolvedTypeVariables.get(fooClass).get(fooClass.typeParameters().get(0)), Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS)); resolvedTypeVariables.clear(); // Foo<T>, Object Set<Type> fooTypes = Types.getClassBeanTypeClosure(fooClass, dummyDeployment); assertEquals(2, fooTypes.size()); for (Type t : fooTypes) { if (t.kind().equals(Kind.PARAMETERIZED_TYPE)) { ParameterizedType fooType = t.asParameterizedType(); assertEquals("T", fooType.arguments().get(0).asTypeVariable().identifier()); assertEquals(DotNames.OBJECT, fooType.arguments().get(0).asTypeVariable().bounds().get(0).name()); } } ClassInfo producerClass = index.getClassByName(producerName); String producersName = "produce"; MethodInfo producerMethod = producerClass.method(producersName); // Object is the sole type Set<Type> producerMethodTypes = Types.getProducerMethodTypeClosure(producerMethod, dummyDeployment); assertEquals(1, producerMethodTypes.size()); // Object is the sole type FieldInfo producerField = producerClass.field(producersName); Set<Type> producerFieldTypes = Types.getProducerFieldTypeClosure(producerField, dummyDeployment); assertEquals(1, producerFieldTypes.size()); }