Search in sources :

Example 1 with ClassModel

use of org.glassfish.hk2.classmodel.reflect.ClassModel in project Payara by payara.

the class ApplicationProcessor method createSchema.

private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type, ExtensibleType clazz, Collection<ParameterizedInterfaceModel> classParameterizedTypes) {
    if (schema == null) {
        schema = new SchemaImpl();
    }
    SchemaType schemaType = ModelUtils.getSchemaType(type, context);
    // If the annotated element is the same type as the reference class, return a null schema
    if (schemaType == SchemaType.OBJECT && type.getType() != null && type.getType().equals(clazz)) {
        schema.setType(null);
        schema.setItems(null);
        return schema;
    }
    if (type.getType() == null) {
        ParameterizedInterfaceModel classParameterizedType = findParameterizedModelFromGenerics(clazz, classParameterizedTypes, type);
        String typeName = null;
        if (type.getTypeName() != null) {
            typeName = type.getTypeName();
        }
        if ((typeName == null || Object.class.getName().equals(typeName)) && classParameterizedType != null) {
            typeName = classParameterizedType.getRawInterfaceName();
        }
        schemaType = ModelUtils.getSchemaType(typeName, context);
        if (schema.getType() == null) {
            schema.setType(schemaType);
        }
        Schema containerSchema = schema;
        if (schemaType == SchemaType.ARRAY) {
            containerSchema = new SchemaImpl();
            schema.setItems(containerSchema);
        }
        if (classParameterizedType != null) {
            Collection<ParameterizedInterfaceModel> genericTypes = classParameterizedType.getParametizedTypes();
            if (genericTypes.isEmpty()) {
                if (insertObjectReference(context, containerSchema, classParameterizedType.getRawInterface(), classParameterizedType.getRawInterfaceName())) {
                    containerSchema.setType(null);
                    containerSchema.setItems(null);
                }
            } else if (classParameterizedType.getRawInterface() instanceof ClassModel) {
                visitSchemaClass(containerSchema, null, (ClassModel) classParameterizedType.getRawInterface(), genericTypes, context);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { classParameterizedType.getRawInterface() });
            }
        } else if (!type.getParameterizedTypes().isEmpty()) {
            List<ParameterizedType> genericTypes = type.getParameterizedTypes();
            if (ModelUtils.isMap(typeName, context) && genericTypes.size() == 2) {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
                containerSchema = new SchemaImpl();
                schema.setAdditionalPropertiesSchema(containerSchema);
                createSchema(containerSchema, context, genericTypes.get(1), clazz, classParameterizedTypes);
            } else {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
            }
        } else {
            return createSchema(containerSchema, context, type);
        }
        return schema;
    }
    return createSchema(schema, context, type);
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) List(java.util.List) ArrayList(java.util.ArrayList) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Example 2 with ClassModel

use of org.glassfish.hk2.classmodel.reflect.ClassModel in project Payara by payara.

the class ApplicationProcessor method insertObjectReference.

/**
 * Replace the object in the referee with a reference, and create the
 * reference in the API.
 *
 * @param context the API context.
 * @param referee the object containing the reference.
 * @param referenceClass the class of the object being referenced.
 * @return if the reference has been created.
 */
private boolean insertObjectReference(ApiContext context, Reference<?> referee, AnnotatedElement referenceClass, String referenceClassName) {
    // Firstly check if it's been already defined (i.e. config property definition)
    for (Entry<String, Schema> schemaEntry : context.getApi().getComponents().getSchemas().entrySet()) {
        final Schema entryValue = schemaEntry.getValue();
        if (entryValue instanceof SchemaImpl) {
            final SchemaImpl entryValueImpl = (SchemaImpl) entryValue;
            final String implementationClass = entryValueImpl.getImplementation();
            if (implementationClass != null && implementationClass.equals(referenceClassName)) {
                referee.setRef(schemaEntry.getKey());
                return true;
            }
        }
    }
    // If the object is a java core class
    if (referenceClassName == null || referenceClassName.startsWith("java.")) {
        return false;
    }
    // If the object is a Java EE object type
    if (referenceClassName.startsWith("javax.")) {
        return false;
    }
    // Check the class exists in the application
    if (!context.isApplicationType(referenceClassName)) {
        return false;
    }
    if (referenceClass != null && referenceClass instanceof ExtensibleType) {
        ExtensibleType referenceClassType = (ExtensibleType) referenceClass;
        final AnnotationModel schemaAnnotation = context.getAnnotationInfo(referenceClassType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        String schemaName = ModelUtils.getSchemaName(context, referenceClass);
        // Set the reference name
        referee.setRef(schemaName);
        Schema schema = context.getApi().getComponents().getSchemas().get(schemaName);
        if (schema == null) {
            // Create the schema
            if (context.isAllowedType(referenceClassType)) {
                visitSchema(schemaAnnotation, referenceClassType, context);
            } else if (referenceClassType instanceof ClassModel) {
                apiWalker.processAnnotation((ClassModel) referenceClassType, this);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { referenceClassName });
            }
        }
        return true;
    }
    return false;
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType)

