use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionClassDeclarationTest method testAllAncestors.
// /
// / Test ancestors
// /
@Test
public void testAllAncestors() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedClassDeclaration arraylist = new ReflectionClassDeclaration(ArrayList.class, typeResolver);
Map<String, ResolvedReferenceType> ancestors = new HashMap<>();
arraylist.getAllAncestors().forEach(a -> ancestors.put(a.getQualifiedName(), a));
assertEquals(9, ancestors.size());
ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(arraylist.getTypeParameters().get(0));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(RandomAccess.class, typeResolver), typeResolver), ancestors.get("java.util.RandomAccess"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(AbstractCollection.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.util.AbstractCollection"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.util.List"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Cloneable.class, typeResolver), typeResolver), ancestors.get("java.lang.Cloneable"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Collection.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.util.Collection"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(AbstractList.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.util.AbstractList"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeResolver), typeResolver), ancestors.get("java.lang.Object"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Iterable.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.lang.Iterable"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Serializable.class, typeResolver), typeResolver), ancestors.get("java.io.Serializable"));
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionClassDeclarationTest method testGetAncestorsWithTypeParameters.
@Test
public void testGetAncestorsWithTypeParameters() {
ReflectionClassDeclaration constructorDeclaration = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
assertEquals(9, constructorDeclaration.getAncestors().size());
ResolvedReferenceType ancestor;
List<ResolvedReferenceType> ancestors = constructorDeclaration.getAncestors();
ancestors.sort(Comparator.comparing(ResolvedReferenceType::getQualifiedName));
ancestor = ancestors.get(0);
assertEquals("com.github.javaparser.ast.body.CallableDeclaration", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.body.CallableDeclaration.T").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(1);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(2);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithJavadoc", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithJavadoc.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(3);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithParameters", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithParameters.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(4);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithSimpleName", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithSimpleName.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(5);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithThrownExceptions", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithThrownExceptions.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(6);
assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(7);
assertEquals("com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers", ancestor.getQualifiedName());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", ancestor.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers.N").get().asReferenceType().getQualifiedName());
ancestor = ancestors.get(8);
assertEquals("com.github.javaparser.resolution.Resolvable", ancestor.getQualifiedName());
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionInterfaceDeclarationTest method testAllAncestors.
@Test
public void testAllAncestors() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);
Map<String, ResolvedReferenceType> ancestors = new HashMap<>();
list.getAllAncestors().forEach(a -> ancestors.put(a.getQualifiedName(), a));
assertEquals(3, ancestors.size());
ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(list.getTypeParameters().get(0));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Collection.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.util.Collection"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeResolver), typeResolver), ancestors.get("java.lang.Object"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Iterable.class, typeResolver), ImmutableList.of(typeVariable), typeResolver), ancestors.get("java.lang.Iterable"));
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project flow by vaadin.
the class OpenAPIObjectGenerator method parseReferencedTypeAsSchema.
@SuppressWarnings("squid:S1872")
private List<Schema> parseReferencedTypeAsSchema(GeneratorType type) {
List<Schema> results = new ArrayList<>();
Schema schema = schemaGenerator.createSingleSchemaFromResolvedType(type);
ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
String qualifiedName = resolvedReferenceType.getQualifiedName();
generatedSchema.add(qualifiedName);
List<ResolvedReferenceType> directAncestors = resolvedReferenceType.getDirectAncestors().stream().filter(parent -> parent.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).isClass() && !Object.class.getName().equals(parent.getQualifiedName())).collect(Collectors.toList());
if (directAncestors.isEmpty() || type.isEnum()) {
results.add(schema);
results.addAll(generatedRelatedSchemas(schema));
} else {
ComposedSchema parentSchema = new ComposedSchema();
parentSchema.name(qualifiedName);
results.add(parentSchema);
for (ResolvedReferenceType directAncestor : directAncestors) {
String ancestorQualifiedName = directAncestor.getQualifiedName();
String parentRef = SchemaResolver.getFullQualifiedNameRef(ancestorQualifiedName);
parentSchema.addAllOfItem(new ObjectSchema().$ref(parentRef));
usedTypes.put(ancestorQualifiedName, new GeneratorType(directAncestor));
}
parentSchema.addAllOfItem(schema);
results.addAll(generatedRelatedSchemas(parentSchema));
}
return results;
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project flow by vaadin.
the class SchemaGenerator method createSingleSchemaFromResolvedType.
Schema createSingleSchemaFromResolvedType(GeneratorType type) {
ResolvedReferenceType resolvedReferenceType = type.asResolvedType().asReferenceType();
if (type.isEnum()) {
List<String> entries = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).asEnum().getEnumConstants().stream().map(ResolvedEnumConstantDeclaration::getName).collect(Collectors.toList());
StringSchema schema = new StringSchema();
schema.name(resolvedReferenceType.getQualifiedName());
schema.setEnum(entries);
return schema;
}
Schema schema = new ObjectSchema().name(resolvedReferenceType.getQualifiedName());
Map<String, Boolean> fieldsOptionalMap = getFieldsAndOptionalMap(type);
List<ResolvedFieldDeclaration> serializableFields = resolvedReferenceType.getTypeDeclaration().orElseThrow(IllegalArgumentException::new).getDeclaredFields().stream().filter(resolvedFieldDeclaration -> fieldsOptionalMap.containsKey(resolvedFieldDeclaration.getName())).collect(Collectors.toList());
// Make sure the order is consistent in properties map
schema.setProperties(new LinkedHashMap<>());
for (ResolvedFieldDeclaration resolvedFieldDeclaration : serializableFields) {
String name = resolvedFieldDeclaration.getName();
ResolvedType fieldType = resolvedFieldDeclaration.getType();
ResolvedType mappedType = openApiObjectGenerator.toMappedType(fieldType);
if (mappedType != null) {
fieldType = mappedType;
}
Schema subtype = openApiObjectGenerator.parseResolvedTypeToSchema(new GeneratorType(fieldType)).nullable(null);
if (!fieldsOptionalMap.get(name)) {
schema.addRequiredItem(name);
}
schema.addProperties(name, subtype);
}
return schema;
}
Aggregations