protected Node getFirstChild(Node contextNode) { if (contextNode.jjtGetNumChildren() > 0) { return contextNode.jjtGetChild(0); } else { return null; } }
private int checkEachChildOnNextLine(Object data, Node parent, int firstLine, int indentation) { int currentLine = firstLine; for (int i = 0; i < parent.jjtGetNumChildren(); i++) { Node child = parent.jjtGetChild(i); if (child.getBeginLine() != currentLine) { addViolationWithMessage(data, child, child.getImage() + " should be on line " + currentLine); } else if (i > 0 && child.getBeginColumn() != indentation) { addViolationWithMessage(data, child, child.getImage() + " should begin at column " + indentation); } // next entry needs to be on the next line currentLine++; } return currentLine; }
public void closeNodeScope(Node n, int num) { mk = marks.remove(marks.size()-1); while (num-- > 0) { Node c = popNode(); c.jjtSetParent(n); n.jjtAddChild(c, num); } n.jjtClose(); pushNode(n); node_created = true; }
/** * This method can be called on a prefix */ private ASTArguments getSuffixMethodArgs(Node node) { Node prefix = node.jjtGetParent(); if (prefix instanceof ASTPrimaryPrefix && prefix.jjtGetParent().jjtGetNumChildren() >= 2) { return prefix.jjtGetParent().jjtGetChild(1).getFirstChildOfType(ASTArguments.class); } return null; }
protected Node getNextSibling(Node contextNode) { Node parentNode = contextNode.jjtGetParent(); if (parentNode != null) { int nextPosition = contextNode.jjtGetChildIndex() + 1; if (nextPosition < parentNode.jjtGetNumChildren()) { return parentNode.jjtGetChild(nextPosition); } } return null; }
/** * Indicate whether this node is allocating a new object. * * @param n * node that might be allocating a new object * @return true if child 0 is an AllocationExpression */ private boolean isAllocation(Node n) { return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTAllocationExpression && n.jjtGetParent().jjtGetNumChildren() == 1; }
public ParametricRuleViolation(Rule theRule, RuleContext ctx, T node, String message) { rule = theRule; description = message; filename = ctx.getSourceCodeFilename(); if (filename == null) { filename = ""; } if (node != null) { beginLine = node.getBeginLine(); beginColumn = node.getBeginColumn(); endLine = node.getEndLine(); endColumn = node.getEndColumn(); } // Apply Rule specific suppressions if (node != null && rule != null) { setSuppression(rule, node); } }
@Override public boolean isTargetMethod(JavaNameOccurrence occ) { if (occ.getNameForWhichThisIsAQualifier() != null && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("trim") != -1) { Node pExpression = occ.getLocation().jjtGetParent().jjtGetParent(); if (pExpression.jjtGetNumChildren() > 2 && "length".equals(pExpression.jjtGetChild(2).getImage())) { return true; } } return false; }
private void collectNames(String target, List<String> names, Node node) { for (int i = 0; i < node.jjtGetNumChildren(); i++) { Node child = node.jjtGetChild(i); if (child.jjtGetNumChildren() > 0) { collectNames(target, names, child); } else { if (child instanceof ASTName && isQualifiedName(child) && target.equals(getVariableName(child.getImage()))) { names.add(child.getImage()); } } } }
private static int compareNodes(Node n1, Node n2) { int l1 = n1.getBeginLine(); int l2 = n2.getBeginLine(); if (l1 == l2) { return n1.getBeginColumn() - n2.getBeginColumn(); } return l1 - l2; }
private void setVariableNameIfExists(Node node) { if (node instanceof ASTFieldDeclaration) { variableName = getVariableNames((ASTFieldDeclaration) node); } else if (node instanceof ASTLocalVariableDeclaration) { variableName = getVariableNames((ASTLocalVariableDeclaration) node); } else if (node instanceof ASTVariableDeclarator) { variableName = node.jjtGetChild(0).getImage(); } else if (node instanceof ASTVariableDeclaratorId) { variableName = node.getImage(); } else if (node instanceof ASTFormalParameter) { setVariableNameIfExists(node.getFirstChildOfType(ASTVariableDeclaratorId.class)); } else { variableName = ""; } } }
protected Node getPreviousSibling(Node contextNode) { Node parentNode = contextNode.jjtGetParent(); if (parentNode != null) { int prevPosition = contextNode.jjtGetChildIndex() - 1; if (prevPosition >= 0) { return parentNode.jjtGetChild(prevPosition); } } return null; }
if (argument.jjtGetChild(0).getEndColumn() > longestParameterEndColumn) { longestParameterEndColumn = argument.jjtGetChild(0).getEndColumn(); && arguments.get(0).jjtGetChild(1).getBeginColumn() > expectedBeginColumn) { expectedBeginColumn = arguments.get(0).jjtGetChild(1).getBeginColumn(); checkIndentation(data, expr, expectedBeginColumn, expr.getImage()); if (primaryExpression.getEndLine() != node.getEndLine() + 1) { addViolationWithMessage(data, primaryExpression, "Closing paranthesis should be on a new line.");
@Override public Object visit(ASTSubqueryOperation node, Object data) { // get previous sibling int thisIndex = node.jjtGetChildIndex(); Node prevSibling = node.jjtGetParent().jjtGetChild(thisIndex - 1); checkIndentation(data, node, prevSibling.getBeginColumn(), node.getImage()); // it should also be on the next line if (node.getBeginLine() != prevSibling.getEndLine() + 1) { addViolationWithMessage(data, node, node.getImage() + " should be on line " + (prevSibling.getEndLine() + 1)); } return super.visit(node, data); }
/** * Returns the name of the annotation as it is used, * eg {@code java.lang.Override} or {@code Override}. */ public String getAnnotationName() { return jjtGetChild(0).jjtGetChild(0).getImage(); }
private <T extends AstNode> EcmascriptNode<T> buildInternal(T astNode) { // Create a Node EcmascriptNode<T> node = createNodeAdapter(astNode); // Append to parent Node parent = nodes.isEmpty() ? null : nodes.peek(); if (parent != null) { parent.jjtAddChild(node, parent.jjtGetNumChildren()); node.jjtSetParent(parent); } handleParseProblems(node); // Build the children... nodes.push(node); parents.push(astNode); astNode.visit(this); nodes.pop(); parents.pop(); return node; }
private static boolean isAfter(Node n1, Node n2) { return n1.getBeginLine() > n2.getBeginLine() || n1.getBeginLine() == n2.getBeginLine() && n1.getBeginColumn() >= n2.getEndColumn(); }
private ASTTypeParameter getTypeParameterDeclaration(Node startNode, String image) { for (Node parent = startNode.jjtGetParent(); parent != null; parent = parent.jjtGetParent()) { ASTTypeParameters typeParameters = null; if (parent instanceof ASTTypeParameters) { // if type parameter defined in the same < > typeParameters = (ASTTypeParameters) parent; } else if (parent instanceof ASTConstructorDeclaration || parent instanceof ASTMethodDeclaration || parent instanceof ASTClassOrInterfaceDeclaration) { typeParameters = parent.getFirstChildOfType(ASTTypeParameters.class); } if (typeParameters != null) { for (int index = 0; index < typeParameters.jjtGetNumChildren(); ++index) { String imageToCompareTo = typeParameters.jjtGetChild(index).getImage(); if (imageToCompareTo != null && imageToCompareTo.equals(image)) { return (ASTTypeParameter) typeParameters.jjtGetChild(index); } } } } return null; }
private boolean isCommentNotWithin(FormalComment n1, Node n2, Node node) { if (n1 == null || n2 == null || node == null) { return true; } boolean isNotWithinNode2 = !(n1.getEndLine() < n2.getEndLine() || n1.getEndLine() == n2.getEndLine() && n1.getEndColumn() < n2.getEndColumn()); boolean isNotSameClass = node.getFirstParentOfType(ASTClassOrInterfaceBody.class) != n2 .getFirstParentOfType(ASTClassOrInterfaceBody.class); boolean isNodeWithinNode2 = node.getEndLine() < n2.getEndLine() || node.getEndLine() == n2.getEndLine() && node.getEndColumn() < n2.getEndColumn(); return isNotWithinNode2 || isNotSameClass || isNodeWithinNode2; }
private String getLastPartOfName(Node name) { String result = ""; if (name != null) { result = name.getImage(); } int dotIndex = result.lastIndexOf('.'); if (dotIndex > -1 && result.length() > dotIndex + 1) { result = result.substring(dotIndex + 1); } return result; }