@Override public void visitIdentifier(IdentifierTree tree) { if (!packageName.isEmpty()) { packageName += "."; } packageName += tree.name(); } }
private static String getArgName(ExpressionTree tree, int index) { if (tree.is(Kind.IDENTIFIER)) { return ((IdentifierTree) tree).name(); } return "Argument " + (index + 1); } }
private boolean is(ExpressionTree expression, String match) { if (expression.is(Tree.Kind.IDENTIFIER)) { String targetName = ((IdentifierTree) expression).name(); return match.equals(targetName); } return false; }
private static boolean matchingNames(ParametersList formalParameters, List<IdentifierTree> argumentsList) { List<String> argListNames = argumentsList.stream().filter(Objects::nonNull).map(arg -> arg.name().toLowerCase(Locale.ENGLISH)).collect(Collectors.toList()); return allUnique(argListNames) && argListNames.stream().allMatch(formalParameters::hasArgumentWithName); }
private String className() { String className = packageName; IdentifierTree identifierTree = classTrees.peek().simpleName(); if (identifierTree != null) { className += "." + identifierTree.name(); } return className; }
private static String fullQualifiedName(Tree tree) { if (tree.is(Tree.Kind.IDENTIFIER)) { return ((IdentifierTree) tree).name(); } else if (tree.is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree m = (MemberSelectExpressionTree) tree; return fullQualifiedName(m.expression()) + "." + m.identifier().name(); } throw new UnsupportedOperationException(String.format("Kind/Class '%s' not supported", tree.getClass())); } }
private static boolean isOverridingOrDesignedForExtension(MethodTree methodTree) { // we need to be sure that it's not an override return !Boolean.FALSE.equals(methodTree.isOverriding()) || SerializableContract.SERIALIZABLE_CONTRACT_METHODS.contains(methodTree.simpleName().name()) || isDesignedForExtension(methodTree); }
private static String attributeName(ExpressionTree expression) { if (expression.is(Tree.Kind.ASSIGNMENT)) { AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression; // assignment.variable() in annotation is always a Tree.Kind.IDENTIFIER return ((IdentifierTree) assignment.variable()).name(); } return "value"; }
@Override public void visitMemberSelectExpression(MemberSelectExpressionTree tree) { if (JavaKeyword.CLASS.getValue().equals(tree.identifier().name())) { // skip visit of class literal (MyType.class) return; } super.visitMemberSelectExpression(tree); }
private static boolean isCollection(Tree methodReturnType) { IdentifierTree identifierTree = null; if (methodReturnType.is(Tree.Kind.IDENTIFIER)) { identifierTree = (IdentifierTree) methodReturnType; } else if (methodReturnType.is(Tree.Kind.MEMBER_SELECT)) { identifierTree = ((MemberSelectExpressionTree) methodReturnType).identifier(); } return identifierTree != null && COLLECTION_TYPES.contains(identifierTree.name()); } }
private void checkPositiveIncrement(Tree tree, ForLoopIncrement loopIncrement) { if (loopIncrement.value() < 0) { reportIssue(tree, String.format("\"%s\" is decremented and will never reach \"stop condition\".", loopIncrement.identifier().name())); } }
private void onMemberSelectExpressionFound(IdentifierTree identifierTree) { Symbol symbol = identifierTree.symbol(); if (symbol.isVariableSymbol() && symbol.owner().type().is("com.google.common.base.Charsets")) { String identifier = identifierTree.name(); String aliasedIdentigier = identifier.replace("_", "-"); if (STANDARD_CHARSETS.stream().anyMatch(c -> c.name().equals(aliasedIdentigier))) { reportIssue(identifierTree, "Replace \"com.google.common.base.Charsets." + identifier + "\" with \"StandardCharsets." + identifier + "\"."); } } }
private static boolean isFinalize(MethodTree methodTree) { if ("finalize".equals(methodTree.simpleName().name())) { Tree returnType = methodTree.returnType(); if (returnType != null && returnType.is(Tree.Kind.PRIMITIVE_TYPE)) { return "void".equals(((PrimitiveTypeTree) returnType).keyword().text()); } } return false; } }
private static boolean isSecurityAuthenticationConstant(ExpressionTree tree) { if (tree.is(Kind.MEMBER_SELECT)) { MemberSelectExpressionTree constantExpression = (MemberSelectExpressionTree) tree; return "javax.naming.Context".equals(constantExpression.expression().symbolType().fullyQualifiedName()) && "SECURITY_AUTHENTICATION".equals(constantExpression.identifier().name()); } return LiteralUtils.hasValue(tree, "java.naming.security.authentication"); } }
@Override public void visitClass(ClassTree tree) { if (tree.is(Tree.Kind.CLASS) && tree.simpleName() != null && !pattern.matcher(tree.simpleName().name()).matches()) { context.reportIssue(this, tree.simpleName(), "Rename this class name to match the regular expression '" + format + "'."); } super.visitClass(tree); }
private static boolean isFileSeparator(ExpressionTree firstArg) { if (firstArg.is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree mse = (MemberSelectExpressionTree) firstArg; return "separator".equals(mse.identifier().name()) && mse.expression().symbolType().is("java.io.File"); } return false; }
private static String getAnnotationName(ExpressionTree initializer) { String result = ""; if (initializer.is(Tree.Kind.ANNOTATION)) { Tree annotationType = ((AnnotationTree) initializer).annotationType(); if (annotationType.is(Tree.Kind.IDENTIFIER)) { result = ((IdentifierTree) annotationType).name(); } else if (annotationType.is(Tree.Kind.MEMBER_SELECT)) { result = fullName((MemberSelectExpressionTree) annotationType); } } return result; }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { Symbol symbol = tree.symbol(); if (tree.methodSelect().is(Tree.Kind.IDENTIFIER) && isCallToSuperclassMethod(symbol)) { String methodName = ((IdentifierTree) tree.methodSelect()).name(); reportIssue(ExpressionUtils.methodName(tree), "Prefix this call to \"" + methodName + "\" with \"super.\"."); } super.visitMethodInvocation(tree); }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect(); boolean issue = ("equals".equals(memberSelect.identifier().name())) && (isToUpperCaseOrToLowerCase(memberSelect.expression()) || (tree.arguments().size() == 1 && isToUpperCaseOrToLowerCase(tree.arguments().get(0)))); if (issue) { context.reportIssue(this, tree, "Replace these toUpperCase()/toLowerCase() and equals() calls with a single equalsIgnoreCase() call."); } } super.visitMethodInvocation(tree); }