Search in sources :

Example 56 with Context

use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveSymbolReferringToStaticallyImportedValue.

@Test
public void solveSymbolReferringToStaticallyImportedValue() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitSymbols");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("out", typeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("java.io.PrintStream", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 57 with Context

use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveSymbolAsValueReferringToStaticallyImportedUsingAsteriskValue.

@Test
public void solveSymbolAsValueReferringToStaticallyImportedUsingAsteriskValue() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitSymbols");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    Optional<Value> ref = context.solveSymbolAsValue("err", typeSolver);
    assertEquals(true, ref.isPresent());
    assertEquals("java.io.PrintStream", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 58 with Context

use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveTypeInSamePackage.

@Test
public void solveTypeInSamePackage() {
    CompilationUnit cu = parseSample("CompilationUnitWithImports");
    Context context = new CompilationUnitContext(cu, typeSolver);
    ResolvedReferenceTypeDeclaration otherClass = mock(ResolvedReferenceTypeDeclaration.class);
    when(otherClass.getQualifiedName()).thenReturn("com.foo.OtherClassInSamePackage");
    MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
    memoryTypeSolver.addDeclaration("com.foo.OtherClassInSamePackage", otherClass);
    SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("OtherClassInSamePackage", memoryTypeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("com.foo.OtherClassInSamePackage", ref.getCorrespondingDeclaration().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 59 with Context

use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveTypeNotImported.

@Test
public void solveTypeNotImported() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitWithImports");
    Context context = new CompilationUnitContext(cu, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("org.junit.Assume", new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    assertEquals(true, ref.isSolved());
    assertEquals("org.junit.Assume", ref.getCorrespondingDeclaration().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 60 with Context

use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveMethodStaticallyImportedWithAsterisk.

@Test
public void solveMethodStaticallyImportedWithAsterisk() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitWithImports");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    typeSolver.add(new ReflectionTypeSolver());
    SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("assertFalse", ImmutableList.of(ResolvedPrimitiveType.BOOLEAN), false, typeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("assertFalse", ref.getCorrespondingDeclaration().getName());
    assertEquals(1, ref.getCorrespondingDeclaration().getNumberOfParams());
    assertEquals("boolean", ref.getCorrespondingDeclaration().getParam(0).getType().describe());
    assertEquals(true, ref.getCorrespondingDeclaration().getParam(0).getType().isPrimitive());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

Context (com.github.javaparser.symbolsolver.core.resolution.Context)72 CompilationUnit (com.github.javaparser.ast.CompilationUnit)65 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)63 Test (org.junit.Test)63 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)40 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)37 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)32 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)26 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)17 Value (com.github.javaparser.symbolsolver.model.resolution.Value)16 MethodUsage (com.github.javaparser.resolution.MethodUsage)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)11 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)10 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)9 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)7 JarTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver)7 EnumDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext)6 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)6 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)5