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; }
final public void VariableDeclarator() throws ParseException { ASTVariableDeclarator jjtn000 = new ASTVariableDeclarator(this, JJTVARIABLEDECLARATOR); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { VariableDeclaratorId(); if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
private boolean indexStartsAtZero(VariableNameDeclaration index) { ASTVariableDeclaratorId name = (ASTVariableDeclaratorId) index.getNode(); ASTVariableDeclarator declarator = name.getFirstParentOfType(ASTVariableDeclarator.class); if (declarator == null) { return false; } try { List<Node> zeroLiteral = declarator.findChildNodesWithXPath( "./VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0' and " + "@StringLiteral='false']"); if (!zeroLiteral.isEmpty()) { return true; } } catch (JaxenException je) { throw new RuntimeException(je); } return false; }
@Override public Object visit(ASTVariableDeclarator node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("createNewNode ASTVariableDeclarator: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } dataFlow.createNewNode(node); return super.visit(node, data); }
for (ASTVariableDeclarator declarator : variableDeclarators) { ASTVariableDeclaratorId variableDeclaratorId = declarator .getFirstChildOfType(ASTVariableDeclaratorId.class); .getFirstDescendantOfType(ASTAllocationExpression.class) != null; boolean iterator = isIterator() || isFactory(declarator); boolean forLoop = isForLoop(declarator); assignments.add(new Assignment(declarator.getBeginLine(), allocationFound, iterator, forLoop));
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) { Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class) .getFirstChildOfType(ASTVariableInitializer.class); if (initializer == null) { return null; } ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class); if (nameNode == null) { // TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled return null; } String name = nameNode.getImage(); int dotIndex = name.indexOf('.'); if (dotIndex > 0) { name = name.substring(0, dotIndex); } return findDeclaration(name, scope); }
if (variableDeclarator.getType() == SimpleDateFormat.class) { ASTVariableDeclaratorId variableDeclaratorId = variableDeclarator.getFirstChildOfType(ASTVariableDeclaratorId.class); localSimpleDateFormatNames.add(variableDeclaratorId.getImage()); return;
/** * Report when number of variable usages is equal to zero. * @param node Node to check. * @param data Context. * @param name Variable name. */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private void usages(final JavaNode node, final Object data, final ASTVariableDeclarator name) { final Map<NameDeclaration, List<NameOccurrence>> vars = name .getScope().getDeclarations(); // @checkstyle LineLength (1 line) for (final Map.Entry<NameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) { final List<NameOccurrence> usages = entry.getValue(); if (usages.size() > 1) { continue; } for (final NameOccurrence occ: usages) { if (occ.getLocation().equals(name)) { this.addViolation( data, node, new Object[]{name.getImage()} ); } } } } }
private boolean checkThreadLocalWithInitalValue(ASTFieldDeclaration fieldDeclaration) { ASTVariableDeclarator variableDeclarator = fieldDeclaration.getFirstDescendantOfType( ASTVariableDeclarator.class); if (variableDeclarator == null) { return false; } ASTMethodDeclarator initialValueMethod = variableDeclarator.getFirstDescendantOfType(ASTMethodDeclarator.class); if (initialValueMethod != null && METHOD_INITIAL_VALUE.equals(initialValueMethod.getImage())) { return true; } ASTName name = variableDeclarator.getFirstDescendantOfType(ASTName.class); return name != null && WITH_INITIAL.equals(name.getImage()); }
private void checkField(ASTType typeNode, ASTVariableDeclarator node, Object data) { for (String prefix : getProperty(BOOLEAN_FIELD_PREFIXES_PROPERTY)) { if (hasPrefix(node.getName(), prefix) && !isBooleanType(typeNode)) { addViolationWithMessage(data, node, "Linguistics Antipattern - The field ''{0}'' indicates linguistically it is a boolean, but it is ''{1}''", new Object[] { node.getName(), typeNode.getTypeImage() }); } } }
private Object checkVariableDeclarators(List<String> prefixes, List<String> suffixes, Node root, boolean isStatic, boolean isFinal, Object data) { for (ASTVariableDeclarator variableDeclarator : root.findChildrenOfType(ASTVariableDeclarator.class)) { for (ASTVariableDeclaratorId variableDeclaratorId : variableDeclarator .findChildrenOfType(ASTVariableDeclaratorId.class)) { checkVariableDeclaratorId(prefixes, suffixes, isStatic, isFinal, variableDeclaratorId, data); } } return data; }
private boolean isFactory(ASTVariableDeclarator declarator) { boolean factory = false; List<ASTName> names = declarator.findDescendantsOfType(ASTName.class); for (ASTName name : names) { if (name.getImage().toLowerCase(Locale.ROOT).contains("factory")) { factory = true; break; } } return factory; }
for (ASTVariableDeclarator declarator : variableDeclarators) { ASTVariableDeclaratorId variableDeclaratorId = declarator .getFirstChildOfType(ASTVariableDeclaratorId.class); .getFirstDescendantOfType(ASTAllocationExpression.class) != null; boolean iterator = isIterator() || isFactory(declarator); boolean forLoop = isForLoop(declarator); assignments.add(new Assignment(declarator.getBeginLine(), allocationFound, iterator, forLoop));
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) { Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class) .getFirstChildOfType(ASTVariableInitializer.class); if (initializer == null) { return null; } ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class); if (nameNode == null) { // TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled return null; } String name = nameNode.getImage(); int dotIndex = name.indexOf('.'); if (dotIndex > 0) { name = name.substring(0, dotIndex); } return findDeclaration(name, scope); }
@Override public Object visit(ASTVariableDeclarator node, Object data) { if (!(data instanceof Structure)) { return data; } Structure dataFlow = (Structure) data; if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("createNewNode ASTVariableDeclarator: line " + node.getBeginLine() + ", column " + node.getBeginColumn()); } dataFlow.createNewNode(node); return super.visit(node, data); }
/** * Report when number of variable usages is equal to zero. * @param node Node to check. * @param data Context. * @param name Variable name. */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private void usages(final JavaNode node, final Object data, final ASTVariableDeclarator name) { final Map<NameDeclaration, List<NameOccurrence>> vars = name .getScope().getDeclarations(); // @checkstyle LineLength (1 line) for (final Map.Entry<NameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) { final List<NameOccurrence> usages = entry.getValue(); if (usages.size() > 1) { continue; } for (final NameOccurrence occ: usages) { if (occ.getLocation().equals(name)) { this.addViolation( data, node, new Object[]{name.getImage()} ); } } } } }
private void checkVariable(ASTType typeNode, ASTVariableDeclarator node, Object data) { for (String prefix : getProperty(BOOLEAN_FIELD_PREFIXES_PROPERTY)) { if (hasPrefix(node.getName(), prefix) && !isBooleanType(typeNode)) { addViolationWithMessage(data, node, "Linguistics Antipattern - The variable ''{0}'' indicates linguistically it is a boolean, but it is ''{1}''", new Object[] { node.getName(), typeNode.getTypeImage() }); } } }
private Object checkVariableDeclarators(List<String> prefixes, List<String> suffixes, Node root, boolean isStatic, boolean isFinal, Object data) { for (ASTVariableDeclarator variableDeclarator : root.findChildrenOfType(ASTVariableDeclarator.class)) { for (ASTVariableDeclaratorId variableDeclaratorId : variableDeclarator .findChildrenOfType(ASTVariableDeclaratorId.class)) { checkVariableDeclaratorId(prefixes, suffixes, isStatic, isFinal, variableDeclaratorId, data); } } return data; }
private boolean isFactory(ASTVariableDeclarator declarator) { boolean factory = false; List<ASTName> names = declarator.findDescendantsOfType(ASTName.class); for (ASTName name : names) { if (name.getImage().toLowerCase(Locale.ROOT).contains("factory")) { factory = true; break; } } return factory; }
@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); }