@Override public Object visit(ASTPrimaryExpression exp, Object data) { int n = exp.jjtGetNumChildren(); if (n < 4) { return data; } int first = getBadPrefixOrNull(exp, n); if (first == -1) { return data; } String second = getBadSuffixOrNull(exp, first + 2); if (second == null) { return data; } if (!(exp.jjtGetChild(first + 1) instanceof ASTPrimarySuffix)) { return data; } ASTPrimarySuffix methodCall = (ASTPrimarySuffix) exp.jjtGetChild(first + 1); if (!methodCall.isArguments() || methodCall.getArgumentCount() > 0) { return data; } addViolation(data, exp); return data; }
private void analyze(ASTPrimarySuffix suffix) { baseName = METHOD_CALL_CHAIN; methodName = suffix.getImage(); }
private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) { ASTClassOrInterfaceType type = null; ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class) .getFirstChildOfType(ASTName.class); if (methodName != null) { ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class); Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations(); for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) { if (e.getKey().getName().equals(methodName.getImage())) { type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class) .getFirstChildOfType(ASTResultType.class) .getFirstDescendantOfType(ASTClassOrInterfaceType.class); break; } } } return type; }
/** * Get the number of arguments for this primary suffix. One should call * {@link #isArguments()} to see if there are arguments. If this method is * called when there are no arguments it returns <code>-1</code>. * * @return A non-negative argument number when there are arguments, * <code>-1</code> otherwise. */ public int getArgumentCount() { if (!this.isArguments()) { return -1; } return ((ASTArguments) jjtGetChild(jjtGetNumChildren() - 1)).getArgumentCount(); }
private String getBadSuffixOrNull(ASTPrimaryExpression exp, int equalsPosition) { // verify PrimarySuffix[@Image='equals'] if (!(exp.jjtGetChild(equalsPosition) instanceof ASTPrimarySuffix)) { return null; } ASTPrimarySuffix suffix = (ASTPrimarySuffix) exp.jjtGetChild(equalsPosition); if (suffix.getImage() == null || !(suffix.hasImageEqualTo("equals") || suffix.hasImageEqualTo("equalsIgnoreCase"))) { return null; } return suffix.getImage(); }
@Override 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); }
ASTPrimarySuffix jjtn000 = new ASTPrimarySuffix(this, JJTPRIMARYSUFFIX); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1));Token t; try { if (jj_2_59(2)) { jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; jjtn000.jjtSetLastToken(getToken(0)); jjtn000.setIsArrayDereference(); break; case DOT: jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; jjtn000.jjtSetLastToken(getToken(0)); jjtn000.setImage(t.image); break; case LPAREN: jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; jjtn000.jjtSetLastToken(getToken(0)); jjtn000.setIsArguments(); break; default: if (jjtc000) {
@Override public Object visit(ASTPrimarySuffix node, Object data) { if (node.getImage() != null && node.getImage().endsWith("size")) { ASTClassOrInterfaceType type = getTypeOfPrimaryPrefix(node); if (type == null) { type = getTypeOfMethodCall(node); } if (type != null && CollectionUtil.isCollectionType(type.getType(), true)) { Node expr = node.jjtGetParent().jjtGetParent(); checkNodeAndReport(data, node, expr); } } return data; }
if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) { return super.visit(node, data); if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) { return super.visit(node, data); ASTPrimarySuffix r = rhsSuffixes.get(i); if (!l.hasImageEqualTo(r.getImage())) { return super.visit(node, data);
if (child2.getImage() == null && child2.jjtGetNumChildren() == 0) { thisIndex = x; break; Node child = node.jjtGetChild(x); ASTPrimarySuffix ps = (ASTPrimarySuffix) child; if (!ps.isArguments()) { String name = ((ASTPrimarySuffix) child).getImage(); if (x == i - 2) { // last node methodName = name; if (!child.isArguments()) { String name = child.getImage(); if (!child.isArguments()) { String name = child.getImage(); if (x == i - 2) { methodName = name;
private ASTClassOrInterfaceType getTypeOfPrimaryPrefix(ASTPrimarySuffix node) { return node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class) .getFirstDescendantOfType(ASTClassOrInterfaceType.class); } }
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; }
if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) { previousWasAppend = true; } else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) { previousWasAppend = false; ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class); if (literal != null && literal.isStringLiteral()) { result++;
private boolean isMethodCall(ASTPrimaryExpression node) { boolean result = false; List<ASTPrimarySuffix> suffixes = node.findDescendantsOfType(ASTPrimarySuffix.class); if (suffixes.size() == 1) { result = suffixes.get(0).isArguments(); } return result; }
private boolean occurenceIsArrayAccess(NameOccurrence occ, String arrayName) { if (occ.getLocation() instanceof ASTName) { ASTPrimarySuffix suffix = occ.getLocation().getFirstParentOfType(ASTPrimarySuffix.class); if (suffix == null || !suffix.isArrayDereference()) { return false; } return suffix.hasDescendantMatchingXPath("./Expression/PrimaryExpression[count(*)" + "=1]/PrimaryPrefix/Name[@Image='" + occ.getImage() + "']") && suffix.hasDescendantMatchingXPath("../PrimaryPrefix/Name[@Image='" + arrayName + "']") && !suffix.hasDescendantMatchingXPath("../../AssignmentOperator"); } return false; }
if (!primarySuffix.hasImageEqualTo(methodDeclarator.getImage())) { return super.visit(node, data); ASTArguments arguments = (ASTArguments) primarySuffix.jjtGetChild(0); ASTFormalParameters formalParameters = (ASTFormalParameters) methodDeclarator.jjtGetChild(0); if (formalParameters.jjtGetNumChildren() != arguments.jjtGetNumChildren()) {
continue; if (childName.endsWith(".toArray") && suffix.getArgumentCount() == 0 && primarySuffixs.size() == 1) { addViolationWithMessage(data, item,
/** * Determines the log level, that is used. It is either the called method name * itself or - in case java util logging is used, then it is the first argument of * the method call (if it exists). * * @param node the method call * @param methodCallName the called method name previously determined * @return the log level or <code>null</code> if it could not be determined */ private String getLogLevelName(Node node, String methodCallName) { if (!JAVA_UTIL_LOG_METHOD.equals(methodCallName) && !JAVA_UTIL_LOG_GUARD_METHOD.equals(methodCallName)) { return methodCallName; } String logLevel = null; ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class); if (suffix != null) { ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class); if (argumentList != null && argumentList.jjtGetNumChildren() > 0) { // at least one argument - the log level. If the method call is "log", then a message might follow ASTName name = GuardLogStatementRule.<ASTName>getFirstChild(argumentList.jjtGetChild(0), ASTPrimaryExpression.class, ASTPrimaryPrefix.class, ASTName.class); String lastPart = getLastPartOfName(name); lastPart = lastPart.toLowerCase(Locale.ROOT); if (!lastPart.isEmpty()) { logLevel = lastPart; } } } return logLevel; }
} else if (node instanceof ASTPrimarySuffix) { ASTPrimarySuffix primarySuffix = (ASTPrimarySuffix) node; if (primarySuffix.isArrayDereference()) { if (image == null) { image = "[";
if (pp != null && pp.usesThisModifier()) { final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class); if (ps.hasImageEqualTo(vn)) { addViolation(data, ret, vn);