Search in sources :

Example 1 with AssignExpr

use of com.github.javaparser.ast.expr.AssignExpr 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 2 with AssignExpr

use of com.github.javaparser.ast.expr.AssignExpr in project javaparser by javaparser.

the class ContextTest method resolveInheritedFieldReference.

@Test
public void resolveInheritedFieldReference() {
    CompilationUnit cu = parseSample("ReferencesToField");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferencesToFieldExtendingClass");
    MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "method2");
    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 3 with AssignExpr

use of com.github.javaparser.ast.expr.AssignExpr in project javaparser by javaparser.

the class FieldDeclaration method createSetter.

/**
 * Create a setter for this field, <b>will only work if this field declares only 1 identifier and if this field is
 * already added to a ClassOrInterfaceDeclaration</b>
 *
 * @return the {@link MethodDeclaration} created
 * @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
 *             class or enum
 */
public MethodDeclaration createSetter() {
    if (getVariables().size() != 1)
        throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
    ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
    EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
    if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
        throw new IllegalStateException("You can use this only when the field is attached to a class or an enum");
    VariableDeclarator variable = getVariables().get(0);
    String fieldName = variable.getId().getName();
    String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
    final MethodDeclaration setter;
    if (parentClass != null)
        setter = parentClass.addMethod("set" + fieldNameUpper, PUBLIC);
    else
        setter = parentEnum.addMethod("set" + fieldNameUpper, PUBLIC);
    setter.setType(VOID_TYPE);
    setter.getParameters().add(new Parameter(variable.getType(), new VariableDeclaratorId(fieldName)));
    BlockStmt blockStmt2 = new BlockStmt();
    setter.setBody(blockStmt2);
    blockStmt2.addStatement(new AssignExpr(new NameExpr("this." + fieldName), new NameExpr(fieldName), Operator.assign));
    return setter;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

AssignExpr (com.github.javaparser.ast.expr.AssignExpr)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)2 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)2 Test (org.junit.Test)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1