private String exceptionName(@Nullable Type type) { if (type == null || type.isUnknown()) { return "Exception"; } return "'" + type.name() + "'"; }
private String exceptionName(@Nullable Type type) { if (type == null || type.isUnknown()) { return "Exception"; } return "'" + type.name() + "'"; }
private boolean reportIssueOnDeprecatedType(Tree tree, Type type) { if (isDeprecatedType(type)) { reportIssue(tree, "Replace the synchronized class \"" + type.name() + "\" by an unsynchronized one such as " + REPLACEMENTS.get(type.fullyQualifiedName()) + "."); return true; } return false; }
@CheckForNull private static String returnStringFromJavaObject(Type argType) { if (argType.is("java.lang.Integer")) { return "Int"; } if (argType.is("java.lang.Double") || argType.is("java.lang.Long")) { return argType.name(); } return null; } }
private void checkForUselessUnboxing(Type targetType, Tree reportTree, ExpressionTree arg0) { Type argType = arg0.symbolType(); if (argType.is(targetType.fullyQualifiedName())) { reportIssue(reportTree, String.format("Remove the boxing to \"%s\"; The argument is already of the same type.", argType.name())); } }
@Override protected void onConstructorFound(NewClassTree newClassTree) { if(isBigIntegerPotentiallyBiggerThanLong(newClassTree)) { return; } reportIssue(newClassTree.identifier(), "Remove this \"" + newClassTree.symbolType().name() + "\" constructor"); }
@Override public void visitNode(Tree tree) { ExpressionTree expression = ((SynchronizedStatementTree) tree).expression(); Type expressionType = expression.symbolType(); if (isValueBasedType(expressionType)) { reportIssue(expression, String.format("Synchronize on a non-value-based object; synchronizing on a \"%s\" could lead to contention.%s", expressionType.name(), context.getJavaVersion().java8CompatibilityMessage())); } }
@Override public void visitNode(Tree tree) { AnnotationTree annotation = (AnnotationTree) tree; Type type = annotation.symbolType(); if (type.is("org.springframework.context.annotation.ComponentScan") || type.is("org.springframework.boot.autoconfigure.SpringBootApplication")) { reportIssue(annotation.annotationType(), "Consider replacing \"@" + type.name() + "\" by a list of beans imported with @Import to speed-up the start-up of the application."); } } }
private void checkCast(Type type, MethodInvocationTree mit) { if (type.isArray() && !type.is("java.lang.Object[]")) { Type elementType = ((Type.ArrayType) type).elementType(); if (!((JavaType) elementType).isTagged(JavaType.TYPEVAR)) { reportIssue(mit, "Pass \"new " + elementType.name() + "[0]\" as argument to \"toArray\"."); } } } }
@Override protected void onMethodFound(MethodInvocationTree mit) { ExpressionTree expressionTree = mit.arguments().get(0); // For now ignore everything that is not a .class expression if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) { Type symbolType = ((MemberSelectExpressionTree) expressionTree).expression().symbolType(); if (!symbolType.isUnknown() && isNotRuntimeAnnotation(symbolType)) { addIssue(mit, "\"@" + symbolType.name() + "\" is not available at runtime and cannot be seen with reflection."); } } }
@Override public void visitNode(Tree tree) { ThrowStatementTree throwStatementTree = (ThrowStatementTree) tree; Type symbolType = throwStatementTree.expression().symbolType(); if (symbolType.isSubtypeOf("java.lang.Exception") && !symbolType.isSubtypeOf("java.lang.RuntimeException")) { addIssue(tree, "Remove the usage of the checked exception '" + symbolType.name() + "'."); } }
@Override public void visitNode(Tree tree) { AnnotationTree annotation = (AnnotationTree) tree; Type type = annotation.symbolType(); if (type.is("org.springframework.context.annotation.ComponentScan") || type.is("org.springframework.boot.autoconfigure.SpringBootApplication")) { reportIssue(annotation.annotationType(), "Consider replacing \"@" + type.name() + "\" by a list of beans imported with @Import to speed-up the start-up of the application."); } } }
private void handleParameter(Symbol parameter, boolean springInjectionAnnotated) { Type leastSpecificType = findLeastSpecificType(parameter); if (parameter.type() != leastSpecificType && !leastSpecificType.is("java.lang.Object")) { String suggestedType = getSuggestedType(springInjectionAnnotated, leastSpecificType); reportIssue(parameter.declaration(), String.format("Use '%s' here; it is a more general type than '%s'.", suggestedType, parameter.type().name())); } }
@Override public void visitNode(Tree tree) { if (tree.is(Tree.Kind.METHOD)) { methods.push((MethodTree) tree); } else { ThrowStatementTree throwStatementTree = (ThrowStatementTree) tree; Type symbolType = throwStatementTree.expression().symbolType(); if (symbolType.isSubtypeOf("java.lang.Exception") && !symbolType.isSubtypeOf("java.lang.RuntimeException") && !isFromMethodOverride(symbolType)) { reportIssue(throwStatementTree.expression(), "Remove the usage of the checked exception '" + symbolType.name() + "'."); } } }
private void insertIssue(Tree tree) { Type type; if (tree.is(Tree.Kind.VARIABLE)) { type = ((VariableTree) tree).symbol().type(); } else { type = ((IdentifierTree) tree).symbol().type(); } check.addIssue(tree, "Close this \"" + type.name() + "\""); }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { Tree parent = mit.parent(); if (parent.is(Tree.Kind.EXPRESSION_STATEMENT) || (parent.is(Tree.Kind.VARIABLE) && ((VariableTree) parent).symbol().usages().isEmpty())) { reportIssue(parent, "Do something with the \"" + mit.symbolType().name() + "\" value returned by \"" + mit.symbol().name() + "\"."); } }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { Tree parent = mit.parent(); if (parent.is(Tree.Kind.EXPRESSION_STATEMENT) || (parent.is(Tree.Kind.VARIABLE) && ((VariableTree) parent).symbol().usages().isEmpty())) { reportIssue(parent, "Do something with the \"" + mit.symbolType().name() + "\" value returned by \"" + mit.symbol().name() + "\"."); } }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { ExpressionTree expressionTree = mit.arguments().get(0); // For now ignore everything that is not a .class expression if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) { Type symbolType = ((MemberSelectExpressionTree) expressionTree).expression().symbolType(); if (!symbolType.isUnknown() && isNotRuntimeAnnotation(symbolType)) { reportIssue(expressionTree, "\"@" + symbolType.name() + "\" is not available at runtime and cannot be seen with reflection."); } } }