Search in sources :

Example 21 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value 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)

Example 22 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolAsValueReferringToValue.

@Test
public void solveSymbolAsValueReferringToValue() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("E1", new MemoryTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("MyEnum", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 23 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolAsValueReferringToDeclaredStaticField.

@Test
public void solveSymbolAsValueReferringToDeclaredStaticField() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("j", new MemoryTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("long", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 24 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class GenericsResolutionTest method resolveFieldWithGenericTypeToInteger.

@Test
public void resolveFieldWithGenericTypeToInteger() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "i");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("i", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("i", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("java.lang.Integer", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 25 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class GenericsResolutionTest method resolveFieldWithGenericTypeToDeclaredClass.

@Test
public void resolveFieldWithGenericTypeToDeclaredClass() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "g");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("g", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("g", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("me.tomassetti.symbolsolver.javaparser.Generics", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Aggregations

Value (com.github.javaparser.symbolsolver.model.resolution.Value)26 CompilationUnit (com.github.javaparser.ast.CompilationUnit)21 Test (org.junit.Test)20 Context (com.github.javaparser.symbolsolver.core.resolution.Context)16 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)15 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)11 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)9 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)8 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)7 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)4 SymbolDeclarator (com.github.javaparser.symbolsolver.resolution.SymbolDeclarator)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 Parameter (com.github.javaparser.ast.body.Parameter)3 EnumDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2