This commit is contained in:
Young Chan
2020-05-31 10:36:40 +08:00
committed by Andreas Dangel
parent 11a0ec51fd
commit f5ccc94130

View File

@ -31,6 +31,12 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
addRuleChainVisit(ASTVariableDeclaratorId.class); addRuleChainVisit(ASTVariableDeclaratorId.class);
} }
/**
* This method is used to check whether user appends string directly instead of using StringBuffer or StringBuilder
* @param node This is the expression of part of java code to be checked.
* @param data This is the data to return.
* @return Object This returns the data passed in. If violation happens, violation is added to data.
*/
@Override @Override
public Object visit(ASTVariableDeclaratorId node, Object data) { public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray() if (!TypeHelper.isA(node, String.class) || node.isArray()
@ -76,8 +82,10 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
} }
} }
if (statement.getNumChildren() > 0 && statement.getChild(0) instanceof ASTPrimaryExpression) { if (statement.getNumChildren() > 0 && statement.getChild(0) instanceof ASTPrimaryExpression) {
// System.out.println(name.toString());
ASTName astName = statement.getChild(0).getFirstDescendantOfType(ASTName.class); ASTName astName = statement.getChild(0).getFirstDescendantOfType(ASTName.class);
if (astName != null) { if (astName != null) {
// System.out.println(astName.getNameDeclaration().getName());
if (astName.equals(name)) { if (astName.equals(name)) {
ASTAssignmentOperator assignmentOperator = statement ASTAssignmentOperator assignmentOperator = statement
.getFirstDescendantOfType(ASTAssignmentOperator.class); .getFirstDescendantOfType(ASTAssignmentOperator.class);