Skip to content

Commit

Permalink
Drop the JavaKeywords class (#1442)
Browse files Browse the repository at this point in the history
Instead:
- Move its sole used method `#isValidIdentifier` to the `SourceCode`
  class.
- Delegate most of said method's implementation to
  `javax.lang.model.SourceVersion`.
  • Loading branch information
Stephan202 authored Dec 9, 2024
1 parent 75ca303 commit db18d6a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tech.picnic.errorprone.utils;

import static tech.picnic.errorprone.utils.JavaKeywords.isValidIdentifier;

import com.google.errorprone.VisitorState;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ImportTree;
Expand Down Expand Up @@ -47,7 +45,7 @@ public static Optional<String> findMethodRenameBlocker(
return Optional.of(String.format("`%s` is already statically imported", newName));
}

if (!isValidIdentifier(newName)) {
if (!SourceCode.isValidIdentifier(newName)) {
return Optional.of(String.format("`%s` is not a valid identifier", newName));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.Position;
import java.util.Optional;
import javax.lang.model.SourceVersion;

/**
* A collection of Error Prone utility methods for dealing with the source code representation of
Expand All @@ -28,6 +29,18 @@ public final class SourceCode {

private SourceCode() {}

/**
* 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.indexOf('.') < 0 && SourceVersion.isName(str);
}

/**
* Returns a string representation of the given {@link Tree}, preferring the original source code
* (if available) over its prettified representation.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.picnic.errorprone.utils;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
Expand All @@ -22,10 +24,41 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import java.util.Optional;
import java.util.stream.Stream;
import javax.lang.model.element.Name;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

final class SourceCodeTest {
private static Stream<Arguments> isValidIdentifierTestCases() {
/* { string, expected } */
return Stream.of(
arguments("", false),
arguments(".", false),
arguments("a.", false),
arguments(".a", false),
arguments("a.b", 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 string, boolean expected) {
assertThat(SourceCode.isValidIdentifier(string)).isEqualTo(expected);
}

@Test
void toStringConstantExpression() {
BugCheckerRefactoringTestHelper.newInstance(
Expand Down

0 comments on commit db18d6a

Please sign in to comment.