Skip to content
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

Drop the JavaKeywords class #1442

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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);

Check warning on line 41 in error-prone-utils/src/main/java/tech/picnic/errorprone/utils/SourceCode.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 41 without causing a test to fail

changed conditional boundary (covered by 17 tests ConditionalsBoundaryMutator)
}

/**
* 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