Search in sources :

Example 1 with Value

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

the class GenericsResolutionTest method resolveFieldWithGenericTypeToString.

@Test
public void resolveFieldWithGenericTypeToString() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "s");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("s", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("s", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("java.lang.String", 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 2 with Value

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

the class GenericsResolutionTest method resolveFieldOfGenericReferringToVariableType.

@Test
public void resolveFieldOfGenericReferringToVariableType() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    VariableDeclarator field = Navigator.demandField(clazz, "as");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("as", field);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("as", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.util.List<A>", type.describe());
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    ResolvedType typeParam = type.asReferenceType().typeParametersValues().get(0);
    assertEquals(true, typeParam.isTypeVariable());
    assertEquals("A", typeParam.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 3 with Value

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

the class GenericsResolutionTest method resolveFieldOfVariableType.

@Test
public void resolveFieldOfVariableType() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    VariableDeclarator field = Navigator.demandField(clazz, "a");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("a", field);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("a", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(true, type.isTypeVariable());
    assertEquals("A", type.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 4 with Value

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

the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolAsValueReferringToDeclaredInstanceField.

@Test
public void solveSymbolAsValueReferringToDeclaredInstanceField() {
    CompilationUnit cu = parseSample("ClassWithSymbols");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("i", new MemoryTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("int", ref.get().getType().describe());
}
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) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 5 with Value

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

the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolAsValueReferringToUnknownElement.

@Test
public void solveSymbolAsValueReferringToUnknownElement() {
    CompilationUnit cu = parseSample("ClassWithSymbols");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("zzz", new MemoryTypeSolver());
    assertEquals(false, ref.isPresent());
}
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) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) 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