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

Switch order of String literals to prevent NullPointerException #3491

Merged
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
Expand Up @@ -59,7 +59,7 @@ static CloseablePath create(URI uri, FileSystemProvider fileSystemProvider) thro
return createForJarFileSystem(new URI(jarUri), fileSystem -> fileSystem.getPath(jarEntry),
fileSystemProvider);
}
if (uri.getScheme().equals(FILE_URI_SCHEME) && uri.getPath().endsWith(JAR_FILE_EXTENSION)) {
if (FILE_URI_SCHEME.equals(uri.getScheme()) && uri.getPath().endsWith(JAR_FILE_EXTENSION)) {
return createForJarFileSystem(new URI(JAR_URI_SCHEME + ':' + uri),
fileSystem -> fileSystem.getRootDirectories().iterator().next(), fileSystemProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final Optional<Segment> getRoot() {
* @see #forEngine(String)
*/
public final Optional<String> getEngineId() {
return getRoot().filter(segment -> segment.getType().equals(ENGINE_SEGMENT_TYPE)).map(Segment::getValue);
return getRoot().filter(segment -> ENGINE_SEGMENT_TYPE.equals(segment.getType())).map(Segment::getValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ private static boolean validateReservedIds(TestEngine testEngine) {
if (!engineId.startsWith("junit-")) {
return true;
}
if (engineId.equals("junit-jupiter")) {
if ("junit-jupiter".equals(engineId)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I understand the intention of this PR, this particular change cannot possibly avoid a NullPointerException, since the preceding if-statement has already invoked a method on engineId.

Instead of making changes like this one, I think we should rather focus on specifying that org.junit.platform.engine.TestEngine.getId() may never return null, and we can consider introducing a Preconditions.notNull(testEngine.getId()) check if we are concerned about that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pixeebot @ryandens Here's some feedback on the java/switch-literal-first codemod

validateWellKnownClassName(testEngine, "org.junit.jupiter.engine.JupiterTestEngine");
return true;
}
if (engineId.equals("junit-vintage")) {
if ("junit-vintage".equals(engineId)) {
validateWellKnownClassName(testEngine, "org.junit.vintage.engine.VintageTestEngine");
return true;
}
if (engineId.equals("junit-platform-suite")) {
if ("junit-platform-suite".equals(engineId)) {
validateWellKnownClassName(testEngine, "org.junit.platform.suite.engine.SuiteTestEngine");
return true;
}
Expand Down