use of org.glassfish.hk2.classmodel.reflect.ExtensibleType 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.ExtensibleType 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.ExtensibleType in project Payara by payara.
the class ApplicationProcessor method findParameterizedModelFromGenerics.
private ParameterizedInterfaceModel findParameterizedModelFromGenerics(ExtensibleType<? extends ExtensibleType> annotatedElement, Collection<ParameterizedInterfaceModel> parameterizedModels, ParameterizedType genericType) {
if (parameterizedModels == null || parameterizedModels.isEmpty()) {
return null;
}
List<String> formalParamKeys = new ArrayList<>(annotatedElement.getFormalTypeParameters().keySet());
int i = 0;
for (ParameterizedInterfaceModel parameterizedModel : parameterizedModels) {
if (formalParamKeys.get(i).equals(genericType.getFormalType())) {
return parameterizedModel;
}
i++;
}
return null;
}
use of org.glassfish.hk2.classmodel.reflect.ExtensibleType in project Payara by payara.
the class ModelUtils method getSchemaName.
@SuppressWarnings("unchecked")
public static String getSchemaName(ApiContext context, AnnotatedElement type) {
assert type != null;
// context and annotation can be null
final Class<? extends Annotation>[] ANNOTATION_TYPES = new Class[] { org.eclipse.microprofile.openapi.annotations.media.Schema.class, javax.xml.bind.annotation.XmlRootElement.class, javax.xml.bind.annotation.XmlElement.class };
for (Class<? extends Annotation> annotationType : ANNOTATION_TYPES) {
AnnotationModel annotationModel;
// Fetch the element annotations
if (context != null && type instanceof ExtensibleType) {
// Fetch the annotation from the cache
ExtensibleType<?> implementationType = (ExtensibleType<?>) type;
AnnotationInfo annotationInfo = context.getAnnotationInfo(implementationType);
annotationModel = annotationInfo.getAnnotation(annotationType);
} else {
// Fetch the annotation manually
annotationModel = type.getAnnotation(annotationType.getName());
}
// Fields can be named by their accessors
if (annotationModel == null) {
if (type instanceof FieldModel) {
final FieldModel field = (FieldModel) type;
final String accessorName = getAccessorName(field.getName());
for (MethodModel method : field.getDeclaringType().getMethods()) {
// Check if it's the accessor
if (accessorName.equals(method.getName())) {
annotationModel = type.getAnnotation(annotationType.getName());
break;
}
}
}
}
// Get the schema name if the annotation exists
if (annotationModel != null) {
final String name = annotationModel.getValue("name", String.class);
if (name != null && !name.isEmpty() && !name.equals("##default")) {
return name;
}
}
}
return getSimpleName(type.getName());
}
use of org.glassfish.hk2.classmodel.reflect.ExtensibleType 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<>()));
}
}
}
Aggregations