Leverage basic Java 17 features (#451)

- Refactor code to use the following basic Java 17 features:
  - pattern matching for instanceof
  - @Serial annotation
  - switch expressions
  - enhanced switch statements
  - StringBuilder.isEmpty()
- Replace two switch statements with simpler if statements.
- Rename a few local variables.
This commit is contained in:
translatenix
2024-04-25 12:52:28 -07:00
committed by GitHub
parent 3ab9e4184e
commit a7c7e51180
142 changed files with 1333 additions and 1798 deletions

View File

@@ -458,8 +458,8 @@ public class PklPlugin implements Plugin<Project> {
private Optional<SourceDirectorySet> getKotlinSourceDirectorySet(SourceSet sourceSet) {
// First, try loading it as an extension - 1.8+ version of Kotlin plugin does this.
var kotlinExtension = sourceSet.getExtensions().findByName("kotlin");
if (kotlinExtension instanceof SourceDirectorySet) {
return Optional.of((SourceDirectorySet) kotlinExtension);
if (kotlinExtension instanceof SourceDirectorySet sourceDirSet) {
return Optional.of(sourceDirSet);
}
// Otherwise, try to load it as a convention. First, we attempt to get the convention
@@ -476,8 +476,8 @@ public class PklPlugin implements Plugin<Project> {
try {
var getConventionMethod = sourceSet.getClass().getMethod("getConvention");
var convention = getConventionMethod.invoke(sourceSet);
if (convention instanceof Convention) {
var kotlinSourceSet = ((Convention) convention).getPlugins().get("kotlin");
if (convention instanceof Convention c) {
var kotlinSourceSet = c.getPlugins().get("kotlin");
if (kotlinSourceSet == null) {
project
.getLogger()
@@ -490,8 +490,8 @@ public class PklPlugin implements Plugin<Project> {
var getKotlinMethod = kotlinSourceSet.getClass().getMethod("getKotlin");
var kotlinSourceDirectorySet = getKotlinMethod.invoke(kotlinSourceSet);
if (kotlinSourceDirectorySet instanceof SourceDirectorySet) {
return Optional.of((SourceDirectorySet) kotlinSourceDirectorySet);
if (kotlinSourceDirectorySet instanceof SourceDirectorySet sourceDirSet) {
return Optional.of(sourceDirSet);
}
project

View File

@@ -78,10 +78,9 @@ public abstract class BasePklTask extends DefaultTask {
return getParsedSettingsModule()
.map(
it -> {
if (it instanceof File) {
return (File) it;
if (it instanceof File file) {
return file;
}
//noinspection DataFlowIssue
return null;
});
}
@@ -92,10 +91,9 @@ public abstract class BasePklTask extends DefaultTask {
return getParsedSettingsModule()
.map(
it -> {
if (it instanceof URI) {
return (URI) it;
if (it instanceof URI uri) {
return uri;
}
//noinspection DataFlowIssue
return null;
});
}
@@ -201,29 +199,28 @@ public abstract class BasePklTask extends DefaultTask {
* @throws InvalidUserDataException In case the input is none of the types described above, or
* when the underlying value cannot be parsed correctly.
*/
protected Object parseModuleNotation(Object m) {
if (m instanceof URI) {
var u = (URI) m;
if ("file".equals(u.getScheme())) {
return new File(u.getPath());
protected Object parseModuleNotation(Object notation) {
if (notation instanceof URI uri) {
if ("file".equals(uri.getScheme())) {
return new File(uri.getPath());
}
return u;
} else if (m instanceof File) {
return m;
} else if (m instanceof Path) {
return uri;
} else if (notation instanceof File) {
return notation;
} else if (notation instanceof Path path) {
try {
return ((Path) m).toFile();
return path.toFile();
} catch (UnsupportedOperationException e) {
throw new InvalidUserDataException("Failed to parse Pkl module file path: " + m, e);
throw new InvalidUserDataException("Failed to parse Pkl module file path: " + notation, e);
}
} else if (m instanceof URL) {
} else if (notation instanceof URL url) {
try {
return parseModuleNotation(((URL) m).toURI());
return parseModuleNotation(url.toURI());
} catch (URISyntaxException e) {
throw new InvalidUserDataException("Failed to parse Pkl module URI: " + m, e);
throw new InvalidUserDataException("Failed to parse Pkl module URI: " + notation, e);
}
} else if (m instanceof CharSequence) {
var s = m.toString();
} else if (notation instanceof CharSequence) {
var s = notation.toString();
if (IoUtils.isUriLike(s)) {
try {
return parseModuleNotation(IoUtils.toUri(s));
@@ -237,11 +234,14 @@ public abstract class BasePklTask extends DefaultTask {
throw new InvalidUserDataException("Failed to parse Pkl module file path: " + s, e);
}
}
} else if (m instanceof FileSystemLocation) {
return ((FileSystemLocation) m).getAsFile();
} else if (notation instanceof FileSystemLocation location) {
return location.getAsFile();
} else {
throw new InvalidUserDataException(
"Unsupported value of type " + m.getClass() + " used as a module path: " + m);
"Unsupported value of type "
+ notation.getClass()
+ " used as a module path: "
+ notation);
}
}
@@ -255,14 +255,13 @@ public abstract class BasePklTask extends DefaultTask {
* IoUtils#createUri(String)} because other ways of conversion can make relative paths into
* absolute URIs, which may break module loading.
*/
private URI parsedModuleNotationToUri(Object m) {
if (m instanceof File) {
var f = (File) m;
return IoUtils.createUri(f.getPath());
} else if (m instanceof URI) {
return (URI) m;
private URI parsedModuleNotationToUri(Object notation) {
if (notation instanceof File file) {
return IoUtils.createUri(file.getPath());
} else if (notation instanceof URI uri) {
return uri;
}
throw new IllegalArgumentException("Invalid parsed module notation: " + m);
throw new IllegalArgumentException("Invalid parsed module notation: " + notation);
}
protected List<Pattern> patternsFromStrings(List<String> patterns) {

View File

@@ -118,10 +118,10 @@ public abstract class ModulesTask extends BasePklTask {
var uris = new ArrayList<URI>();
for (var m : modules) {
var parsed = parseModuleNotation(m);
if (parsed instanceof File) {
files.add((File) parsed);
} else if (parsed instanceof URI) {
uris.add((URI) parsed);
if (parsed instanceof File file) {
files.add(file);
} else if (parsed instanceof URI uri) {
uris.add(uri);
}
}
return Pair.of(files, uris);
@@ -132,14 +132,13 @@ public abstract class ModulesTask extends BasePklTask {
* IoUtils#createUri(String)} because other ways of conversion can make relative paths into
* absolute URIs, which may break module loading.
*/
private URI parsedModuleNotationToUri(Object m) {
if (m instanceof File) {
var f = (File) m;
return IoUtils.createUri(f.getPath());
} else if (m instanceof URI) {
return (URI) m;
private URI parsedModuleNotationToUri(Object notation) {
if (notation instanceof File file) {
return IoUtils.createUri(file.getPath());
} else if (notation instanceof URI uri) {
return uri;
}
throw new IllegalArgumentException("Invalid parsed module notation: " + m);
throw new IllegalArgumentException("Invalid parsed module notation: " + notation);
}
protected URI parseModuleNotationToUri(Object m) {