3737/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
3838@ BugPattern (
3939 summary =
40- "Printing to standard output should only be used for debugging, not in production code" ,
40+ "Production code should not print to standard out or standard error. Standard out and"
41+ + " standard error should only be used for debugging." ,
4142 severity = WARNING ,
4243 tags = StandardTags .LIKELY_ERROR )
43- public class SystemOut extends BugChecker
44+ public final class SystemOut extends BugChecker
4445 implements MethodInvocationTreeMatcher , MemberSelectTreeMatcher {
4546
46- private static final Matcher <ExpressionTree > SYSTEM_OUT =
47+ private static final Matcher <ExpressionTree > BAD_FIELDS =
4748 anyOf (
4849 staticField (System .class .getName (), "out" ), //
4950 staticField (System .class .getName (), "err" ));
5051
51- private static final Matcher <ExpressionTree > PRINT_STACK_TRACE =
52+ private static final Matcher <ExpressionTree > BAD_METHODS =
5253 anyOf (
54+ staticMethod ().onClass ("java.lang.IO" ).namedAnyOf ("print" , "println" ),
5355 staticMethod ().onClass (Thread .class .getName ()).named ("dumpStack" ).withNoParameters (),
5456 instanceMethod ()
5557 .onDescendantOf (Throwable .class .getName ())
@@ -58,17 +60,11 @@ public class SystemOut extends BugChecker
5860
5961 @ Override
6062 public Description matchMemberSelect (MemberSelectTree tree , VisitorState state ) {
61- if (SYSTEM_OUT .matches (tree , state )) {
62- return describeMatch (tree );
63- }
64- return NO_MATCH ;
63+ return BAD_FIELDS .matches (tree , state ) ? describeMatch (tree ) : NO_MATCH ;
6564 }
6665
6766 @ Override
6867 public Description matchMethodInvocation (MethodInvocationTree tree , VisitorState state ) {
69- if (PRINT_STACK_TRACE .matches (tree , state )) {
70- return describeMatch (tree );
71- }
72- return NO_MATCH ;
68+ return BAD_METHODS .matches (tree , state ) ? describeMatch (tree ) : NO_MATCH ;
7369 }
7470}
0 commit comments