use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class FieldsResolutionTest method accessFieldThroughSuper.
@Test
public void accessFieldThroughSuper() {
CompilationUnit cu = parseSample("AccessThroughSuper");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessThroughSuper.SubClass");
MethodDeclaration method = Navigator.demandMethod(clazz, "fieldTest");
ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0);
Expression expression = returnStmt.getExpression().get();
ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
assertEquals("java.lang.String", ref.describe());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class JavaParserFacadeResolutionTest method solveTryWithResourceVariable.
// See issue 119
@Test
public void solveTryWithResourceVariable() {
String code = "import java.util.Scanner; class A { void foo() { try (Scanner sc = new Scanner(System.in)) {\n" + " sc.nextLine();\n" + "} } }";
CompilationUnit cu = JavaParser.parse(code);
MethodCallExpr methodCallExpr = Navigator.findMethodCall(cu, "nextLine").get();
Expression scope = methodCallExpr.getScope().get();
ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(scope);
assertEquals(true, type.isReferenceType());
assertEquals("java.util.Scanner", type.asReferenceType().getQualifiedName());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class JavaParserFacadeResolutionTest method solveMultiCatchType.
@Test
public void solveMultiCatchType() {
String code = "class A {\n" + " public void foo() {\n" + " try {\n" + " \n" + " } catch (IllegalStateException | IllegalArgumentException e) {\n" + " \n" + " }\n" + " }\n" + " }";
CompilationUnit cu = parseWithTypeSolver(code);
CatchClause catchClause = Navigator.findNodeOfGivenClass(cu, CatchClause.class);
Type jpType = catchClause.getParameter().getType();
ResolvedType jssType = jpType.resolve();
assertEquals(true, jssType instanceof ResolvedUnionType);
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class JavaParserFacadeResolutionTest method classToResolvedTypeViaReflection.
@Test
public void classToResolvedTypeViaReflection() {
Class<?> clazz = this.getClass();
ReflectionTypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
JavaParserFacade facade = JavaParserFacade.get(reflectionTypeSolver);
ResolvedType resolvedType = facade.classToResolvedType(clazz);
assertNotNull(resolvedType);
assertTrue(resolvedType.isReferenceType());
assertEquals(clazz.getCanonicalName(), resolvedType.asReferenceType().getQualifiedName());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class LambdaResolutionTest method lambdaReduce.
@Test
public void lambdaReduce() {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration method = Navigator.demandMethod(clazz, "reduce");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
Expression expr = returnStmt.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
ResolvedType type1 = javaParserFacade.getType(expr);
assertEquals("java.util.Optional<java.lang.Integer>", type1.describe());
}
Aggregations