Search in sources :

Example 46 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class ContextTest method resolveTypeUsageOfFirstMethodInGenericClass.

@Test
public void resolveTypeUsageOfFirstMethodInGenericClass() 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 callToGetTypes = Navigator.findMethodCall(method, "getTypes").get();
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    MethodUsage filterUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToGetTypes);
    assertEquals("java.util.List<com.github.javaparser.ast.body.TypeDeclaration>", filterUsage.returnType().describe());
    assertEquals(1, filterUsage.returnType().asReferenceType().typeParametersValues().size());
    assertEquals("com.github.javaparser.ast.body.TypeDeclaration", filterUsage.returnType().asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 47 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class ContextTest method resolveReferenceToOverloadMethodFindStricter.

@Test
public void resolveReferenceToOverloadMethodFindStricter() {
    CompilationUnit cu = parseSample("OverloadedMethods");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
    MethodDeclaration method = Navigator.demandMethod(clazz, "m2");
    MethodCallExpr call = Navigator.findMethodCall(method, "overloaded").get();
    ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
    MethodUsage ref = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    assertEquals("overloaded", ref.getName());
    assertEquals(1, ref.getNoParams());
    assertEquals("java.lang.String", ref.getParamTypes().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 48 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class ContextTest method resolveParameterReference.

@Test
public void resolveParameterReference() {
    CompilationUnit cu = parseSample("ReferencesToParameter");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferenceToParameter");
    MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "aMethod");
    NameExpr foo = Navigator.findNameExpression(method1, "foo").get();
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference symbolReference = symbolSolver.solveSymbol("foo", foo);
    assertEquals(true, symbolReference.isSolved());
    assertEquals("foo", symbolReference.getCorrespondingDeclaration().getName());
    assertEquals(true, symbolReference.getCorrespondingDeclaration().isParameter());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) NameExpr(com.github.javaparser.ast.expr.NameExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 49 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class ContextTest method resolveDeclaredFieldReference.

@Test
public void resolveDeclaredFieldReference() {
    CompilationUnit cu = parseSample("ReferencesToField");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferencesToField");
    MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "method1");
    ExpressionStmt stmt = (ExpressionStmt) method1.getBody().get().getStatements().get(0);
    AssignExpr assignExpr = (AssignExpr) stmt.getExpression();
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference symbolReference = symbolSolver.solveSymbol("i", assignExpr.getTarget());
    assertEquals(true, symbolReference.isSolved());
    assertEquals("i", symbolReference.getCorrespondingDeclaration().getName());
    assertEquals(true, symbolReference.getCorrespondingDeclaration().isField());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 50 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration 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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)325 Test (org.junit.Test)166 CompilationUnit (com.github.javaparser.ast.CompilationUnit)143 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)107 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)100 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)84 Expression (com.github.javaparser.ast.expr.Expression)82 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)77 NameExpr (com.github.javaparser.ast.expr.NameExpr)58 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)57 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)47 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)46 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)41 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)40 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)38 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)35 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)35 NodeList (com.github.javaparser.ast.NodeList)32 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)27