private boolean isAssignmentToFinalField(ASTStatementExpression n) { ASTName name = n.getFirstDescendantOfType(ASTName.class); return name != null && name.getNameDeclaration() instanceof VariableNameDeclaration && ((AccessNode) ((VariableNameDeclaration) name.getNameDeclaration()).getAccessNodeParent()).isFinal(); }
private boolean isReferencingThrowable(ASTPrimaryExpression last) { // check the variable type, if there is a reference by name ASTName variable = last.getFirstDescendantOfType(ASTName.class); if (variable != null && variable.getNameDeclaration() != null && variable.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration declaration = (VariableNameDeclaration) variable.getNameDeclaration(); if (declaration.getType() != null && Throwable.class.isAssignableFrom(declaration.getType())) { return true; } // convention: Exception type names should end with Exception if (declaration.getTypeImage() != null && declaration.getTypeImage().endsWith("Exception")) { return true; } } return false; }
@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; }
private ASTType getTypeNode(ASTName name) { if (name.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration(); if (vnd.getAccessNodeParent() instanceof ASTLocalVariableDeclaration) { ASTLocalVariableDeclaration l = (ASTLocalVariableDeclaration) vnd.getAccessNodeParent(); return l.getTypeNode(); } else if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) { ASTFormalParameter p = (ASTFormalParameter) vnd.getAccessNodeParent(); return p.getTypeNode(); } } return null; }
protected static boolean isInStringBufferOperation(Node node, int length, String methodName) { if (!(node.getNthParent(length) instanceof ASTStatementExpression)) { return false; } ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class); if (s == null) { return false; } ASTName n = s.getFirstDescendantOfType(ASTName.class); if (n == null || n.getImage().indexOf(methodName) == -1 || !(n.getNameDeclaration() instanceof TypedNameDeclaration)) { return false; } // TODO having to hand-code this kind of dredging around is ridiculous // we need something to support this in the framework // but, "for now" (tm): // if more than one arg to append(), skip it ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class); if (argList == null || argList.jjtGetNumChildren() > 1) { return false; } return TypeHelper.isExactlyAny((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class, StringBuilder.class); }
@Override public Object visit(final ASTName node, final Object data) { final NameDeclaration nameDeclaration = node.getNameDeclaration();
if (namedVar != null) { if (namedVar != null && namedVar.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration varDecl = (VariableNameDeclaration) namedVar.getNameDeclaration(); ASTVariableInitializer initializer = varDecl.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class); if (initializer != null) {
private static boolean isPrimitive(Node parent) { boolean result = false; if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) { Node child = parent.jjtGetChild(0); if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) { Node gc = child.jjtGetChild(0); if (gc instanceof ASTName) { ASTName name = (ASTName) gc; NameDeclaration nd = name.getNameDeclaration(); if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) { result = true; } } else if (gc instanceof ASTLiteral) { result = !((ASTLiteral) gc).isStringLiteral(); } } } return result; }
if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration(); AccessNode accessNodeParent = vnd.getAccessNodeParent(); if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
if (namedVar != null) { if (namedVar != null && namedVar.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration varDecl = (VariableNameDeclaration) namedVar.getNameDeclaration(); ASTVariableInitializer initializer = varDecl.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class); if (initializer != null) {
NameDeclaration nd = name.getNameDeclaration(); if (nd == null) { return data;
if (node.getNameDeclaration() != null && node.getNameDeclaration().getNode() instanceof TypeNode) { JavaTypeDefinition nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getTypeDefinition(); if (nodeType != null) { node.setTypeDefinition(nodeType);
ASTName n1 = (ASTName) c1.jjtGetChild(0); if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration(); VariableNameDeclaration nd1 = (VariableNameDeclaration) n1.getNameDeclaration();
ASTName arg = args.getFirstDescendantOfType(ASTName.class); if (arg != null) { NameDeclaration declaration = arg.getNameDeclaration(); if (declaration != null) { ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent()
private boolean isAssignmentToFinalField(ASTStatementExpression n) { ASTName name = n.getFirstDescendantOfType(ASTName.class); return name != null && name.getNameDeclaration() instanceof VariableNameDeclaration && ((AccessNode) ((VariableNameDeclaration) name.getNameDeclaration()).getAccessNodeParent()).isFinal(); }
private boolean isReferencingThrowable(ASTPrimaryExpression last) { // check the variable type, if there is a reference by name ASTName variable = last.getFirstDescendantOfType(ASTName.class); if (variable != null && variable.getNameDeclaration() != null && variable.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration declaration = (VariableNameDeclaration) variable.getNameDeclaration(); if (declaration.getType() != null && Throwable.class.isAssignableFrom(declaration.getType())) { return true; } // convention: Exception type names should end with Exception if (declaration.getTypeImage() != null && declaration.getTypeImage().endsWith("Exception")) { return true; } } return false; }
@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; }
private ASTType getTypeNode(ASTName name) { if (name.getNameDeclaration() instanceof VariableNameDeclaration) { VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration(); if (vnd.getAccessNodeParent() instanceof ASTLocalVariableDeclaration) { ASTLocalVariableDeclaration l = (ASTLocalVariableDeclaration) vnd.getAccessNodeParent(); return l.getTypeNode(); } else if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) { ASTFormalParameter p = (ASTFormalParameter) vnd.getAccessNodeParent(); return p.getTypeNode(); } } return null; }
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 static boolean isPrimitive(Node parent) { boolean result = false; if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) { Node child = parent.jjtGetChild(0); if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) { Node gc = child.jjtGetChild(0); if (gc instanceof ASTName) { ASTName name = (ASTName) gc; NameDeclaration nd = name.getNameDeclaration(); if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) { result = true; } } else if (gc instanceof ASTLiteral) { result = !((ASTLiteral) gc).isStringLiteral(); } } } return result; }