Java Code Examples for org.jboss.jandex.Type#kind()

The following examples show how to use org.jboss.jandex.Type#kind() . 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: JandexUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("incomplete-switch")
public static String getBoxedTypeName(Type type) {
    switch (type.kind()) {
        case PRIMITIVE:
            switch (type.asPrimitiveType().primitive()) {
                case BOOLEAN:
                    return "java.lang.Boolean";
                case BYTE:
                    return "java.lang.Byte";
                case CHAR:
                    return "java.lang.Character";
                case DOUBLE:
                    return "java.lang.Double";
                case FLOAT:
                    return "java.lang.Float";
                case INT:
                    return "java.lang.Integer";
                case LONG:
                    return "java.lang.Long";
                case SHORT:
                    return "java.lang.Short";
            }
    }
    return type.toString();
}
 
Example 2
Source File: InjectionPointInfo.java    From quarkus with Apache License 2.0 6 votes vote down vote up
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 3
Source File: ParameterProcessor.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Scan and parse a Spring DefaultValue property on the mapping annotation.
 * If the target is a Java primitive, the value will be parsed into an equivalent
 * wrapper object.
 *
 * @param target target annotated with a Spring mapping
 * @return the default value
 */
static Object getDefaultValue(AnnotationTarget target) {
    AnnotationInstance defaultValueAnno = TypeUtil.getAnnotation(target, SpringConstants.QUERY_PARAM);
    Object defaultValue = null;

    if (defaultValueAnno != null) {
        AnnotationValue value = defaultValueAnno.value("defaultValue");
        if (value != null && !value.asString().isEmpty()) {
            String defaultValueString = value.asString();
            defaultValue = defaultValueString;
            Type targetType = getType(target);

            if (targetType != null && targetType.kind() == Type.Kind.PRIMITIVE) {
                Primitive primitive = targetType.asPrimitiveType().primitive();
                Object primitiveValue = primitiveToObject(primitive, defaultValueString);

                if (primitiveValue != null) {
                    defaultValue = primitiveValue;
                }
            }
        }
    }
    return defaultValue;
}
 
Example 4
Source File: SchemaFactory.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Introspect into the given Class to generate a Schema model. The boolean indicates
 * whether this class type should be turned into a reference.
 *
 * @param index the index of classes being scanned
 * @param type the implementation type of the item to scan
 * @param schemaReferenceSupported
 */
static Schema readClassSchema(IndexView index, Type type, boolean schemaReferenceSupported) {
    if (type == null) {
        return null;
    }
    Schema schema;
    if (type.kind() == Type.Kind.ARRAY) {
        schema = new SchemaImpl().type(SchemaType.ARRAY);
        ArrayType array = type.asArrayType();
        int dimensions = array.dimensions();
        Type componentType = array.component();

        if (dimensions > 1) {
            // Recurse using a new array type with dimensions decremented
            schema.items(readClassSchema(index, ArrayType.create(componentType, dimensions - 1), schemaReferenceSupported));
        } else {
            // Recurse using the type of the array elements
            schema.items(readClassSchema(index, componentType, schemaReferenceSupported));
        }
    } else if (type.kind() == Type.Kind.PRIMITIVE) {
        schema = OpenApiDataObjectScanner.process(type.asPrimitiveType());
    } else {
        schema = introspectClassToSchema(index, type.asClassType(), schemaReferenceSupported);
    }
    return schema;
}
 
Example 5
Source File: ConfigBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private boolean isHandledByProducers(Type type) {
    if (type.kind() == Kind.ARRAY) {
        return false;
    }
    if (type.kind() == Kind.PRIMITIVE) {
        return true;
    }
    return DotNames.STRING.equals(type.name()) ||
            DotNames.OPTIONAL.equals(type.name()) ||
            SET_NAME.equals(type.name()) ||
            LIST_NAME.equals(type.name()) ||
            DotNames.LONG.equals(type.name()) ||
            DotNames.FLOAT.equals(type.name()) ||
            DotNames.INTEGER.equals(type.name()) ||
            DotNames.BOOLEAN.equals(type.name()) ||
            DotNames.DOUBLE.equals(type.name()) ||
            DotNames.SHORT.equals(type.name()) ||
            DotNames.BYTE.equals(type.name()) ||
            DotNames.CHARACTER.equals(type.name());
}
 
Example 6
Source File: AsmUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the bytecode instruction to load the given Jandex Type. This returns the specialised
 * bytecodes <tt>ILOAD, DLOAD, FLOAD and LLOAD</tt> for primitives, or <tt>ALOAD</tt> otherwise.
 * 
 * @param jandexType The Jandex Type whose load instruction to return.
 * @return The bytecode instruction to load the given Jandex Type.
 */