Example 3 with ClassModel

use of org.glassfish.hk2.classmodel.reflect.ClassModel in project Payara by payara.

the class OpenApiContext method generateResourceMapping.

/**
 * Generates a map listing the location each resource class is mapped to.
 */
private Map<String, Set<Type>> generateResourceMapping() {
    Set<Type> classList = new HashSet<>();
    Map<String, Set<Type>> mapping = new HashMap<>();
    for (Type type : allowedTypes) {
        if (type instanceof ClassModel) {
            ClassModel classModel = (ClassModel) type;
            if (classModel.getAnnotation(ApplicationPath.class.getName()) != null) {
                // Produce the mapping
                AnnotationModel annotation = classModel.getAnnotation(ApplicationPath.class.getName());
                String key = annotation.getValue("value", String.class);
                Set<Type> resourceClasses = new HashSet<>();
                mapping.put(key, resourceClasses);
                try {
                    Class<?> clazz = appClassLoader.loadClass(classModel.getName());
                    Application app = (Application) clazz.newInstance();
                    // Add all classes contained in the application
                    resourceClasses.addAll(app.getClasses().stream().map(Class::getName).filter(// Remove all Jersey providers
                    name -> !name.startsWith("org.glassfish.jersey")).map(allTypes::getBy).filter(Objects::nonNull).collect(toSet()));
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
                    LOGGER.log(WARNING, "Unable to initialise application class.", ex);
                }
            } else {
                classList.add(classModel);
            }
        }
    }
    // If there is one application and it's empty, add all classes
    if (mapping.keySet().size() == 1) {
        Set<Type> classes = mapping.values().iterator().next();
        if (classes.isEmpty()) {
            classes.addAll(classList);
        }
    }
    // If there is no application, add all classes to the context root.
    if (mapping.isEmpty()) {
        mapping.put("/", classList);
    }
    return mapping;
}
Also used : HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationPath(javax.ws.rs.ApplicationPath) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Type(org.glassfish.hk2.classmodel.reflect.Type) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Objects(java.util.Objects) Application(javax.ws.rs.core.Application) HashSet(java.util.HashSet)

Example 4 with ClassModel

use of org.glassfish.hk2.classmodel.reflect.ClassModel in project Payara by payara.

the class AnnotationInfo method init.

private void init(ExtensibleType<? extends ExtensibleType> type) {
    // recurse first so that re-stated annotations "override"
    ExtensibleType<? extends ExtensibleType> supertype = type.getParent();
    if (supertype != null) {
        init(supertype);
    }
    for (InterfaceModel implementedInterface : type.getInterfaces()) {
        if (implementedInterface != null && implementedInterface != type) {
            init(implementedInterface);
        }
    }
    // collect annotations
    putAll(type.getAnnotations(), typeAnnotations);
    if (type instanceof ClassModel) {
        for (FieldModel field : ((ClassModel) type).getFields()) {
            putAll(field.getAnnotations(), fieldAnnotations.computeIfAbsent(field.getName(), key -> new ConcurrentHashMap<>()));
        }
    }
    for (MethodModel method : type.getMethods()) {
        putAll(method.getAnnotations(), methodAnnotations.computeIfAbsent(getSignature(method), key -> new ConcurrentHashMap<>()));
        for (Parameter parameter : method.getParameters()) {
            putAll(parameter.getAnnotations(), methodParameterAnnotations.computeIfAbsent(getIdentifier(parameter), key -> new ConcurrentHashMap<>()));
        }
    }
}
Also used : InterfaceModel(org.glassfish.hk2.classmodel.reflect.InterfaceModel) Collection(java.util.Collection) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) Parameter(org.glassfish.hk2.classmodel.reflect.Parameter) Field(java.lang.reflect.Field) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) Method(java.lang.reflect.Method) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) InterfaceModel(org.glassfish.hk2.classmodel.reflect.InterfaceModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Parameter(org.glassfish.hk2.classmodel.reflect.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with ClassModel

use of org.glassfish.hk2.classmodel.reflect.ClassModel in project Payara by payara.

the class OpenApiWalker method processAnnotation.

@SuppressWarnings("unchecked")
public final void processAnnotation(ClassModel annotatedClass, ApiVisitor visitor) {
    AnnotationInfo annotations = context.getAnnotationInfo(annotatedClass);
    processAnnotation((E) annotatedClass, annotations, visitor, new OpenApiContext(context, annotatedClass));
    for (final MethodModel method : annotatedClass.getMethods()) {
        processAnnotation((E) method, annotations, visitor, new OpenApiContext(context, method));
    }
    for (final FieldModel field : annotatedClass.getFields()) {
        processAnnotation((E) field, annotations, visitor, new OpenApiContext(context, field));
    }
    for (final MethodModel method : annotatedClass.getMethods()) {
        for (org.glassfish.hk2.classmodel.reflect.Parameter parameter : method.getParameters()) {
            processAnnotation((E) parameter, annotations, visitor, new OpenApiContext(context, method));
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)5 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)4 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)3 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)3 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)2 ParameterizedInterfaceModel (org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1