use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class StatementContextResolutionTest method resolveLocalVariableInParent.
@Test
public void resolveLocalVariableInParent() {
CompilationUnit cu = parseSample("LocalVariableInParent");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo3");
NameExpr nameExpr = Navigator.findNameExpression(method, "s").get();
SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(nameExpr);
assertTrue(ref.isSolved());
assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class LambdaExprContextResolutionTest method solveParameterOfLambdaInVarDecl.
@Test
public void solveParameterOfLambdaInVarDecl() {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration method = Navigator.demandMethod(clazz, "testFunctionalVar");
VariableDeclarator varDecl = Navigator.demandVariableDeclaration(method, "a").get();
LambdaExpr lambdaExpr = (LambdaExpr) varDecl.getInitializer().get();
File src = adaptPath(new File("src/test/resources"));
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(src));
Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
assertTrue(ref.isPresent());
assertEquals("java.lang.String", ref.get().getType().describe());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.
@Test
public void declaredOnMethodPositiveCase() {
CompilationUnit cu = parseSample("MethodTypeParameter");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
assertTrue(tp instanceof JavaParserTypeParameter);
assertEquals("C", tp.getName());
assertEquals(true, tp.declaredOnMethod());
assertEquals(false, tp.declaredOnType());
}
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class Widget method main.
public static void main(String[] args) throws IOException, ParseException {
File src = new File(JAVA_ROOT);
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver(true));
combinedTypeSolver.add(new JavaParserTypeSolver(src));
CompilationUnit compilationUnit = JavaParser.parse(new File(CLASS));
JavaParserFacade parserFacade = JavaParserFacade.get(combinedTypeSolver);
MethodDeclaration methodDeclaration = compilationUnit.getNodesByType(MethodDeclaration.class).stream().filter(node -> node.getName().equals("doSomething")).findAny().orElse(null);
methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve);
}
use of com.github.javaparser.ast.body.MethodDeclaration 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());
}
Aggregations