use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class InvalidSlf4jMessageFormatRule method visit.
@Override
public Object visit(final ASTName node, final Object data) {
final NameDeclaration nameDeclaration = node.getNameDeclaration();
// ignore imports or methods
if (!(nameDeclaration instanceof VariableNameDeclaration)) {
return super.visit(node, data);
}
// ignore non slf4j logger
Class<?> type = ((VariableNameDeclaration) nameDeclaration).getType();
if (type == null || !type.getName().equals(LOGGER_CLASS)) {
return super.visit(node, data);
}
// get the node that contains the logger
final ASTPrimaryExpression parentNode = node.getFirstParentOfType(ASTPrimaryExpression.class);
// get the log level
final String method = parentNode.getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class).getImage().replace(nameDeclaration.getImage() + ".", "");
// ignore if not a log level
if (!LOGGER_LEVELS.contains(method)) {
return super.visit(node, data);
}
// find the arguments
final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class).getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
// remove the message parameter
final ASTPrimaryExpression messageParam = argumentList.remove(0).getFirstDescendantOfType(ASTPrimaryExpression.class);
final int expectedArguments = expectedArguments(messageParam);
if (expectedArguments == 0) {
// or if we couldn't analyze the message parameter
return super.visit(node, data);
}
// But only, if it is not used as a placeholder argument
if (argumentList.size() > expectedArguments) {
removeThrowableParam(argumentList);
}
if (argumentList.size() < expectedArguments) {
addViolationWithMessage(data, node, "Missing arguments," + getExpectedMessage(argumentList, expectedArguments));
} else if (argumentList.size() > expectedArguments) {
addViolationWithMessage(data, node, "Too many arguments," + getExpectedMessage(argumentList, expectedArguments));
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class InvalidSlf4jMessageFormatRule method removeThrowableParam.
private void removeThrowableParam(final List<ASTExpression> params) {
// Throwable parameters are the last one in the list, if any.
if (params.isEmpty()) {
return;
}
int lastIndex = params.size() - 1;
ASTPrimaryExpression last = params.get(lastIndex).getFirstDescendantOfType(ASTPrimaryExpression.class);
if (isNewThrowable(last) || hasTypeThrowable(last) || isReferencingThrowable(last)) {
params.remove(lastIndex);
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class PreserveStackTraceRule method visit.
@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
// Inspect all the throw stmt inside the catch stmt
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
continue;
}
// Retrieve all argument for the throw exception (to see if the
// original exception is preserved)
ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
Node parent = args.jjtGetParent().jjtGetParent();
if (parent instanceof ASTAllocationExpression) {
// maybe it is used inside a anonymous class
ck(data, target, throwStatement, parent);
} else {
// Check all arguments used in the throw statement
ck(data, target, throwStatement, throwStatement);
}
} else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null) {
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()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> occurrences = entry.getValue();
if (decl.getImage().equals(child.getImage())) {
if (!isInitCauseCalled(target, occurrences)) {
// Check how the variable is initialized
ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
// constructor with args?
ck(data, target, throwStatement, args);
} else if (!isFillInStackTraceCalled(target, initializer)) {
addViolation(data, throwStatement);
}
}
}
}
}
} else if (child instanceof ASTClassOrInterfaceType) {
addViolation(data, throwStatement);
}
}
}
}
return super.visit(catchStmt, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class ConfusingTernaryRule method isParenthesisAroundMatch.
private static boolean isParenthesisAroundMatch(Node node) {
// look for "(match)"
if (!(node instanceof ASTPrimaryExpression) || node.jjtGetNumChildren() != 1) {
return false;
}
Node inode = node.jjtGetChild(0);
if (!(inode instanceof ASTPrimaryPrefix) || inode.jjtGetNumChildren() != 1) {
return false;
}
Node jnode = inode.jjtGetChild(0);
if (!(jnode instanceof ASTExpression) || jnode.jjtGetNumChildren() != 1) {
return false;
}
Node knode = jnode.jjtGetChild(0);
// recurse!
return isMatch(knode);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class LawOfDemeterRule method visit.
/**
* That's a new method. We are going to check each method call inside the
* method.
*
* @return <code>null</code>.
*/
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
List<ASTPrimaryExpression> primaryExpressions = node.findDescendantsOfType(ASTPrimaryExpression.class);
for (ASTPrimaryExpression expression : primaryExpressions) {
List<MethodCall> calls = MethodCall.createMethodCalls(expression);
addViolations(calls, (RuleContext) data);
}
return null;
}
Aggregations