private static boolean isNotEquals(Node node) { // look for "x != y" return node instanceof ASTEqualityExpression && "!=".equals(node.getImage()); }
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; }
private static String[] imagesOf(List<? extends Node> nodes) { String[] imageArray = new String[nodes.size()]; for (int i = 0; i < nodes.size(); i++) { imageArray[i] = nodes.get(i).getImage(); } return imageArray; }
/** * Returns the name of the annotation as it is used, * eg {@code java.lang.Override} or {@code Override}. */ public String getAnnotationName() { return jjtGetChild(0).getImage(); }
/** * Returns the full name of the import. For on-demand imports, this is the name without * the final dot and asterisk. */ public String getImportedName() { return jjtGetChild(0).getImage(); }
/** * Returns the name of the annotation as it is used, * eg {@code java.lang.Override} or {@code Override}. */ public String getAnnotationName() { return jjtGetChild(0).getImage(); }
/** * Returns the name of the declared variable. */ public String getName() { // first child will be VariableDeclaratorId return jjtGetChild(0).getImage(); }
/** * Returns the name of the annotation as it is used, * eg {@code java.lang.Override} or {@code Override}. */ public String getAnnotationName() { return jjtGetChild(0).getImage(); }
private boolean basicEquivalence(Node node1, Node node2, String varName1, String varName2) { // Nodes must have the same type if (node1.getClass() != node2.getClass()) { return false; } String image1 = node1.getImage(); String image2 = node2.getImage(); // image of nodes must be the same return Objects.equals(image1, image2) // or must be references to the variable we allow to interchange || Objects.equals(image1, varName1) && Objects.equals(image2, varName2) // which means we must filter out method references. && isNoMethodName(node1) && isNoMethodName(node2); }
@Override public Object visit(ASTImportDeclaration node, Object data) { String img = node.jjtGetChild(0).getImage(); if (img.startsWith("sun.") && !img.startsWith("sun.misc.Signal")) { addViolation(data, node); } return data; }
@Override public Object visit(final ASTConstructorDeclaration node, final Object data) { if (node.isPrivate()) { final String className = node.jjtGetParent().jjtGetParent().jjtGetParent().getImage(); if (!privateConstructors.containsKey(className)) { privateConstructors.put(className, new ArrayList<ASTConstructorDeclaration>()); } privateConstructors.get(className).add(node); } return 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 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()); } } } }
@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; } }
public String getName() { if (node instanceof ASTConstructorDeclaration) { return this.getEnclosingScope(ClassScope.class).getClassName(); } return node.jjtGetChild(1).getImage(); }
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; }
@Override public Object visit(ASTAllocationExpression node, Object data) { if (!inPrimaryExpressionContext || !(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) { return super.visit(node, data); } if (!PRIMITIVE_WRAPPERS.contains(node.jjtGetChild(0).getImage())) { return super.visit(node, data); } usingPrimitiveWrapperAllocation = true; return super.visit(node, data); }
@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; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (int x = 0; x < node.jjtGetNumChildren(); ++x) { Node firstStmt = node.jjtGetChild(x); if (firstStmt instanceof ASTType) { ASTType tp = (ASTType) firstStmt; Node nd = tp.jjtGetChild(0); checkVariableType(nd, nd.getImage()); } } return super.visit(node, data); }
private boolean hasArraysCopyOf(ASTReturnStatement ret) { List<ASTPrimaryExpression> expressions = ret.findDescendantsOfType(ASTPrimaryExpression.class); for (ASTPrimaryExpression e : expressions) { if (e.jjtGetNumChildren() == 2 && e.jjtGetChild(0) instanceof ASTPrimaryPrefix && e.jjtGetChild(0).jjtGetNumChildren() == 1 && e.jjtGetChild(0).jjtGetChild(0) instanceof ASTName && e.jjtGetChild(0).jjtGetChild(0).getImage().endsWith("Arrays.copyOf")) { return true; } } return false; }