public static int getLoadOpcode(Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
            case BYTE:
            case SHORT:
            case INT:
            case CHAR:
                return Opcodes.ILOAD;
            case DOUBLE:
                return Opcodes.DLOAD;
            case FLOAT:
                return Opcodes.FLOAD;
            case LONG:
                return Opcodes.LLOAD;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    }
    return Opcodes.ALOAD;
}
 
Example 7
Source File: TypeResolver.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve a type against this {@link TypeResolver}'s resolution stack
 *
 * @param fieldType type to resolve
 * @return resolved type (if found)
 */
public Type getResolvedType(Type fieldType) {
    Type current = TypeUtil.resolveWildcard(fieldType);

    for (Map<String, Type> map : resolutionStack) {
        String varName = null;

        switch (current.kind()) {
            case TYPE_VARIABLE:
                varName = current.asTypeVariable().identifier();
                break;
            case UNRESOLVED_TYPE_VARIABLE:
                varName = current.asUnresolvedTypeVariable().identifier();
                break;
            default:
                break;
        }

        // Look in next entry map-set if the name is present.
        if (varName != null && map.containsKey(varName)) {
            current = map.get(varName);
        }
    }
    return current;
}
 
Example 8
Source File: CustomQueryMethodsAdder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private Type verifyQueryResultType(Type t) {
    if (isIntLongOrBoolean(t.name())) {
        return t;
    }
    if (t.kind() == Kind.ARRAY) {
        return verifyQueryResultType(t.asArrayType().component());
    } else if (t.kind() == Kind.PARAMETERIZED_TYPE) {
        List<Type> list = t.asParameterizedType().arguments();
        if (list.size() == 1) {
            return verifyQueryResultType(list.get(0));
        } else {
            for (Type x : list) {
                verifyQueryResultType(x);
            }
            return t;
        }
    } else if (!DotNames.OBJECT.equals(t.name())) {
        ClassInfo typeClassInfo = index.getClassByName(t.name());
        if (typeClassInfo == null) {
            throw new IllegalStateException(t.name() + " was not part of the Quarkus index");
        }
    }
    return t;
}
 
Example 9
Source File: Types.java    From quarkus with Apache License 2.0 6 votes vote down vote up
static Set<Type> getProducerFieldTypeClosure(FieldInfo producerField, BeanDeployment beanDeployment) {
    Set<Type> types;
    Type fieldType = producerField.type();
    if (fieldType.kind() == Kind.PRIMITIVE || fieldType.kind() == Kind.ARRAY) {
        types = new HashSet<>();
        types.add(fieldType);
        types.add(OBJECT_TYPE);
    } else {
        ClassInfo fieldClassInfo = getClassByName(beanDeployment.getIndex(), producerField.type());
        if (fieldClassInfo == null) {
            throw new IllegalArgumentException("Producer field type not found in index: " + producerField.type().name());
        }
        if (Kind.CLASS.equals(fieldType.kind())) {
            types = getTypeClosure(fieldClassInfo, producerField, Collections.emptyMap(), beanDeployment, null);
        } else if (Kind.PARAMETERIZED_TYPE.equals(fieldType.kind())) {
            types = getTypeClosure(fieldClassInfo, producerField,
                    buildResolvedMap(fieldType.asParameterizedType().arguments(), fieldClassInfo.typeParameters(),
                            Collections.emptyMap(), beanDeployment.getIndex()),
                    beanDeployment, null);
        } else {
            throw new IllegalArgumentException("Unsupported return type");
        }
    }
    return restrictBeanTypes(types, beanDeployment.getAnnotations(producerField));
}
 
Example 10
Source File: Methods.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static DotName toRawType(Type a) {
    switch (a.kind()) {
        case CLASS:
        case PRIMITIVE:
        case ARRAY:
            return a.name();
        case PARAMETERIZED_TYPE:
            return a.asParameterizedType().name();
        case TYPE_VARIABLE:
        case UNRESOLVED_TYPE_VARIABLE:
        case WILDCARD_TYPE:
        default:
            return DotNames.OBJECT;
    }
}
 
Example 11
Source File: SchemaFactory.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Jandex type to a {@link Schema} model.
 * 
 * @param index the index of classes being scanned
 * @param type the implementation type of the item to scan
 * @param extensions list of AnnotationScannerExtensions
 * @return Schema model
 */
