Search in sources :

Example 31 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.

the class CloneVisitor method visit.

@Override
public Node visit(TypeDeclarationStmt _n, Object _arg) {
    TypeDeclaration typeDecl = cloneNodes(_n.getTypeDeclaration(), _arg);
    Comment comment = cloneNodes(_n.getComment(), _arg);
    TypeDeclarationStmt r = new TypeDeclarationStmt(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), typeDecl);
    r.setComment(comment);
    return r;
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) BlockComment(com.github.javaparser.ast.comments.BlockComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 32 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.

the class CloneVisitor method visit.

@Override
public Node visit(CompilationUnit _n, Object _arg) {
    PackageDeclaration package_ = cloneNodes(_n.getPackage(), _arg);
    List<ImportDeclaration> imports = visit(_n.getImports(), _arg);
    List<TypeDeclaration> types = visit(_n.getTypes(), _arg);
    return new CompilationUnit(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), package_, imports, types);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 33 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.

the class DumpVisitor method visit.

@Override
public void visit(final CompilationUnit n, final Object arg) {
    printJavaComment(n.getComment(), arg);
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (final ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
        printer.printLn();
    }
    if (n.getTypes() != null) {
        for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext(); ) {
            i.next().accept(this, arg);
            printer.printLn();
            if (i.hasNext()) {
                printer.printLn();
            }
        }
    }
    printOrphanCommentsEnding(n);
}
Also used : ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 34 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.

the class Issue2602Test method setUp.

@BeforeEach
public void setUp() {
    typeSolver = new MemoryTypeSolver();
    configuration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
    javaParser = new JavaParser(configuration);
    // language=JAVA
    String src = "package java.lang;" + " class Object {}\n" + "\n" + "class A extends Object {}\n" + "\n" + "class B extends Object {}\n";
    ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(src));
    System.out.println("parseResult = " + parseResult);
    parseResult.getProblems().forEach(problem -> System.out.println("problem.getVerboseMessage() = " + problem.getVerboseMessage()));
    assertTrue(parseResult.isSuccessful());
    assertEquals(0, parseResult.getProblems().size(), "Expected zero errors when attempting to parse the input code.");
    assertTrue(parseResult.getResult().isPresent(), "Must have a parse result to run this test.");
    this.cu = parseResult.getResult().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(this.typeSolver);
    for (TypeDeclaration t : this.cu.getTypes()) {
        JavaParserClassDeclaration classDecl = new JavaParserClassDeclaration((ClassOrInterfaceDeclaration) t, this.typeSolver);
        this.typeSolver.addDeclaration((String) t.getFullyQualifiedName().get(), classDecl);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) JavaParser(com.github.javaparser.JavaParser) JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ParserConfiguration(com.github.javaparser.ParserConfiguration) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 35 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.

the class AnonymousClassDeclarationContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
    List<TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(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)));
    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)
        Optional<ResolvedReferenceTypeDeclaration> optionalTypeDeclaration = ancestor.getTypeDeclaration();
        if (optionalTypeDeclaration.isPresent()) {
            ResolvedReferenceTypeDeclaration typeDeclaration = optionalTypeDeclaration.get();
            if (typeDeclaration.getName().equals(name)) {
                return SymbolReference.solved(typeDeclaration);
            }
            // look into internal types of extended classes and implemented interfaces
            try {
                for (ResolvedTypeDeclaration internalTypeDeclaration : typeDeclaration.internalTypes()) {
                    if (internalTypeDeclaration.getName().equals(name)) {
                        return SymbolReference.solved(internalTypeDeclaration);
                    }
                }
            } catch (UnsupportedOperationException e) {
            // just continue using the next ancestor
            }
        }
    }
    return solveTypeInParentContext(name);
}
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) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) 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) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) 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) Optional(java.util.Optional) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) Preconditions(com.google.common.base.Preconditions) TypeParameter(com.github.javaparser.ast.type.TypeParameter) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) 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) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)44 CompilationUnit (com.github.javaparser.ast.CompilationUnit)22 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)13 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)8 List (java.util.List)8 Optional (java.util.Optional)8 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)6 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)6 Collectors (java.util.stream.Collectors)6 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)5 NodeList (com.github.javaparser.ast.NodeList)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)5 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)5 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)5