use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class TreeVisitorTest method isValidBreadthFirstTraversal.
@Test
public void isValidBreadthFirstTraversal() {
Expression expression = JavaParser.parseExpression("(2+3)+(4+5)");
StringBuilder result = new StringBuilder();
TreeVisitor visitor = new TreeVisitor() {
@Override
public void process(Node node) {
result.append("<").append(node).append("> ");
}
};
visitor.visitBreadthFirst(expression);
assertEquals("<(2 + 3) + (4 + 5)> <(2 + 3)> <(4 + 5)> <2 + 3> <4 + 5> <2> <3> <4> <5> ", result.toString());
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class TreeVisitorTest method isValidDirectChildrenTraversal.
@Test
public void isValidDirectChildrenTraversal() {
Expression expression = JavaParser.parseExpression("(2+3)+(4+5)");
StringBuilder result = new StringBuilder();
TreeVisitor visitor = new TreeVisitor() {
@Override
public void process(Node node) {
result.append("<").append(node).append("> ");
}
};
visitor.visitDirectChildren(expression);
assertEquals("<(2 + 3)> <(4 + 5)> ", result.toString());
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class ContextTest method resolveReferenceToLambdaParam.
@Test
public void resolveReferenceToLambdaParam() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToGetName = Navigator.findMethodCall(method, "getName").get();
Expression referenceToT = callToGetName.getScope().get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
ResolvedType typeOfT = JavaParserFacade.get(typeSolver).getType(referenceToT);
assertEquals("? super com.github.javaparser.ast.body.TypeDeclaration", typeOfT.describe());
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class ContextTest method resolveLambdaType.
@Test
public void resolveLambdaType() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToFilter = Navigator.findMethodCall(method, "filter").get();
Expression lambdaExpr = callToFilter.getArguments().get(0);
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
ResolvedType typeOfLambdaExpr = JavaParserFacade.get(typeSolver).getType(lambdaExpr);
assertEquals("java.util.function.Predicate<? super com.github.javaparser.ast.body.TypeDeclaration>", typeOfLambdaExpr.describe());
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class EnumResolutionTest method switchOnEnum.
@Test
public void switchOnEnum() {
CompilationUnit cu = parseSample("SwitchOnEnum");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SwitchOnEnum");
MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
SwitchStmt switchStmt = Navigator.findSwitch(method);
Expression expression = switchStmt.getEntries().get(0).getLabel().get();
SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
assertTrue(ref.isSolved());
assertEquals("SwitchOnEnum.MyEnum", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
}
Aggregations