Search in sources :

Example 11 with ResolvedTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration 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 12 with ResolvedTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration in project javaparser by javaparser.

the class Issue232 method issue232.

@Test
public void issue232() {
    CompilationUnit cu = parseSample("Issue232");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    Context context = JavaParserFactory.getContext(cls, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> reference = context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>", typeSolver);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 13 with ResolvedTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationContextResolutionTest method solveTypeRefToObject.

@Test
public void solveTypeRefToObject() {
    CompilationUnit cu = parseSample("ClassWithTypes");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("Object", new ReflectionTypeSolver());
    assertEquals(true, ref.isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 14 with ResolvedTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationContextResolutionTest method solveTypeRefToJavaLangObject.

@Test
public void solveTypeRefToJavaLangObject() {
    CompilationUnit cu = parseSample("ClassWithTypes");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("java.lang.Object", new ReflectionTypeSolver());
    assertEquals(true, ref.isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 15 with ResolvedTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationContextResolutionTest method solveTypeRefToAnotherClassInFile.

@Test
public void solveTypeRefToAnotherClassInFile() {
    CompilationUnit cu = parseSample("ClassWithTypes");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("Super", new MemoryTypeSolver());
    assertEquals(true, ref.isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)23 CompilationUnit (com.github.javaparser.ast.CompilationUnit)19 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)17 Context (com.github.javaparser.symbolsolver.core.resolution.Context)17 Test (org.junit.Test)17 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)16 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)14 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)11 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)11 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)5 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)5 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)4 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)4 SymbolSolver (com.github.javaparser.symbolsolver.resolution.SymbolSolver)4 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)3 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)3 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)3 List (java.util.List)3 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)2