public static Schema typeToSchema(IndexView index, Type type, List<AnnotationScannerExtension> extensions) {
    Schema schema = null;

    AnnotationScanner annotationScanner = CurrentScannerInfo.getCurrentAnnotationScanner();

    if (TypeUtil.isOptional(type)) {
        // Recurse using the optional's type
        return typeToSchema(index, TypeUtil.getOptionalType(type), extensions);
    } else if (annotationScanner.isWrapperType(type)) {
        // Recurse using the wrapped type
        return typeToSchema(index, annotationScanner.unwrapType(type), extensions);
    } else if (type.kind() == Type.Kind.ARRAY) {
        schema = new SchemaImpl().type(SchemaType.ARRAY);
        ArrayType array = type.asArrayType();
        int dimensions = array.dimensions();
        Type componentType = array.component();

        if (dimensions > 1) {
            // Recurse using a new array type with dimensions decremented
            schema.items(typeToSchema(index, ArrayType.create(componentType, dimensions - 1), extensions));
        } else {
            // Recurse using the type of the array elements
            schema.items(typeToSchema(index, componentType, extensions));
        }
    } else if (type.kind() == Type.Kind.CLASS) {
        schema = introspectClassToSchema(index, type.asClassType(), true);
    } else if (type.kind() == Type.Kind.PRIMITIVE) {
        schema = OpenApiDataObjectScanner.process(type.asPrimitiveType());
    } else {
        schema = otherTypeToSchema(index, type, extensions);
    }

    return schema;
}
 
Example 12
Source File: Beans.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void fetchType(Type type, BeanDeployment beanDeployment) {
    if (type == null) {
        return;
    }
    if (type.kind() == Type.Kind.CLASS) {
        // Index the class additionally if needed
        getClassByName(beanDeployment.getIndex(), type.name());
    } else {
        analyzeType(type, beanDeployment);
    }
}
 
Example 13
Source File: AsmUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the right unboxing method for the given Jandex Type if it is a primitive.
 * 
 * @param mv The MethodVisitor on which to visit the unboxing instructions
 * @param jandexType The Jandex Type to unbox if it is a primitive.
 */
public static void unboxIfRequired(MethodVisitor mv, Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
                unbox(mv, "java/lang/Boolean", "booleanValue", "Z");
                break;
            case BYTE:
                unbox(mv, "java/lang/Byte", "byteValue", "B");
                break;
            case CHAR:
                unbox(mv, "java/lang/Character", "charValue", "C");
                break;
            case DOUBLE:
                unbox(mv, "java/lang/Double", "doubleValue", "D");
                break;
            case FLOAT:
                unbox(mv, "java/lang/Float", "floatValue", "F");
                break;
            case INT:
                unbox(mv, "java/lang/Integer", "intValue", "I");
                break;
            case LONG:
                unbox(mv, "java/lang/Long", "longValue", "J");
                break;
            case SHORT:
                unbox(mv, "java/lang/Short", "shortValue", "S");
                break;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    }
}
 
Example 14
Source File: HibernateValidatorProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static DotName getClassName(Type type) {
    switch (type.kind()) {
        case CLASS:
        case PARAMETERIZED_TYPE:
            return type.name();
        case ARRAY:
            return getClassName(type.asArrayType().component());
        default:
            return null;
    }
}
 
Example 15
Source File: IndexClassLookupUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param index
 * @param type
 * @return the class for the given type or {@code null} for primitives, arrays and
 */
static ClassInfo getClassByName(IndexView index, Type type) {
    if (type != null && (type.kind() == Kind.CLASS || type.kind() == Kind.PARAMETERIZED_TYPE)) {
        return getClassByName(index, type.name());
    }
    return null;
}
 
Example 16
Source File: Types.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static Type box(Type type) {
    if (type.kind() == Kind.PRIMITIVE) {
        return box(type.asPrimitiveType().primitive());
    }
    return type;
}
 
Example 17
Source File: Types.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static Type box(Type type) {
    if (type.kind() == Kind.PRIMITIVE) {
        return box(type.asPrimitiveType().primitive());
    }
    return type;
}
 
Example 18
Source File: AsmUtil.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Invokes the proper LDC Class Constant instructions for the given Jandex Type. This will properly create LDC instructions
 * for array types, class/parameterized classes, and primitive types by loading their equivalent <tt>TYPE</tt>
 * constants in their box types, as well as type variables (using the first bound or Object) and Void.
 * 
 * @param mv The MethodVisitor on which to visit the LDC instructions
 * @param jandexType the Jandex Type whose Class Constant to load.
 */
