-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve JUnitMethodDeclaration
check
#406
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,19 @@ | |
import com.google.common.collect.Sets; | ||
|
||
/** Utility class that can be used to identify reserved keywords of the Java language. */ | ||
// XXX: This class is no longer only about keywords. Consider changing its name and class-level | ||
// documentation. | ||
public final class JavaKeywords { | ||
/** | ||
* Enumeration of boolean and null literals. | ||
* | ||
* @see <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.10.3">JDK 17 | ||
* JLS section 3.10.3: Boolean Literals</a> | ||
* @see <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.10.8">JDK 17 | ||
* JLS section 3.10.8: The Null Literal</a> | ||
*/ | ||
private static final ImmutableSet<String> BOOLEAN_AND_NULL_LITERALS = | ||
ImmutableSet.of("true", "false", "null"); | ||
/** | ||
* List of all reserved keywords in the Java language. | ||
* | ||
|
@@ -64,7 +76,6 @@ public final class JavaKeywords { | |
"void", | ||
"volatile", | ||
"while"); | ||
|
||
/** | ||
* List of all contextual keywords in the Java language. | ||
* | ||
|
@@ -89,13 +100,28 @@ public final class JavaKeywords { | |
"var", | ||
"with", | ||
"yield"); | ||
|
||
/** List of all keywords in the Java language. */ | ||
private static final ImmutableSet<String> ALL_KEYWORDS = | ||
Sets.union(RESERVED_KEYWORDS, CONTEXTUAL_KEYWORDS).immutableCopy(); | ||
|
||
private JavaKeywords() {} | ||
|
||
/** | ||
* Tells whether the given string is a valid identifier in the Java language. | ||
* | ||
* @param str The string of interest. | ||
* @return {@code true} if the given string is a valid identifier in the Java language. | ||
* @see <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8">JDK 17 JLS | ||
* section 3.8: Identifiers</a> | ||
*/ | ||
public static boolean isValidIdentifier(String str) { | ||
return !str.isEmpty() | ||
&& !isReservedKeyword(str) | ||
&& !BOOLEAN_AND_NULL_LITERALS.contains(str) | ||
&& Character.isJavaIdentifierStart(str.codePointAt(0)) | ||
&& str.codePoints().skip(1).allMatch(Character::isUnicodeIdentifierPart); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pitest flags that mutants that remove |
||
} | ||
|
||
/** | ||
* Tells whether the given string is a reserved keyword in the Java language. | ||
* | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||
package tech.picnic.errorprone.bugpatterns.util; | ||||||||
|
||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||||||||
|
||||||||
import java.util.stream.Stream; | ||||||||
import org.junit.jupiter.params.ParameterizedTest; | ||||||||
import org.junit.jupiter.params.provider.Arguments; | ||||||||
import org.junit.jupiter.params.provider.MethodSource; | ||||||||
|
||||||||
final class JavaKeywordsTest { | ||||||||
private static Stream<Arguments> isValidIdentifierTestCases() { | ||||||||
/* { str, expected } */ | ||||||||
return Stream.of( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
arguments("", false), | ||||||||
arguments("public", false), | ||||||||
arguments("true", false), | ||||||||
arguments("false", false), | ||||||||
arguments("null", false), | ||||||||
arguments("0", false), | ||||||||
arguments("\0", false), | ||||||||
arguments("a%\0", false), | ||||||||
arguments("a", true), | ||||||||
arguments("a0", true), | ||||||||
arguments("_a0", true), | ||||||||
arguments("test", true)); | ||||||||
} | ||||||||
|
||||||||
@MethodSource("isValidIdentifierTestCases") | ||||||||
@ParameterizedTest | ||||||||
void isValidIdentifier(String str, boolean expected) { | ||||||||
assertThat(JavaKeywords.isValidIdentifier(str)).isEqualTo(expected); | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for this change? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More efficient storage and retrieval, just like what we do here.