public Object visit(ASTPrimaryPrefix node, Object data) { if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) { return super.visit(node, data); } String image = ((ASTName) node.jjtGetChild(0)).getImage(); if (image.startsWith("java.lang.")) { image = image.substring(10); } boolean checkBoolean = ((RuleContext) data) .getLanguageVersion() .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0; if (PREFIX_SET.contains(image) || (checkBoolean && "Boolean.valueOf".equals(image))) { ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent(); if (parent.jjtGetNumChildren() >= 3) { Node n = parent.jjtGetChild(2); if (n instanceof ASTPrimarySuffix) { ASTPrimarySuffix suffix = (ASTPrimarySuffix) n; image = suffix.getImage(); if (SUFFIX_SET.contains(image) || (checkBoolean && "booleanValue".equals(image))) { super.addViolation(data, node); return data; } } } } return super.visit(node, data); }
/** * Checks if the variable designed in parameter is written to a field (not local variable) in the * statements. */ private boolean checkForDirectAssignment( Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) { final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class); final String varName = vid.getImage(); for (ASTBlockStatement b : bs) { if (b.hasDescendantOfType(ASTAssignmentOperator.class)) { final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class); if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) { continue; } String assignedVar = getExpressionVarName(se); if (assignedVar == null) { continue; } ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0); Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class); if (n == null) { n = pe.getFirstParentOfType(ASTConstructorDeclaration.class); if (n == null) { continue; } } if (!isLocalVariable(assignedVar, n)) { // TODO could this be more clumsy? We really // need to build out the PMD internal framework more // to support simply queries like "isAssignedTo()" or something if (se.jjtGetNumChildren() < 3) { continue; } ASTExpression e = (ASTExpression) se.jjtGetChild(2); if (e.hasDescendantOfType(ASTEqualityExpression.class)) { continue; } String val = getExpressionVarName(e); if (val == null) { continue; } ASTPrimarySuffix foo = se.getFirstDescendantOfType(ASTPrimarySuffix.class); if (foo != null && foo.isArrayDereference()) { continue; } if (val.equals(varName)) { Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class); if (md == null) { md = pe.getFirstParentOfType(ASTConstructorDeclaration.class); } if (!isLocalVariable(varName, md)) { addViolation(ctx, parameter, varName); } } } } } return false; }
public Object visit(ASTPrimarySuffix node, Object data) { if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) { if (node.hasImageEqualTo("toString")) { if (node.jjtGetParent() == primary) { addViolation(data, node); } } } return super.visit(node, data); }
private String getExpressionVarName(Node e) { String assignedVar = getFirstNameImage(e); if (assignedVar == null) { ASTPrimarySuffix suffix = e.getFirstDescendantOfType(ASTPrimarySuffix.class); if (suffix != null) { assignedVar = suffix.getImage(); ASTPrimaryPrefix prefix = e.getFirstDescendantOfType(ASTPrimaryPrefix.class); if (prefix != null) { if (prefix.usesThisModifier()) { assignedVar = "this." + assignedVar; } else if (prefix.usesSuperModifier()) { assignedVar = "super." + assignedVar; } } } } return assignedVar; }