public static void visitLdc(MethodVisitor mv, Type jandexType) {
    switch (jandexType.kind()) {
        case ARRAY:
            mv.visitLdcInsn(org.objectweb.asm.Type.getType(jandexType.name().toString('/').replace('.', '/')));
            break;
        case CLASS:
        case PARAMETERIZED_TYPE:
            mv.visitLdcInsn(org.objectweb.asm.Type.getType("L" + jandexType.name().toString('/') + ";"));
            break;
        case PRIMITIVE:
            switch (jandexType.asPrimitiveType().primitive()) {
                case BOOLEAN:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;");
                    break;
                case BYTE:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Byte", "TYPE", "Ljava/lang/Class;");
                    break;
                case CHAR:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;");
                    break;
                case DOUBLE:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;");
                    break;
                case FLOAT:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;");
                    break;
                case INT:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");
                    break;
                case LONG:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;");
                    break;
                case SHORT:
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
                    break;
                default:
                    throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
            }
            break;
        case TYPE_VARIABLE:
            List<Type> bounds = jandexType.asTypeVariable().bounds();
            if (bounds.isEmpty())
                mv.visitLdcInsn(org.objectweb.asm.Type.getType(Object.class));
            else
                visitLdc(mv, bounds.get(0));
            break;
        case UNRESOLVED_TYPE_VARIABLE:
            mv.visitLdcInsn(org.objectweb.asm.Type.getType(Object.class));
            break;
        case VOID:
            mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;");
            break;
        case WILDCARD_TYPE:
            visitLdc(mv, jandexType.asWildcardType().extendsBound());
            break;
        default:
            throw new IllegalArgumentException("Unknown jandex type: " + jandexType);
    }
}
 
Example 19
Source File: ConfigBuildStep.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
void analyzeConfigPropertyInjectionPoints(BeanRegistrationPhaseBuildItem beanRegistrationPhase,
        BuildProducer<ConfigPropertyBuildItem> configProperties,
        BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<BeanConfiguratorBuildItem> beanConfigurators) {

    Set<Type> customBeanTypes = new HashSet<>();

    for (InjectionPointInfo injectionPoint : beanRegistrationPhase.getContext().get(BuildExtension.Key.INJECTION_POINTS)) {
        if (injectionPoint.hasDefaultedQualifier()) {
            // Defaulted qualifier means no @ConfigProperty
            continue;
        }

        AnnotationInstance configProperty = injectionPoint.getRequiredQualifier(CONFIG_PROPERTY_NAME);
        if (configProperty != null) {
            AnnotationValue nameValue = configProperty.value("name");
            AnnotationValue defaultValue = configProperty.value("defaultValue");
            String propertyName;
            if (nameValue != null) {
                propertyName = nameValue.asString();
            } else {
                // org.acme.Foo.config
                if (injectionPoint.isField()) {
                    FieldInfo field = injectionPoint.getTarget().asField();
                    propertyName = getPropertyName(field.name(), field.declaringClass());
                } else if (injectionPoint.isParam()) {
                    MethodInfo method = injectionPoint.getTarget().asMethod();
                    propertyName = getPropertyName(method.parameterName(injectionPoint.getPosition()),
                            method.declaringClass());
                } else {
                    throw new IllegalStateException("Unsupported injection point target: " + injectionPoint);
                }
            }

            // Register a custom bean for injection points that are not handled by ConfigProducer
            Type requiredType = injectionPoint.getRequiredType();
            if (!isHandledByProducers(requiredType)) {
                customBeanTypes.add(requiredType);
            }

            if (DotNames.OPTIONAL.equals(requiredType.name())) {
                // Never validate Optional values
                continue;
            }
            if (defaultValue != null && !ConfigProperty.UNCONFIGURED_VALUE.equals(defaultValue.asString())) {
                // No need to validate properties with default values
                continue;
            }

            configProperties.produce(new ConfigPropertyBuildItem(propertyName, requiredType));
        }
    }

    for (Type type : customBeanTypes) {
        if (type.kind() != Kind.ARRAY) {
            // Implicit converters are most likely used
            reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, type.name().toString()));
        }
        beanConfigurators.produce(new BeanConfiguratorBuildItem(beanRegistrationPhase.getContext().configure(
                type.kind() == Kind.ARRAY ? DotName.createSimple(ConfigBeanCreator.class.getName()) : type.name())
                .creator(ConfigBeanCreator.class)
                .providerType(type)
                .types(type)
                .qualifiers(AnnotationInstance.create(CONFIG_PROPERTY_NAME, null, Collections.emptyList()))
                .param("requiredType", type.name().toString())));
    }
}
 
Example 20
Source File: TypeUtil.java    From smallrye-open-api with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves the default schema attributes for the given type, wrapped in
 * a TypeWithFormat instance.
 *
 * XXX: Consider additional check for subclasses of java.lang.Number and
 * implementations of java.lang.CharSequence.
 *
 * @param type the type
 * @return the default schema attributes for the given type, wrapped in
 *         a TypeWithFormat instance
 */
private static TypeWithFormat getTypeFormat(Type type) {
    if (type.kind() == Type.Kind.ARRAY) {
        return arrayFormat();
    }

    return TYPE_MAP.getOrDefault(getName(type), objectFormat());
}