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);
}
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;
}
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;
}
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<>()));
}
}
}
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));
}
}
}
Aggregations