use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class MethodReturnsInternalArrayRule method visit.
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
if (!method.getResultType().returnsArray() || method.isPrivate()) {
return data;
}
List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class);
ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class);
for (ASTReturnStatement ret : returns) {
final String vn = getReturnedVariableName(ret);
if (!isField(vn, td)) {
continue;
}
if (ret.findDescendantsOfType(ASTPrimarySuffix.class).size() > 2) {
continue;
}
if (ret.hasDescendantOfType(ASTAllocationExpression.class)) {
continue;
}
if (hasArraysCopyOf(ret)) {
continue;
}
if (hasClone(ret, vn)) {
continue;
}
if (isEmptyArray(vn, td)) {
continue;
}
if (!isLocalVariable(vn, method)) {
addViolation(data, ret, vn);
} else {
// This is to handle field hiding
final ASTPrimaryPrefix pp = ret.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (pp != null && pp.usesThisModifier()) {
final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (ps.hasImageEqualTo(vn)) {
addViolation(data, ret, vn);
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class AbstractSunSecureRule method getReturnedVariableName.
/**
* Gets the name of the variable returned. Some examples: <br>
* for this.foo returns foo <br>
* for foo returns foo <br>
* for foo.bar returns foo.bar
*
* @param ret
* a return statement to evaluate
* @return the name of the variable associated or <code>null</code> if it
* cannot be detected
*/
protected final String getReturnedVariableName(ASTReturnStatement ret) {
if (hasTernaryCondition(ret) && hasTernaryNullCheck(ret)) {
return ret.getFirstDescendantOfType(ASTConditionalExpression.class).jjtGetChild(0).getFirstDescendantOfType(ASTName.class).getImage();
}
final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
if (n != null) {
return n.getImage();
}
final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (ps != null) {
return ps.getImage();
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class ArrayIsStoredDirectlyRule method getExpressionVarName.
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;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class ArrayIsStoredDirectlyRule method checkForDirectAssignment.
/**
* 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)) {
// 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 = e.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;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class TccAttributeAccessCollector method getVariableName.
private String getVariableName(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (prefix.usesThisModifier()) {
List<ASTPrimarySuffix> suffixes = node.findChildrenOfType(ASTPrimarySuffix.class);
if (suffixes.size() > 1) {
if (!suffixes.get(1).isArguments()) {
// not a method call
return suffixes.get(0).getImage();
}
}
}
ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
String variableName = null;
if (name != null) {
int dotIndex = name.getImage().indexOf(".");
if (dotIndex == -1) {
variableName = name.getImage();
} else {
variableName = name.getImage().substring(0, dotIndex);
}
}
return variableName;
}
Aggregations