Search in sources :

Example 61 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class VarTypeTest method resolveAReferenceType.

@Test
public void resolveAReferenceType() {
    CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc = \"\";}}")).getResult().get();
    VarType varType = ast.findFirst(VarType.class).get();
    ResolvedType resolvedType = varType.resolve();
    assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver), resolvedType);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) VarType(com.github.javaparser.ast.type.VarType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 62 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SymbolResolutionResolutionTest method conditionalExpressionExample.

@Test
public void conditionalExpressionExample() {
    CompilationUnit cu = parseSample("JreConditionalExpression");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 63 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SymbolResolutionResolutionTest method getTypeOfFieldAccess.

@Test
public void getTypeOfFieldAccess() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field.getInitializer().get());
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 64 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SymbolResolutionResolutionTest method getTypeOfField.

@Test
public void getTypeOfField() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field);
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 65 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SubtypeOfBoundTest method recognizeProperUpperBound2.

@Test
public void recognizeProperUpperBound2() {
    ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class);
    ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(ResolvedTypeParameterDeclaration.class);
    // { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them.
    InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1);
    InferenceVariable beta = new InferenceVariable("β", typeParameterDeclaration2);
    ResolvedType iterableOfWildcard = new ReferenceTypeImpl(iterableType.getTypeDeclaration(), Arrays.asList(ResolvedWildcard.UNBOUNDED), typeSolver);
    ResolvedType listOfBeta = new ReferenceTypeImpl(listType.getTypeDeclaration(), Arrays.asList(beta), typeSolver);
    Bound bound1 = new SubtypeOfBound(alpha, iterableOfWildcard);
    Bound bound2 = new SubtypeOfBound(beta, objectType);
    Bound bound3 = new SubtypeOfBound(alpha, listOfBeta);
    assertEquals(false, isProperType(listOfBeta));
    assertEquals(Optional.of(new ProperUpperBound(alpha, iterableOfWildcard)), bound1.isProperUpperBound());
    assertEquals(Optional.of(new ProperUpperBound(beta, objectType)), bound2.isProperUpperBound());
    assertEquals(true, bound3.isADependency());
}
Also used : ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

ResolvedType (com.github.javaparser.resolution.types.ResolvedType)119 Test (org.junit.Test)78 CompilationUnit (com.github.javaparser.ast.CompilationUnit)68 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)58 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)41 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)39 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)32 Expression (com.github.javaparser.ast.expr.Expression)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)22 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)21 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)20 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)18 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)18 MethodUsage (com.github.javaparser.resolution.MethodUsage)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)15 Context (com.github.javaparser.symbolsolver.core.resolution.Context)15 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)10 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)10 Collectors (java.util.stream.Collectors)10