use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix in project pmd by pmd.
the class GuardLogStatementRule method visit.
@Override
public Object visit(ASTPrimaryExpression node, Object data) {
if (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node.jjtGetChild(0);
String methodCall = getMethodCallName(prefix);
String logLevel = getLogLevelName(node, methodCall);
if (guardStmtByLogLevel.containsKey(methodCall) && node.jjtGetChild(1) instanceof ASTPrimarySuffix && node.jjtGetChild(1).hasDescendantOfType(ASTAdditiveExpression.class)) {
if (!hasGuard(node, methodCall, logLevel)) {
super.addViolation(data, node);
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix in project pmd by pmd.
the class GuardLogStatementRule method hasGuard.
private boolean hasGuard(ASTPrimaryExpression node, String methodCall, String logLevel) {
ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
if (ifStatement == null) {
return false;
}
// an if statement always has an expression
ASTExpression expr = ifStatement.getFirstChildOfType(ASTExpression.class);
List<ASTPrimaryPrefix> guardCalls = expr.findDescendantsOfType(ASTPrimaryPrefix.class);
if (guardCalls.isEmpty()) {
return false;
}
boolean foundGuard = false;
// check all conditions in the if expression
for (ASTPrimaryPrefix guardCall : guardCalls) {
if (guardCall.jjtGetNumChildren() < 1 || guardCall.jjtGetChild(0).getImage() == null) {
continue;
}
String guardMethodCall = getLastPartOfName(guardCall.jjtGetChild(0).getImage());
boolean guardMethodCallMatches = guardStmtByLogLevel.get(methodCall).contains(guardMethodCall);
boolean hasArguments = guardCall.jjtGetParent().hasDescendantOfType(ASTArgumentList.class);
if (guardMethodCallMatches && !hasArguments) {
// simple case: guard method without arguments found
foundGuard = true;
} else if (guardMethodCallMatches && hasArguments) {
// java.util.logging: guard method with argument. Verify the log level
String guardArgLogLevel = getLogLevelName(guardCall.jjtGetParent(), guardMethodCall);
foundGuard = logLevel.equals(guardArgLogLevel);
}
if (foundGuard) {
break;
}
}
return foundGuard;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix 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.ASTPrimaryPrefix 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.ASTPrimaryPrefix 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