Search in sources :

Example 56 with ResolvedReferenceType

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"));
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 57 with ResolvedReferenceType

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());
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 58 with ResolvedReferenceType

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"));
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) HashMap(java.util.HashMap) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 59 with ResolvedReferenceType

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;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Operation(io.swagger.v3.oas.models.Operation) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) Pair(com.github.javaparser.utils.Pair) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Scopes(io.swagger.v3.oas.models.security.Scopes) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Tag(io.swagger.v3.oas.models.tags.Tag) Path(java.nio.file.Path) JavaSymbolSolver(com.github.javaparser.symbolsolver.JavaSymbolSolver) NodeList(com.github.javaparser.ast.NodeList) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Content(io.swagger.v3.oas.models.media.Content) PermitAll(javax.annotation.security.PermitAll) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Collection(java.util.Collection) MediaType(io.swagger.v3.oas.models.media.MediaType) Set(java.util.Set) ParserConfiguration(com.github.javaparser.ParserConfiguration) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) EndpointTransferMapper(dev.hilla.endpointransfermapper.EndpointTransferMapper) Paths(io.swagger.v3.oas.models.Paths) Endpoint(dev.hilla.Endpoint) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) EndpointNameChecker(dev.hilla.EndpointNameChecker) Server(io.swagger.v3.oas.models.servers.Server) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) EndpointExposed(dev.hilla.EndpointExposed) ParseResult(com.github.javaparser.ParseResult) Callback(com.github.javaparser.utils.SourceRoot.Callback) Parameter(com.github.javaparser.ast.body.Parameter) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) HashMap(java.util.HashMap) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) TreeSet(java.util.TreeSet) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) ArrayList(java.util.ArrayList) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) JavadocBlockTag(com.github.javaparser.javadoc.JavadocBlockTag) Schema(io.swagger.v3.oas.models.media.Schema) DenyAll(javax.annotation.security.DenyAll) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SourceRoot(com.github.javaparser.utils.SourceRoot) Logger(org.slf4j.Logger) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) PathItem(io.swagger.v3.oas.models.PathItem) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Info(io.swagger.v3.oas.models.info.Info) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TreeMap(java.util.TreeMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Javadoc(com.github.javaparser.javadoc.Javadoc) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Components(io.swagger.v3.oas.models.Components) AnonymousAllowed(com.vaadin.flow.server.auth.AnonymousAllowed) ClassLoaderTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver) Comparator(java.util.Comparator) Collections(java.util.Collections) ResolvedTypeParametersMap(com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ArrayList(java.util.ArrayList) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema)

Example 60 with ResolvedReferenceType

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;
}
Also used : Arrays(java.util.Arrays) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) LinkedHashMap(java.util.LinkedHashMap) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Type(com.github.javaparser.ast.type.Type) ResolvedFieldDeclaration(com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Schema(io.swagger.v3.oas.models.media.Schema) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Set(java.util.Set) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) List(java.util.List) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedEnumConstantDeclaration(com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ExplicitNullableTypeChecker(dev.hilla.ExplicitNullableTypeChecker) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedFieldDeclaration(com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)64 Test (org.junit.Test)36 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)28 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)14 Collectors (java.util.stream.Collectors)11 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)10 MethodUsage (com.github.javaparser.resolution.MethodUsage)9 List (java.util.List)9 ArrayList (java.util.ArrayList)8 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)6 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)6 Pair (com.github.javaparser.utils.Pair)6 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)5 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 HashMap (java.util.HashMap)5 Optional (java.util.Optional)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4