use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class MethodTypeResolution method selectMethodsThirdPhase.
/**
* Look for methods considering varargs as well.
* https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.4
*/
public static List<MethodType> selectMethodsThirdPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
// TODO: check if explicit type arguments are applicable to the type parameter bounds
List<MethodType> selectedMethods = new ArrayList<>();
for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
MethodType methodType = methodsToSearch.get(methodIndex);
if (!methodType.isParameterized()) {
throw new ResolutionFailedException();
}
// if we reach here and the method is not a vararg, then we didn't find a resolution in earlier phases
if (methodType.isVararg()) {
// check subtypeability of each argument to the corresponding parameter
boolean methodIsApplicable = true;
List<JavaTypeDefinition> methodParameters = methodType.getParameterTypes();
JavaTypeDefinition varargComponentType = methodType.getVarargComponentType();
if (argList == null) {
// There are no arguments, make sure the method has only a vararg
methodIsApplicable = getArity(methodType.getMethod()) == 1;
} else {
// try each arguments if it's method convertible
for (int argIndex = 0; argIndex < argList.jjtGetNumChildren(); ++argIndex) {
JavaTypeDefinition parameterType = argIndex < methodParameters.size() - 1 ? methodParameters.get(argIndex) : varargComponentType;
if (!isMethodConvertible(parameterType, (ASTExpression) argList.jjtGetChild(argIndex))) {
methodIsApplicable = false;
break;
}
// TODO: If k != n, or if k = n and An cannot be converted by method invocation conversion to
// Sn[], then the type which is the erasure (ยง4.6) of Sn is accessible at the point of invocation.
// TODO: add unchecked conversion in an else if branch
}
}
if (methodIsApplicable) {
selectedMethods.add(methodType);
}
} else {
// TODO: Remove check for vararg here, once we can detect and use return types of method calls
LOG.log(Level.FINE, "Method {0} couldn't be resolved", String.valueOf(methodType));
}
}
return selectedMethods;
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class JavaNameOccurrence method isSelfAssignment.
/**
* Assert it the occurrence is a self assignment such as:
* <code>i += 3;</code>
*
* @return true, if the occurrence is self-assignment, false, otherwise.
*/
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
Node l = location;
while (true) {
Node p = l.jjtGetParent();
Node gp = p.jjtGetParent();
Node node = gp.jjtGetParent();
if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
return true;
}
if (hasAssignmentOperator(gp)) {
return isCompoundAssignment(gp);
}
if (hasAssignmentOperator(node)) {
return isCompoundAssignment(node);
}
// deal with extra parenthesis: "(i)++"
if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
l = node;
continue;
}
// catch this.i++ or ++this.i
return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class BrokenNullCheckRule method visit.
@Override
public Object visit(ASTIfStatement node, Object data) {
ASTExpression expression = (ASTExpression) node.jjtGetChild(0);
ASTConditionalAndExpression conditionalAndExpression = expression.getFirstDescendantOfType(ASTConditionalAndExpression.class);
if (conditionalAndExpression != null) {
checkForViolations(node, data, conditionalAndExpression);
}
ASTConditionalOrExpression conditionalOrExpression = expression.getFirstDescendantOfType(ASTConditionalOrExpression.class);
if (conditionalOrExpression != null) {
checkForViolations(node, data, conditionalOrExpression);
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression 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.ASTExpression 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);
}
Aggregations