@Override public Object visit(ASTName decl, Object data) { if (!decl.getImage().endsWith("toString")) { return data; } NameDeclaration nd = decl.getNameDeclaration(); if (nd == null) { return data; } if (alreadySeen.contains(nd) || !(nd instanceof TypedNameDeclaration) || nd instanceof TypedNameDeclaration && TypeHelper.isExactlyNone((TypedNameDeclaration) nd, StringBuffer.class, StringBuilder.class)) { return data; } alreadySeen.add(nd); if (isViolation(decl)) { addViolation(data, decl); } return data; }
@Override public Object visit(ASTName node, Object data) { Class<?> accessingClass = getEnclosingTypeDeclarationClass(node); String[] dotSplitImage = node.getImage().split("\\."); if (node.getType() != null) { // static field or method previousType = node.getTypeDefinition(); } else { // non-static field or method if (dotSplitImage.length == 1 && astArguments != null) { // method methods, astArgumentList); } else { // field previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass); if (node.getNameDeclaration() != null && node.getNameDeclaration().getNode() instanceof TypeNode) { JavaTypeDefinition nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getTypeDefinition(); if (nodeType != null) { node.setTypeDefinition(nodeType); return super.visit(node, data); node.setTypeDefinition(previousType);
@Override public Object visit(ASTName node, Object data) { if (!(node.jjtGetParent() instanceof ASTImportDeclaration) && !(node.jjtGetParent() instanceof ASTPackageDeclaration)) { // This name has no qualification, it can't be unnecessarily qualified if (node.getImage().indexOf('.') < 0) { return data; } checkImports(node, data); } return data; }
private boolean variableIsPassedToMethod(ASTPrimaryExpression expr, String variable) { List<ASTName> methodParams = new ArrayList<>(); expr.findDescendantsOfType(ASTName.class, methodParams, true); for (ASTName pName : methodParams) { String paramName = pName.getImage(); // also check if we've got the a parameter (i.e if it's an argument // !) ASTArgumentList parentParam = pName.getFirstParentOfType(ASTArgumentList.class); if (paramName.equals(variable) && parentParam != null) { return true; } } return false; }
private boolean throwsOneException(ASTMethodDeclaration node, Class<? extends Throwable> exception) { ASTNameList throwsList = node.getThrows(); if (throwsList != null && throwsList.jjtGetNumChildren() == 1) { ASTName n = (ASTName) throwsList.jjtGetChild(0); if (n.getType() == exception || exception.getSimpleName().equals(n.getImage()) || exception.getName().equals(n.getImage())) { return true; } } return false; }
Map<VariableNameDeclaration, List<NameOccurrence>> vars = name.getScope() .getDeclarations(VariableNameDeclaration.class); for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) { String var = name.getImage(); if (var.indexOf('.') != -1) { var = var.substring(0, var.indexOf('.'));
@Override public Object visit(ASTName node, Object data) { /* * Only doing this for nodes where getNameDeclaration is null this means * it's not a named node, i.e. Static reference or Annotation Doing this * for memory - TODO: Investigate if there is a valid memory concern or * not */ if (node.getNameDeclaration() == null) { // Skip these scenarios as there is no type to populate in these // cases: // 1) Parent is a PackageDeclaration, which is not a type // 2) Parent is a ImportDeclaration, this is handled elsewhere. if (!(node.jjtGetParent() instanceof ASTPackageDeclaration || node.jjtGetParent() instanceof ASTImportDeclaration)) { String name = node.getImage(); if (name.indexOf(StringAndCharConstants.DOT) != -1) { name = name.substring(0, name.indexOf(StringAndCharConstants.DOT)); } populateType(node, name); } } else { // Carry over the type from the declaration if (node.getNameDeclaration().getNode() instanceof TypeNode) { node.setType(((TypeNode)node.getNameDeclaration().getNode()).getType()); } } return super.visit(node, data); }
private boolean isAssignmentToFinalField(ASTStatementExpression n) { ASTName name = n.getFirstDescendantOfType(ASTName.class); return name != null && name.getNameDeclaration() instanceof VariableNameDeclaration && ((AccessNode) ((VariableNameDeclaration) name.getNameDeclaration()).getAccessNodeParent()).isFinal(); }
@Override public Object visit(final ASTName node, final Object data) { final NameDeclaration nameDeclaration = node.getNameDeclaration(); final ASTPrimaryExpression parentNode = node.getFirstParentOfType(ASTPrimaryExpression.class); .getImage().replace(nameDeclaration.getImage() + ".", "");
/** * Gets the image of the ASTName node found by * {@link Node#getFirstDescendantOfType(Class)} if it is the greatgrandchild * of the given node. * * E.g. * * <pre> * n = Expression || StatementExpression * PrimaryExpression * PrimaryPrefix * Name * </pre> * * @param n * the node to search * @return the image of the first ASTName or <code>null</code> */ protected String getFirstNameImage(Node n) { ASTName name = n.getFirstDescendantOfType(ASTName.class); if (name != null && name.getNthParent(3) == n) { return name.getImage(); } return null; }
&& initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTName) { ASTName name = (ASTName) initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); if (name == null || !volatileFields.contains(name.getImage())) { return false; if (!n.hasImageEqualTo(returnVariableName)) { continue; Node expression = n.getNthParent(3); if (expression instanceof ASTEqualityExpression) { continue; if (expression.jjtGetNumChildren() > 2 && expression.jjtGetChild(1) instanceof ASTAssignmentOperator) { ASTName value = expression.jjtGetChild(2).getFirstDescendantOfType(ASTName.class); if (value == null || !volatileFields.contains(value.getImage())) { return false;
final public void Name() throws ParseException { ASTName jjtn000 = new ASTName(this, JJTNAME); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1));StringBuilder s = new StringBuilder(); Token t; try { jjtn000.jjtSetLastToken(getToken(0)); jjtn000.setImage(s.toString()); } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
private boolean isStaticSimpleDateFormatCall(ASTPrimaryExpression primaryExpression, Set<String> localSimpleDateFormatNames) { if (primaryExpression.jjtGetNumChildren() == 0) { return false; } ASTName name = primaryExpression.getFirstDescendantOfType(ASTName.class); if (name == null || name.getType() != SimpleDateFormat.class) { return false; } if (localSimpleDateFormatNames.contains(name.getNameDeclaration().getName())) { return false; } ASTPrimaryPrefix primaryPrefix = (ASTPrimaryPrefix)primaryExpression.jjtGetChild(0); if (primaryPrefix.getType() != SimpleDateFormat.class) { return false; } Token token = (Token)primaryPrefix.jjtGetLastToken(); return FORMAT_METHOD_NAME.equals(token.image); }
private boolean isLockTypeAndMethod(ASTStatementExpression statementExpression, String methodName) { ASTName name = statementExpression.getFirstDescendantOfType(ASTName.class); if (name == null || name.getType() == null || !Lock.class.isAssignableFrom(name.getType())) { return false; } Token token = (Token)name.jjtGetLastToken(); return methodName.equals(token.image); } }
/** * Checks if the given exception is declared in the method or constructor * signature. * * @param exception * to evaluate * @return true if parent node is either a method or constructor declaration */ private boolean isParentSignatureDeclaration(ASTName exception) { Node parent = exception.jjtGetParent().jjtGetParent(); return parent instanceof ASTMethodDeclaration || parent instanceof ASTConstructorDeclaration; }
private boolean methodHasOverride(Node node) { ASTClassOrInterfaceBodyDeclaration method = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); if (method != null && method.jjtGetNumChildren() > 0 && method.jjtGetChild(0) instanceof ASTAnnotation) { ASTMarkerAnnotation marker = method.getFirstDescendantOfType(ASTMarkerAnnotation.class); if (marker != null && marker.getFirstChildOfType(ASTName.class) != null) { ASTName name = marker.getFirstChildOfType(ASTName.class); if (name.getType() == Override.class) { return true; } } } return false; } }
if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) { Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope() .getDeclarations(VariableNameDeclaration.class); for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName) node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } // no need to visit children, the only child, ASTName, will have no type return data; }
Node parent = decl.jjtGetParent().jjtGetParent(); if (parent.jjtGetNumChildren() == 4) { if (parent.jjtGetChild(0).getFirstChildOfType(ASTName.class).getImage().endsWith(".toString")) {