Skip to content

Commit

Permalink
Applied rewrite-plugin Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Aug 14, 2023
1 parent 02840f2 commit ab54915
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
20 changes: 10 additions & 10 deletions src/main/java/de/cuioss/tools/codec/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,14 @@ public byte[] decode(final ByteBuffer buffer) throws DecoderException {
* @see #decodeHex(char[])
*/
public Object decode(final Object object) throws DecoderException {
if (object instanceof String) {
return decode(((String) object).toCharArray());
if (object instanceof String string) {
return decode(string.toCharArray());
}
if (object instanceof byte[]) {
return decode((byte[]) object);
if (object instanceof byte[] bytes) {
return decode(bytes);
}
if (object instanceof ByteBuffer) {
return decode((ByteBuffer) object);
if (object instanceof ByteBuffer buffer) {
return decode(buffer);
}
try {
return decodeHex((char[]) object);
Expand Down Expand Up @@ -508,10 +508,10 @@ public byte[] encode(final ByteBuffer array) {
*/
public Object encode(final Object object) throws EncoderException {
byte[] byteArray;
if (object instanceof String) {
byteArray = ((String) object).getBytes(getCharset());
} else if (object instanceof ByteBuffer) {
byteArray = toByteArray((ByteBuffer) object);
if (object instanceof String string) {
byteArray = string.getBytes(getCharset());
} else if (object instanceof ByteBuffer buffer) {
byteArray = toByteArray(buffer);
} else {
try {
byteArray = (byte[]) object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private String getJoined(final List<String> values) {
private String format(final List<String> values) {
final var joined = getJoined(values);
if (null != joined) {
return String.format("(%s)", joined);
return "(%s)".formatted(joined);
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/cuioss/tools/io/FilenameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,10 @@ private static void failIfNullBytePresent(final String path) {
final var len = path.length();
for (var i = 0; i < len; i++) {
if (path.charAt(i) == 0) {
throw new IllegalArgumentException("Null byte present in file/path name. There are no "
+ "known legitimate use cases for such data, but several injection attacks may use it");
throw new IllegalArgumentException("""
Null byte present in file/path name. There are no \
known legitimate use cases for such data, but several injection attacks may use it\
""");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/cuioss/tools/io/IOStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static String toString(final InputStream input, final Charset encoding) t
* @throws NullPointerException if the input parameter is null
*/
public static BufferedReader toBufferedReader(final Reader reader) {
return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
return reader instanceof BufferedReader br ? br : new BufferedReader(reader);
}

// copy from InputStream
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/de/cuioss/tools/io/MorePaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ public static boolean checkExecutablePath(final @NonNull Path path, final boolea
public static Path getBackupDirectoryForPath(final Path directory) {
if (!checkAccessiblePath(directory, true, true)) {
throw new IllegalArgumentException(
String.format("Given path '%s' does not denote an existing writable directory",
"Given path '%s' does not denote an existing writable directory".formatted(
directory.toFile().getAbsolutePath()));
}
final var backup = directory.resolve(BACKUP_DIR_NAME);
final var backupAsFile = backup.toFile();
if (!backupAsFile.exists() && !backupAsFile.mkdir()) {
throw new IllegalStateException(
String.format("Unable to create directory '%s'", backup.toFile().getAbsolutePath()));
"Unable to create directory '%s'".formatted(backup.toFile().getAbsolutePath()));
}
return backup;

Expand Down Expand Up @@ -286,8 +286,8 @@ public static Path copyToTempLocation(final Path path) throws IOException {
public static void assertAccessibleFile(final Path path) {
requireNonNull(path, "path");
if (!checkAccessiblePath(path, false, true)) {
throw new IllegalArgumentException(String.format(
"Given path '%s' does not denote an existing readable file", path.toFile().getAbsolutePath()));
throw new IllegalArgumentException(
"Given path '%s' does not denote an existing readable file".formatted(path.toFile().getAbsolutePath()));
}
}

Expand All @@ -305,7 +305,7 @@ static Path createNonExistingPath(final Path parentDir, final String fileName) {
}
}
throw new IllegalStateException(
String.format("Unable to determine a non-existing file within '%s' for file-name '%s'",
"Unable to determine a non-existing file within '%s' for file-name '%s'".formatted(
parentDir.toFile().getAbsolutePath(), fileName));
}

Expand Down Expand Up @@ -486,7 +486,10 @@ public static boolean isSameFile(Path path, Path path2) {
if (null != path && null != path2) {
if (!path.toFile().exists() || !path2.toFile().exists()) {
log.debug(
"Comparing paths with #equals, as at least one path does not exist. " + "path_a={}, path_b={}",
"""
Comparing paths with #equals, as at least one path does not exist. \
path_a={}, path_b={}\
""",
path, path2);
return path.equals(path2);
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/de/cuioss/tools/reflect/MoreReflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,18 @@ public static <T> Class<T> extractFirstGenericTypeArgument(final Class<?> typeTo
* type represents already a {@link Class} it will be returned directly.
* Otherwise, the super-type will be checked by calling the superclass
*/
@SuppressWarnings("rawtypes")
public static Optional<Class<?>> extractGenericTypeCovariantly(final Type type) {
if (null == type) {
log.trace("No KeyStoreType given, returning empty");
return Optional.empty();
}
if (type instanceof Class) {
if (type instanceof Class class1) {
log.debug("Found actual class returning as result {}", type);
return Optional.of((Class) type);
return Optional.of(class1);
}
if (type instanceof ParameterizedType) {
if (type instanceof ParameterizedType parameterizedType) {
log.debug("found Parameterized type, for {}, calling recursively", type);
return extractGenericTypeCovariantly(((ParameterizedType) type).getRawType());
return extractGenericTypeCovariantly(parameterizedType.getRawType());
}
log.warn("Unable to determines generic-type for {}", type);
return Optional.empty();
Expand All @@ -466,8 +465,8 @@ public static Optional<ParameterizedType> extractParameterizedType(final Class<?
return Optional.empty();
}
final var genericSuperclass = typeToBeExtractedFrom.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
return Optional.of((ParameterizedType) genericSuperclass);
if (genericSuperclass instanceof ParameterizedType type) {
return Optional.of(type);
}
// Check the tree
return extractParameterizedType(typeToBeExtractedFrom.getSuperclass());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/cuioss/tools/string/Joiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ private String doJoin(Iterable<?> parts) {
if (!joinerConfig.isSkipNulls()) {
builder.add(joinerConfig.getUseForNull());
}
} else if (element instanceof CharSequence) {
builder.add((CharSequence) element);
} else if (element instanceof CharSequence sequence) {
builder.add(sequence);
} else {
builder.add(MoreStrings.lenientToString(element));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/cuioss/tools/string/MoreStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ public static int indexOf(final CharSequence cs, final int searchChar, int start
return INDEX_NOT_FOUND;
}

if (cs instanceof String) {
return ((String) cs).indexOf(searchChar, start);
if (cs instanceof String string) {
return string.indexOf(searchChar, start);
}
final var sz = cs.length();
if (start < 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/cuioss/tools/support/ExceptionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static String extractCauseMessageFromThrowable(final Throwable throwable)
if (null == throwable) {
return NO_MESSAGE;
}
if (throwable instanceof InvocationTargetException) {
return extractMessageFromThrowable(((InvocationTargetException) throwable).getTargetException());
if (throwable instanceof InvocationTargetException exception) {
return extractMessageFromThrowable(exception.getTargetException());
}
return extractMessageFromThrowable(throwable);
}
Expand Down

0 comments on commit ab54915

Please sign in to comment.