Search in sources :

Example 6 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration 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 7 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration 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 8 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project javaparser by javaparser.

the class JavaParser method parseImport.

/**
 * Parses the Java import contained in a {@link String} and returns a
 * {@link ImportDeclaration} that represents it.
 *
 * @param importDeclaration
 *            {@link String} containing Java import code
 * @return ImportDeclaration representing the Java import declaration
 * @throws ParseException
 *             if the source code has parser errors
 */
public static ImportDeclaration parseImport(final String importDeclaration) {
    StringReader sr = new StringReader(importDeclaration);
    ImportDeclaration id = new ASTParser(sr).ImportDeclaration();
    sr.close();
    return id;
}
Also used : StringReader(java.io.StringReader) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ASTParser(com.github.javaparser.ASTParser)

Example 9 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project javaparser by javaparser.

the class CompilationUnitContext method solveSymbol.

// /
// / Public methods
// /
@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    // solve absolute references
    String itName = name;
    while (itName.contains(".")) {
        String typeName = getType(itName);
        String memberName = getMember(itName);
        SymbolReference<ResolvedTypeDeclaration> type = this.solveType(typeName, typeSolver);
        if (type.isSolved()) {
            return new SymbolSolver(typeSolver).solveSymbolInType(type.getCorrespondingDeclaration(), memberName);
        } else {
            itName = typeName;
        }
    }
    // Look among statically imported values
    if (wrappedNode.getImports() != null) {
        for (ImportDeclaration importDecl : wrappedNode.getImports()) {
            if (importDecl.isStatic()) {
                if (importDecl.isAsterisk()) {
                    String qName = importDecl.getNameAsString();
                    ResolvedTypeDeclaration importedType = typeSolver.solveType(qName);
                    SymbolReference<? extends ResolvedValueDeclaration> ref = new SymbolSolver(typeSolver).solveSymbolInType(importedType, name);
                    if (ref.isSolved()) {
                        return ref;
                    }
                } else {
                    String whole = importDecl.getNameAsString();
                    // split in field/method name and type name
                    String memberName = getMember(whole);
                    String typeName = getType(whole);
                    if (memberName.equals(name)) {
                        ResolvedTypeDeclaration importedType = typeSolver.solveType(typeName);
                        return new SymbolSolver(typeSolver).solveSymbolInType(importedType, memberName);
                    }
                }
            }
        }
    }
    return SymbolReference.unsolved(ResolvedValueDeclaration.class);
}
Also used : SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Example 10 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project javaparser by javaparser.

the class CompilationUnitContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    if (wrappedNode.getTypes() != null) {
        for (TypeDeclaration<?> type : wrappedNode.getTypes()) {
            if (type.getName().getId().equals(name)) {
                if (type instanceof ClassOrInterfaceDeclaration) {
                    return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration((ClassOrInterfaceDeclaration) type));
                } else if (type instanceof AnnotationDeclaration) {
                    return SymbolReference.solved(new JavaParserAnnotationDeclaration((AnnotationDeclaration) type, typeSolver));
                } else if (type instanceof EnumDeclaration) {
                    return SymbolReference.solved(new JavaParserEnumDeclaration((EnumDeclaration) type, typeSolver));
                } else {
                    throw new UnsupportedOperationException(type.getClass().getCanonicalName());
                }
            }
        }
    }
    // Look in current package
    if (this.wrappedNode.getPackageDeclaration().isPresent()) {
        String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name;
        SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
        if (ref != null && ref.isSolved()) {
            return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
        }
    } else {
        // look for classes in the default package
        String qName = name;
        SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
        if (ref != null && ref.isSolved()) {
            return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
        }
    }
    if (wrappedNode.getImports() != null) {
        int dotPos = name.indexOf('.');
        String prefix = null;
        if (dotPos > -1) {
            prefix = name.substring(0, dotPos);
        }
        // look into type imports
        for (ImportDeclaration importDecl : wrappedNode.getImports()) {
            if (!importDecl.isAsterisk()) {
                String qName = importDecl.getNameAsString();
                boolean defaultPackage = !importDecl.getName().getQualifier().isPresent();
                boolean found = !defaultPackage && importDecl.getName().getIdentifier().equals(name);
                if (!found) {
                    if (prefix != null) {
                        found = qName.endsWith("." + prefix);
                        if (found) {
                            qName = qName + name.substring(dotPos);
                        }
                    }
                }
                if (found) {
                    SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
                    if (ref.isSolved()) {
                        return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
                    }
                }
            }
        }
        // look into type imports on demand
        for (ImportDeclaration importDecl : wrappedNode.getImports()) {
            if (importDecl.isAsterisk()) {
                String qName = importDecl.getNameAsString() + "." + name;
                SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
                if (ref.isSolved()) {
                    return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
                }
            }
        }
    }
    // Look in the java.lang package
    SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType("java.lang." + name);
    if (ref.isSolved()) {
        return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
    }
    // DO NOT look for absolute name if this name is not qualified: you cannot import classes from the default package
    if (isQualifiedName(name)) {
        return SymbolReference.adapt(typeSolver.tryToSolveType(name), ResolvedTypeDeclaration.class);
    } else {
        return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
    }
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration)

Aggregations

ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)16 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)5 Test (org.junit.Test)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)3 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)2 EmptyTypeDeclaration (com.github.javaparser.ast.body.EmptyTypeDeclaration)2 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)2 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)2 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 JavaParserAnnotationDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration)2 JavaParserEnumDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration)2 SymbolSolver (com.github.javaparser.symbolsolver.resolution.SymbolSolver)2 ASTParser (com.github.javaparser.ASTParser)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1