/** * Returns the name of the variable. */ public String getVariableName() { return getImage(); }
/** * Returns exception name caught by this catch block. */ public String getExceptionName() { return getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(); }
/** * Gets the variable name of this declaration. This method searches the first * VariableDeclartorId node and returns it's image or <code>null</code> if * the child node is not found. * * @deprecated LocalVariableDeclaration may declare several variables, so this is not exhaustive * Iterate on the {@linkplain ASTVariableDeclaratorId VariableDeclaratorIds} instead * * @return a String representing the name of the variable */ // It would be nice to have a way to inform XPath users of the intended replacement // for a deprecated attribute. We may use another annotation for that. @Deprecated public String getVariableName() { ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (decl != null) { return decl.getImage(); } return null; }
/** * Gets the variable name of this field. This method searches the first * VariableDeclartorId node and returns its image or <code>null</code> if * the child node is not found. * * * @deprecated FieldDeclaration may declare several variables, so this is not exhaustive * Iterate on the {@linkplain ASTVariableDeclaratorId VariableDeclaratorIds} instead * * * @return a String representing the name of the variable */ @Deprecated public String getVariableName() { ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class); if (decl != null) { return decl.getImage(); } return null; }
@Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (node.getImage().indexOf('$') != -1) { addViolation(data, node); return data; } return super.visit(node, data); }
@Override public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) { if (fieldDeclaration.isVolatile()) { for (ASTVariableDeclaratorId declarator : fieldDeclaration .findDescendantsOfType(ASTVariableDeclaratorId.class)) { this.volatileFields.add(declarator.getImage()); } } return super.visit(fieldDeclaration, data); }
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) { for (NameOccurrence no : declaration.getUsages()) { Node location = no.getLocation(); System.out.println(declaration.getImage() + " is used here: " + location.getImage()); } } return data; } }
String varName = variableDeclaratorId.getImage();
@Override public Object visit(final ASTFieldDeclaration decl, final Object data) { if (shouldReport(decl)) { addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(), "field")); } return super.visit(decl, data); }
private int getAmountOfExpectedArguments(final String variableName, final List<ASTVariableDeclarator> variables) { for (final ASTVariableDeclarator astVariableDeclarator : variables) { if (astVariableDeclarator.getFirstChildOfType(ASTVariableDeclaratorId.class).getImage() .equals(variableName)) { ASTVariableInitializer variableInitializer = astVariableDeclarator .getFirstDescendantOfType(ASTVariableInitializer.class); ASTExpression expression = null; if (variableInitializer != null) { expression = variableInitializer.getFirstChildOfType(ASTExpression.class); } if (expression != null) { return countPlaceholders(expression); } } } return 0; }
@Override public Object visit(ASTLocalVariableDeclaration node, Object data) { ASTClassOrInterfaceType type = null; if (!node.isTypeInferred()) { type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class); } if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) { ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class); if (declarator != null) { ASTName name = declarator.getFirstDescendantOfType(ASTName.class); if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) { ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class); resultSetVariables.put(id.getImage(), node); } } } return super.visit(node, data); }
@Override public Object visit(ASTLocalVariableDeclaration node, Object data) { ASTVariableDeclaratorId var = getDeclaration(node); if (var == null) { return super.visit(node, data); } String variableName = var.getImage(); for (NameOccurrence no : var.getUsages()) { // FIXME - getUsages will return everything with the same name as // the variable, // see JUnit test, case 6. Changing to Node below, revisit when // getUsages is fixed Node sn = no.getLocation(); Node primaryExpression = sn.jjtGetParent().jjtGetParent(); Class<? extends Node> parentClass = primaryExpression.jjtGetParent().getClass(); if (parentClass.equals(ASTStatementExpression.class)) { String methodCall = sn.getImage().substring(variableName.length()); ASTType nodeType = node.getTypeNode(); if (nodeType != null) { if (MAP_CLASSES.get(nodeType.getTypeImage()).contains(methodCall)) { addViolation(data, sn); } } } } return super.visit(node, data); }
private String getNodeName(Node node) { // constructors are differentiated by their parameters, while we only use method name for methods if (node instanceof ASTMethodDeclaration) { return ((ASTMethodDeclaration) node).getMethodName(); } else if (node instanceof ASTMethodOrConstructorDeclaration) { // constructors are differentiated by their parameters, while we only use method name for methods return ((ASTConstructorDeclaration) node).getQualifiedName().getOperation(); } else if (node instanceof ASTFieldDeclaration) { return ((ASTFieldDeclaration) node).getVariableName(); } else if (node instanceof ASTResource) { return ((ASTResource) node).getVariableDeclaratorId().getImage(); } else { return node.getImage(); } }
private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) { String variableToClose = id.getImage(); Node n = var;
final Set<String> loopVariables = new HashSet<>(); for (ASTVariableDeclaratorId declaratorId : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) { loopVariables.add(declaratorId.getImage());
final String name = primarySuffix.getImage(); if ("append".equals(name)) { String variable = vdId.getImage(); if (isAStringBuilderBuffer(primarySuffix, variable)) { return variable;
addViolation(data, node, new Object[] { declaration.getImage() });
@Override public Object visit(ASTCatchStatement catchStmt, Object data) { String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
final List<ASTBlockStatement> bs) { final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class); final String varName = vid.getImage(); for (ASTBlockStatement b : bs) { if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
ASTVariableDeclaratorId variableId = findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0); if (!argumentName.hasImageEqualTo(variableId.getImage())) {