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());
}
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());
}
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;
}
Aggregations