Search in sources :

Example 21 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue251 method testSolveStaticallyImportedMemberType.

@Test
public void testSolveStaticallyImportedMemberType() {
    CompilationUnit cu = parseSample("Issue251");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodDeclaration m = Navigator.demandMethod(cls, "bar");
    ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(1);
    MethodCallExpr expression = (MethodCallExpr) stmt.getExpression();
    Assert.assertNotNull(javaParserFacade.solve(expression));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 22 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue144 method issue144WithCombinedTypeSolver.

@Test
public void issue144WithCombinedTypeSolver() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    Expression firstParameter = methodCallExpr.getArgument(0);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new CombinedTypeSolver(typeSolver, new ReflectionTypeSolver(true)));
    assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 23 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue116 method arrayTypeIsNotPartOfTheTree.

@Test
public void arrayTypeIsNotPartOfTheTree() {
    CompilationUnit cu = parseSample("Issue116");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    com.github.javaparser.ast.type.Type typeNode = methodDeclaration.getParameters().get(0).getType();
    ResolvedType type = javaParserFacade.convert(typeNode, typeNode);
    assertEquals("java.lang.String[]", type.describe());
    ExpressionStmt expressionStmt = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(0);
    Expression argRef = expressionStmt.getExpression();
    assertEquals("java.lang.String[]", javaParserFacade.getType(argRef).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 24 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue18 method typeDeclarationSuperClassImplicitlyIncludeObject.

@Test
public void typeDeclarationSuperClassImplicitlyIncludeObject() {
    CompilationUnit cu = parseSample("Issue18");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
    ExpressionStmt expr = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(1);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType type = javaParserFacade.getType(expr.getExpression());
    assertEquals("java.lang.Object", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 25 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue235 method issue235.

@Test
public void issue235() {
    CompilationUnit cu = parseSample("Issue235");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Foo");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodDeclaration m = Navigator.demandMethod(cls, this.method);
    ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(0);
    ObjectCreationExpr expression = (ObjectCreationExpr) stmt.getExpression();
    Assert.assertNotNull(javaParserFacade.convertToUsage(expression.getType()));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)27 Test (org.junit.Test)19 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)11 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)10 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)8 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)8 Expression (com.github.javaparser.ast.expr.Expression)7 Statement (com.github.javaparser.ast.stmt.Statement)6 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)5 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)5 Parameter (com.github.javaparser.ast.body.Parameter)3 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)3 Then (org.jbehave.core.annotations.Then)3 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)2