use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToCallOnLambdaParam.
@Test
public void resolveReferenceToCallOnLambdaParam() 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();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToGetName);
assertEquals("getName", methodUsage.getName());
assertEquals("com.github.javaparser.ast.body.TypeDeclaration", methodUsage.declaringType().getQualifiedName());
}
use of com.github.javaparser.ast.body.MethodDeclaration 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());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class GenericsResolutionTest method methodWithGenericParameterTypes.
@Test
public void methodWithGenericParameterTypes() {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
MethodCallExpr methodCall = Navigator.findMethodCall(method, "foo").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodUsage methodUsage = javaParserFacade.solveMethodAsUsage(methodCall);
assertEquals("foo", methodUsage.getName());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class GenericsResolutionTest method typeParamOnReturnType.
@Test
public void typeParamOnReturnType() {
CompilationUnit cu = parseSample("TypeParamOnReturnType");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType");
MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(returnStmt.getExpression().get());
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class GenericsResolutionTest method genericsInheritance.
@Test
public void genericsInheritance() {
CompilationUnit cu = parseSample("MethodTypeParams");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VoidVisitorAdapter");
MethodDeclaration method = Navigator.demandMethod(clazz, "visit");
MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
Expression thisRef = call.getArguments().get(0);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType voidVisitorAdapterOfA = javaParserFacade.getType(thisRef);
List<ResolvedReferenceType> allAncestors = voidVisitorAdapterOfA.asReferenceType().getAllAncestors();
assertEquals(2, allAncestors.size());
}
Aggregations