|
| 1 | +/* |
| 2 | + * Copyright Starburst Data, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF STARBURST DATA. |
| 5 | + * The copyright notice above does not evidence any |
| 6 | + * actual or intended publication of such source code. |
| 7 | + * |
| 8 | + * Redistribution of this material is strictly prohibited. |
| 9 | + */ |
| 10 | +package io.starburst.errorprone; |
| 11 | + |
| 12 | +import com.google.auto.service.AutoService; |
| 13 | +import com.google.errorprone.BugPattern; |
| 14 | +import com.google.errorprone.VisitorState; |
| 15 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 16 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 17 | +import com.google.errorprone.fixes.SuggestedFix; |
| 18 | +import com.google.errorprone.matchers.Description; |
| 19 | +import com.google.errorprone.matchers.Matcher; |
| 20 | +import com.sun.source.tree.ExpressionTree; |
| 21 | +import com.sun.source.tree.LiteralTree; |
| 22 | +import com.sun.source.tree.MethodInvocationTree; |
| 23 | +import com.sun.source.tree.MethodTree; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 30 | +import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; |
| 31 | +import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 32 | +import static com.sun.source.tree.Tree.Kind.IDENTIFIER; |
| 33 | +import static com.sun.source.tree.Tree.Kind.STRING_LITERAL; |
| 34 | +import static java.lang.String.format; |
| 35 | +import static java.util.Objects.requireNonNull; |
| 36 | + |
| 37 | +@AutoService(BugChecker.class) |
| 38 | +@BugPattern( |
| 39 | + name = "RequireNonNullMessage", |
| 40 | + summary = "An error message provided to Objects#requireNonNull is incorrect", |
| 41 | + explanation = """ |
| 42 | + The error message passed as the second argument to Objects#requireNonNull \ |
| 43 | + should make it immediately obvious which value is null; the check will trigger when \ |
| 44 | + an identifier is passes as the first argument to Objects#requireNonNull, \ |
| 45 | + but the error message passes as the second argument does not mention its name.""", |
| 46 | + linkType = BugPattern.LinkType.NONE, |
| 47 | + severity = BugPattern.SeverityLevel.WARNING) |
| 48 | +public class RequireNonNullMessageChecker |
| 49 | + extends BugChecker |
| 50 | + implements MethodInvocationTreeMatcher |
| 51 | +{ |
| 52 | + private static final Pattern MESSAGE_PATTERN = Pattern.compile(" (?:(?:is|are|was|were) (?:null|empty|missing|none)|(?:is|are) required|(?:must not|cannot|can't) be (?:null|empty))"); |
| 53 | + private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("^\\p{javaUnicodeIdentifierStart}\\p{javaUnicodeIdentifierPart}*\\b"); |
| 54 | + |
| 55 | + private static final Matcher<ExpressionTree> requireNonNull = staticMethod() |
| 56 | + .onClass(Objects.class.getName()) |
| 57 | + .named("requireNonNull"); |
| 58 | + |
| 59 | + @Override |
| 60 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) |
| 61 | + { |
| 62 | + if (!requireNonNull.matches(tree, state)) { |
| 63 | + return NO_MATCH; |
| 64 | + } |
| 65 | + List<? extends ExpressionTree> arguments = tree.getArguments(); |
| 66 | + if (arguments.isEmpty()) { |
| 67 | + // weird |
| 68 | + return NO_MATCH; |
| 69 | + } |
| 70 | + |
| 71 | + // no error message at all: |
| 72 | + if (arguments.size() < 2) { |
| 73 | + // this is considered fine: it's probably just a sanity check |
| 74 | + // (we could enable it at some point, but let's just focus on typos for now) |
| 75 | + return NO_MATCH; |
| 76 | + } |
| 77 | + |
| 78 | + // the first argument: identifier or an expression? |
| 79 | + ExpressionTree objectArgument = arguments.get(0); |
| 80 | + if (objectArgument.getKind() != IDENTIFIER) { |
| 81 | + // we don't want to deal with complex expressions; we only want to detect typos in simple cases |
| 82 | + return NO_MATCH; |
| 83 | + } |
| 84 | + String objectArgumentIdentifier = requireNonNull(state.getSourceForNode(objectArgument)); |
| 85 | + |
| 86 | + // inspect the message |
| 87 | + ExpressionTree messageArgument = arguments.get(1); |
| 88 | + if (messageArgument.getKind() != STRING_LITERAL) { |
| 89 | + // something else than a literal: ignore, since we can't inspect it |
| 90 | + // TODO: suggest using message supplier |
| 91 | + return NO_MATCH; |
| 92 | + } |
| 93 | + String messageLiteral = (String) ((LiteralTree) messageArgument).getValue(); |
| 94 | + |
| 95 | + // we will first see if we can recognize the error message as "X is null" kind of thing, then proceed accordingly |
| 96 | + // note: there's no end-of-string anchor in the pattern, so we allow extra information at the end of the message |
| 97 | + // (and it starts with a space instead of the start-of-string anchor!) |
| 98 | + java.util.regex.Matcher patternMatch = MESSAGE_PATTERN.matcher(messageLiteral); |
| 99 | + if (!patternMatch.find()) { |
| 100 | + // the message does not fit the pattern - must be some free-form text which we won't try to interpret |
| 101 | + // checks in constructors are a special case, though, so require that the message is equal to the identifier's name |
| 102 | + if (isWithinConstructor(state) && !messageLiteral.equals(objectArgumentIdentifier)) { |
| 103 | + return describeMatch(tree, (LiteralTree) messageArgument, objectArgumentIdentifier, " is null", ""); |
| 104 | + } |
| 105 | + |
| 106 | + return NO_MATCH; |
| 107 | + } |
| 108 | + |
| 109 | + // now that we know it's "X is null" kind of thing, let's interpret the "X" part |
| 110 | + // note: the pattern ends with a word-break, so we won't get a positive match |
| 111 | + // if only part of the beginning is recognizable as an identifier |
| 112 | + java.util.regex.Matcher identifierMatch = IDENTIFIER_PATTERN.matcher(messageLiteral); |
| 113 | + if (!identifierMatch.find()) { |
| 114 | + // the thing before the pattern is not an identifier at all: ignore, but not in constructors |
| 115 | + if (isWithinConstructor(state)) { |
| 116 | + return describeMatch(tree, (LiteralTree) messageArgument, objectArgumentIdentifier, patternMatch.group(), messageLiteral.substring(patternMatch.end())); |
| 117 | + } |
| 118 | + |
| 119 | + return NO_MATCH; |
| 120 | + } |
| 121 | + |
| 122 | + if (!identifierMatch.group().equals(objectArgumentIdentifier)) { |
| 123 | + // we matched the identifier at the beginning of the message, and it's not equal to the identifier |
| 124 | + // passed as the first argument: report a match |
| 125 | + // note: the thing before the pattern may be an identifier and some extra stuff |
| 126 | + // (e.g. `requireNonNull(parameter, "parameter somehow is null")` is fine) |
| 127 | + return describeMatch(tree, (LiteralTree) messageArgument, objectArgumentIdentifier, patternMatch.group(), messageLiteral.substring(patternMatch.end())); |
| 128 | + } |
| 129 | + |
| 130 | + // not a typo: no error |
| 131 | + return NO_MATCH; |
| 132 | + } |
| 133 | + |
| 134 | + private Description describeMatch(MethodInvocationTree tree, LiteralTree messageArgument, String objectArgumentIdentifier, String pattern, String suffix) |
| 135 | + { |
| 136 | + return describeMatch( |
| 137 | + tree, |
| 138 | + SuggestedFix.replace( |
| 139 | + messageArgument, |
| 140 | + format("\"%s%s%s\"", objectArgumentIdentifier, pattern, suffix))); |
| 141 | + } |
| 142 | + |
| 143 | + private boolean isWithinConstructor(VisitorState state) |
| 144 | + { |
| 145 | + MethodTree enclosingMethodTree = state.findEnclosing(MethodTree.class); |
| 146 | + return enclosingMethodTree != null && getSymbol(enclosingMethodTree).isConstructor(); |
| 147 | + } |
| 148 | +} |
0 commit comments