Search in sources :

Example 6 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class VoidVisitorAdapter method visit.

@Override
public void visit(final ClassOrInterfaceDeclaration n, final A arg) {
    visitComment(n.getComment(), arg);
    visitAnnotations(n, arg);
    n.getNameExpr().accept(this, arg);
    for (final TypeParameter t : n.getTypeParameters()) {
        t.accept(this, arg);
    }
    for (final ClassOrInterfaceType c : n.getExtends()) {
        c.accept(this, arg);
    }
    for (final ClassOrInterfaceType c : n.getImplements()) {
        c.accept(this, arg);
    }
    for (final BodyDeclaration<?> member : n.getMembers()) {
        member.accept(this, arg);
    }
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter)

Example 7 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class AnonymousClassDeclarationContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    List<com.github.javaparser.ast.body.TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(com.github.javaparser.ast.body.TypeDeclaration.class);
    Optional<SymbolReference<ResolvedTypeDeclaration>> exactMatch = typeDeclarations.stream().filter(internalType -> internalType.getName().getId().equals(name)).findFirst().map(internalType -> SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)));
    if (exactMatch.isPresent()) {
        return exactMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> recursiveMatch = typeDeclarations.stream().filter(internalType -> name.startsWith(String.format("%s.", internalType.getName()))).findFirst().map(internalType -> JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1), typeSolver));
    if (recursiveMatch.isPresent()) {
        return recursiveMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> typeArgumentsMatch = wrappedNode.getTypeArguments().map(nodes -> ((NodeWithTypeArguments<?>) nodes).getTypeArguments().orElse(new NodeList<>())).orElse(new NodeList<>()).stream().filter(type -> type.toString().equals(name)).findFirst().map(matchingType -> SymbolReference.solved(new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
    if (typeArgumentsMatch.isPresent()) {
        return typeArgumentsMatch.get();
    }
    // Look into extended classes and implemented interfaces
    for (ResolvedReferenceType ancestor : myDeclaration.getAncestors()) {
        // look at names of extended classes and implemented interfaces (this may not be important because they are checked in CompilationUnitContext)
        if (ancestor.getTypeDeclaration().getName().equals(name)) {
            return SymbolReference.solved(ancestor.getTypeDeclaration());
        }
        // look into internal types of extended classes and implemented interfaces
        try {
            for (ResolvedTypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
                if (internalTypeDeclaration.getName().equals(name)) {
                    return SymbolReference.solved(internalTypeDeclaration);
                }
            }
        } catch (UnsupportedOperationException e) {
        // just continue using the next ancestor
        }
    }
    return getParent().solveType(name, typeSolver);
}
Also used : JavaParserFactory(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory) NodeList(com.github.javaparser.ast.NodeList) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeParameter(com.github.javaparser.ast.type.TypeParameter) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) NodeWithTypeArguments(com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) JavaParserAnonymousClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnonymousClassDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) TypeParameter(com.github.javaparser.ast.type.TypeParameter) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Example 8 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterAsLastWhenThereAreSome.

@Test
public void addingTypeParameterAsLastWhenThereAreSome() throws IOException {
    ClassOrInterfaceDeclaration cid = consider("class A<U> {}");
    cid.addTypeParameter(new TypeParameter("T", new NodeList<>()));
    assertTransformedToString("class A<U, T> {}", cid);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 9 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterWhenThereAreNone.

// typeParameters
@Test
public void addingTypeParameterWhenThereAreNone() throws IOException {
    ClassOrInterfaceDeclaration cid = consider("class A {}");
    cid.addTypeParameter(new TypeParameter("T", new NodeList<>()));
    assertTransformedToString("class A<T> {}", cid);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Aggregations

TypeParameter (com.github.javaparser.ast.type.TypeParameter)9 NodeList (com.github.javaparser.ast.NodeList)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 AbstractLexicalPreservingTest (com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)3 Test (org.junit.Test)3 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)2 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)2 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 NodeWithTypeArguments (com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments)1 NodeWithTypeParameters (com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ArrayType (com.github.javaparser.ast.type.ArrayType)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 ReferenceType (com.github.javaparser.ast.type.ReferenceType)1 Type (com.github.javaparser.ast.type.Type)1