Search in sources :

Example 26 with ExpressionStmt

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

the class FieldsResolutionTest method accessClassFieldThroughThisWithCompetingSymbolInParentContext.

@Test
public void accessClassFieldThroughThisWithCompetingSymbolInParentContext() {
    CompilationUnit cu = parseSample("AccessClassMemberThroughThis");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessClassMemberThroughThis");
    MethodDeclaration method = Navigator.demandMethod(clazz, "setLabel");
    ExpressionStmt expressionStmt = (ExpressionStmt) method.getBody().get().getStatements().get(0);
    AssignExpr assignExpr = (AssignExpr) expressionStmt.getExpression();
    FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) assignExpr.getTarget();
    File src = adaptPath(new File("src/test/resources"));
    CombinedTypeSolver typeSolver = new CombinedTypeSolver(new JavaParserTypeSolver(src), new ReflectionTypeSolver());
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference<? extends ResolvedValueDeclaration> ref = symbolSolver.solveSymbol(fieldAccessExpr.getName().getId(), fieldAccessExpr);
    assertTrue(ref.isSolved());
    assertTrue(ref.getCorrespondingDeclaration().isField());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) Test(org.junit.Test)

Example 27 with ExpressionStmt

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

the class GenericsResolutionTest method resolveUsageOfGenericFieldSimpleCase.

@Test
public void resolveUsageOfGenericFieldSimpleCase() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    ExpressionStmt stmt = (ExpressionStmt) method.getBody().get().getStatements().get(0);
    Expression expression = stmt.getExpression();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.lang.String", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) 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