Search in sources :

Example 1 with Type

use of org.jboss.jandex.Type in project wildfly by wildfly.

the class PassivationAnnotationParsingProcessor method processPassivation.

private void processPassivation(final EEModuleDescription eeModuleDescription, final AnnotationTarget target, final DotName annotationType, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
    if (!(target instanceof MethodInfo)) {
        throw EeLogger.ROOT_LOGGER.methodOnlyAnnotation(annotationType);
    }
    final MethodInfo methodInfo = MethodInfo.class.cast(target);
    final ClassInfo classInfo = methodInfo.declaringClass();
    final EEModuleClassDescription classDescription = eeModuleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
    final Type[] args = methodInfo.args();
    if (args.length > 1) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidNumberOfArguments(methodInfo.name(), annotationType, classInfo.name()));
        return;
    } else if (args.length == 1 && !args[0].name().toString().equals(InvocationContext.class.getName())) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidSignature(methodInfo.name(), annotationType, classInfo.name(), "void methodName(InvocationContext ctx)"));
        return;
    }
    final MethodIdentifier methodIdentifier;
    if (args.length == 0) {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name());
    } else {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name(), InvocationContext.class);
    }
    final InterceptorClassDescription.Builder builder = InterceptorClassDescription.builder(classDescription.getInterceptorClassDescription());
    if (annotationType == POST_ACTIVATE) {
        builder.setPostActivate(methodIdentifier);
    } else {
        builder.setPrePassivate(methodIdentifier);
    }
    classDescription.setInterceptorClassDescription(builder.build());
}
Also used : Type(org.jboss.jandex.Type) InterceptorClassDescription(org.jboss.as.ee.component.interceptors.InterceptorClassDescription) MethodInfo(org.jboss.jandex.MethodInfo) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) InvocationContext(javax.interceptor.InvocationContext) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with Type

use of org.jboss.jandex.Type in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method readSchemaAnnotatedField.

private Schema readSchemaAnnotatedField(AnnotationInstance annotation, String name, Type type, TypeResolver typeResolver, Schema parent, Schema schema, PathEntry pathEntry) {
    if (annotation == null) {
        return parent;
    }
    LOG.debugv("Processing @Schema annotation {0} on a field {1}", annotation, name);
    // Schemas can be hidden. Skip if that's the case.
    Boolean isHidden = JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_HIDDEN);
    if (isHidden != null && isHidden == Boolean.TRUE) {
        return parent;
    }
    // Required is false by default.
    if (JandexUtil.booleanValueWithDefault(annotation, OpenApiConstants.PROP_REQUIRED)) {
        parent.addRequired(name);
    }
    // Type could be replaced (e.g. generics).
    Type postProcessedField = processType(type, typeResolver, schema, pathEntry);
    // TypeFormat pair contains mappings for Java <-> OAS types and formats.
    TypeUtil.TypeWithFormat typeFormat = TypeUtil.getTypeFormat(postProcessedField);
    // Provide inferred type and format if relevant.
    Map<String, Object> overrides = new HashMap<>();
    overrides.put(OpenApiConstants.PROP_TYPE, typeFormat.getSchemaType());
    overrides.put(OpenApiConstants.PROP_FORMAT, typeFormat.getFormat().format());
    return SchemaFactory.readSchema(index, schema, annotation, overrides);
}
Also used : PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) HashMap(java.util.HashMap) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)

Example 3 with Type

use of org.jboss.jandex.Type in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method resolveTypeVariable.

private Type resolveTypeVariable(TypeResolver typeResolver, Schema schema, PathEntry pathEntry, Type fieldType) {
    // Type variable (e.g. A in Foo<A>)
    Type resolvedType = typeResolver.getResolvedType(fieldType);
    LOG.debugv("Resolved type {0} -> {1}", fieldType, resolvedType);
    if (isTerminalType(resolvedType) || getClassByName(resolvedType) == null) {
        LOG.tracev("Is a terminal type {0}", resolvedType);
        TypeUtil.TypeWithFormat replacement = TypeUtil.getTypeFormat(resolvedType);
        schema.setType(replacement.getSchemaType());
        schema.setFormat(replacement.getFormat().format());
    } else {
        LOG.debugv("Attempting to do TYPE_VARIABLE substitution: {0} -> {1}", fieldType, resolvedType);
        if (indexContains(resolvedType)) {
            // Look up the resolved type.
            ClassInfo klazz = getClassByName(resolvedType);
            PathEntry entry = PathEntry.leafNode(pathEntry, klazz, resolvedType, schema);
            path.push(entry);
        } else {
            LOG.debugv("Class for type {0} not available", resolvedType);
        }
    }
    return resolvedType;
}
Also used : PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil) ClassInfo(org.jboss.jandex.ClassInfo)

Example 4 with Type

use of org.jboss.jandex.Type in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method readUnannotatedField.

