Search in sources :

Example 11 with CompilationUnitContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext 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)

Example 12 with CompilationUnitContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveExistingGenericType.

@Test
public void solveExistingGenericType() {
    CompilationUnit cu = parseSample("ClassWithTypeVariables");
    Context context = new CompilationUnitContext(cu, typeSolver);
    Optional<ResolvedType> a = context.solveGenericType("A", new MemoryTypeSolver());
    Optional<ResolvedType> b = context.solveGenericType("B", new MemoryTypeSolver());
    Optional<ResolvedType> c = context.solveGenericType("C", new MemoryTypeSolver());
    assertEquals(false, a.isPresent());
    assertEquals(false, b.isPresent());
    assertEquals(false, c.isPresent());
}
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) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 13 with CompilationUnitContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveMethodStaticallyImportedWithoutAsterisk.

@Test
public void solveMethodStaticallyImportedWithoutAsterisk() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitSymbols");
    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("assertEquals", ImmutableList.of(NullType.INSTANCE, NullType.INSTANCE), false, typeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("assertEquals", ref.getCorrespondingDeclaration().getName());
    assertEquals(2, ref.getCorrespondingDeclaration().getNumberOfParams());
    assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(0).getType().asReferenceType().getQualifiedName());
    assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(1).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) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 14 with CompilationUnitContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveSymbolReferringToStaticallyImportedUsingAsteriskValue.

@Test
public void solveSymbolReferringToStaticallyImportedUsingAsteriskValue() 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("err", 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 15 with CompilationUnitContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveSymbolAsValueReferringToStaticallyImportedValue.

@Test
public void solveSymbolAsValueReferringToStaticallyImportedValue() 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("out", 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)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)15 Context (com.github.javaparser.symbolsolver.core.resolution.Context)15 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)15 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)15 Test (org.junit.Test)15 JarTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver)8 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)8 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)6 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)3 Value (com.github.javaparser.symbolsolver.model.resolution.Value)3 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)3 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)1