Search in sources :

Example 1 with JavaParserInterfaceDeclaration

use of com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration in project javaparser by javaparser.

the class CompilationUnitContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
    if (wrappedNode.getTypes() != null) {
        // interface in this compilation unit called "A".
        for (TypeDeclaration<?> type : wrappedNode.getTypes()) {
            if (type.getName().getId().equals(name) || type.getFullyQualifiedName().map(qualified -> qualified.equals(name)).orElse(false)) {
                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());
                }
            }
        }
        // this method for the remaining part of the given name.
        if (name.indexOf('.') > -1) {
            SymbolReference<ResolvedTypeDeclaration> ref = null;
            SymbolReference<ResolvedTypeDeclaration> outerMostRef = solveType(name.substring(0, name.indexOf(".")));
            if (outerMostRef != null && outerMostRef.isSolved() && outerMostRef.getCorrespondingDeclaration() instanceof JavaParserClassDeclaration) {
                ref = ((JavaParserClassDeclaration) outerMostRef.getCorrespondingDeclaration()).solveType(name.substring(name.indexOf(".") + 1));
            } else if (outerMostRef != null && outerMostRef.isSolved() && outerMostRef.getCorrespondingDeclaration() instanceof JavaParserInterfaceDeclaration) {
                ref = ((JavaParserInterfaceDeclaration) outerMostRef.getCorrespondingDeclaration()).solveType(name.substring(name.indexOf(".") + 1));
            }
            if (ref != null && ref.isSolved()) {
                return ref;
            }
        }
    }
    // Inspect imports for matches, prior to inspecting other classes within the package (per issue #1526)
    int dotPos = name.indexOf('.');
    String prefix = null;
    if (dotPos > -1) {
        prefix = name.substring(0, dotPos);
    }
    // look into single 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 && prefix != null) {
                found = qName.endsWith("." + prefix);
                if (found) {
                    qName = qName + name.substring(dotPos);
                }
            }
            if (found) {
                SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
                if (ref != null && ref.isSolved()) {
                    return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
                }
            }
        }
    }
    // 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);
        }
    }
    // look into asterisk imports on demand
    for (ImportDeclaration importDecl : wrappedNode.getImports()) {
        if (importDecl.isAsterisk()) {
            String qName = importDecl.getNameAsString() + "." + name;
            SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
            if (ref != null && ref.isSolved()) {
                return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
            }
        }
    }
    // Look in the java.lang package
    SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType("java.lang." + name);
    if (ref != null && 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 : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) 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) JavaParserInterfaceDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration)

Aggregations

ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)1 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)1 JavaParserAnnotationDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration)1 JavaParserClassDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration)1 JavaParserEnumDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration)1 JavaParserInterfaceDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration)1