private void readUnannotatedField(TypeResolver typeResolver, Type type, Schema schema, PathEntry pathEntry) {
    if (!shouldInferUnannotatedFields()) {
        return;
    }
    LOG.debugv("Processing unannotated field {0}", type);
    Type processedType = processType(type, typeResolver, schema, pathEntry);
    TypeUtil.TypeWithFormat typeFormat = TypeUtil.getTypeFormat(processedType);
    schema.setType(typeFormat.getSchemaType());
    if (typeFormat.getFormat().hasFormat()) {
        schema.setFormat(typeFormat.getFormat().format());
    }
}
Also used : PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)

Example 5 with Type

use of org.jboss.jandex.Type in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method readParamType.

private Type readParamType(PathEntry pathEntry, Schema schema, ParameterizedType pType, TypeResolver typeResolver) {
    LOG.debugv("Processing parameterized type {0}", pType);
    // If it's a collection, we should treat it as an array.
    if (isA(pType, COLLECTION_TYPE)) {
        // TODO maybe also Iterable?
        LOG.debugv("Processing Java Collection. Will treat as an array.");
        SchemaImpl arraySchema = new SchemaImpl();
        schema.type(Schema.SchemaType.ARRAY);
        schema.items(arraySchema);
        // Should only have one arg for collection.
        Type arg = pType.arguments().get(0);
        if (isTerminalType(arg)) {
            TypeUtil.TypeWithFormat terminalType = TypeUtil.getTypeFormat(arg);
            arraySchema.type(terminalType.getSchemaType());
            arraySchema.format(terminalType.getFormat().format());
        } else if (arg.kind() == Type.Kind.TYPE_VARIABLE || arg.kind() == Type.Kind.UNRESOLVED_TYPE_VARIABLE || arg.kind() == Type.Kind.WILDCARD_TYPE) {
            Type resolved = resolveTypeVariable(typeResolver, arraySchema, pathEntry, arg);
            if (indexContains(resolved)) {
                arraySchema.type(Schema.SchemaType.OBJECT);
            }
        } else if (indexContains(arg)) {
            arraySchema.type(Schema.SchemaType.OBJECT);
            pushFieldToPath(pathEntry, arg, arraySchema);
        }
        // Representing collection as JSON array
        return ARRAY_TYPE_OBJECT;
    } else if (isA(pType, MAP_TYPE)) {
        LOG.debugv("Processing Map. Will treat as an object.");
        schema.type(Schema.SchemaType.OBJECT);
        if (pType.arguments().size() == 2) {
            Type valueType = pType.arguments().get(1);
            SchemaImpl propsSchema = new SchemaImpl();
            if (isTerminalType(valueType)) {
                TypeUtil.TypeWithFormat tf = TypeUtil.getTypeFormat(valueType);
                propsSchema.setType(tf.getSchemaType());
                propsSchema.setFormat(tf.getFormat().format());
            } else if (valueType.kind() == Type.Kind.TYPE_VARIABLE || valueType.kind() == Type.Kind.UNRESOLVED_TYPE_VARIABLE || valueType.kind() == Type.Kind.WILDCARD_TYPE) {
                Type resolved = resolveTypeVariable(typeResolver, propsSchema, pathEntry, valueType);
                if (indexContains(resolved)) {
                    propsSchema.type(Schema.SchemaType.OBJECT);
                }
            } else if (indexContains(valueType)) {
                propsSchema.type(Schema.SchemaType.OBJECT);
                pushFieldToPath(pathEntry, valueType, propsSchema);
            }
            schema.additionalProperties(propsSchema);
        }
        return OBJECT_TYPE;
    } else {
        // This attempts to allow us to resolve the types generically.
        ClassInfo klazz = getClassByName(pType);
        // Build mapping of class's type variables (e.g. A, B) to resolved variables
        // Resolved variables could be any type (e.g. String, another param type, etc)
        PathEntry pair = new PathEntry(pathEntry, klazz, pType, schema);
        path.push(pair);
        return pType;
    }
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

Type (org.jboss.jandex.Type)28 Schema (org.eclipse.microprofile.openapi.models.media.Schema)15 Test (org.junit.Test)11 ClassType (org.jboss.jandex.ClassType)10 ClassInfo (org.jboss.jandex.ClassInfo)9 ParameterizedType (org.jboss.jandex.ParameterizedType)7 PrimitiveType (org.jboss.jandex.PrimitiveType)6 ArrayType (org.jboss.jandex.ArrayType)5 MethodInfo (org.jboss.jandex.MethodInfo)4 TypeUtil (org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)3 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)3 InterceptorClassDescription (org.jboss.as.ee.component.interceptors.InterceptorClassDescription)3 MethodIdentifier (org.jboss.invocation.proxy.MethodIdentifier)3 FieldInfo (org.jboss.jandex.FieldInfo)3 RefType (org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.RefType)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2