Skip to content

Commit

Permalink
Use switch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Dec 26, 2024
1 parent 92e84bc commit d20dda9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,15 @@ public JavacJctFlagBuilderImpl failOnWarnings(boolean enabled) {
@Override
public JctFlagBuilder compilationMode(CompilationMode compilationMode) {
switch (compilationMode) {
case COMPILATION_ONLY:
craftedFlags.add(PROC_NONE);
break;

case ANNOTATION_PROCESSING_ONLY:
craftedFlags.add(PROC_ONLY);
break;

default:
case COMPILATION_ONLY -> craftedFlags.add(PROC_NONE);
case ANNOTATION_PROCESSING_ONLY -> craftedFlags.add(PROC_ONLY);
default -> {
if (Runtime.version().feature() >= 22) {
// In Java 22, the default is to disable all annotation processing by default
// Prior to Java 22, the default was to enable all annotation processing by default.
craftedFlags.add(PROC_FULL);
}
break;
}
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,11 @@ public final void report(Diagnostic<? extends S> diagnostic) {
}

private Level diagnosticToLevel(Diagnostic<?> diagnostic) {
switch (diagnostic.getKind()) {
case ERROR:
return Level.ERROR;
case WARNING:
case MANDATORY_WARNING:
return Level.WARN;
default:
return Level.INFO;
}
return switch (diagnostic.getKind()) {
case ERROR -> Level.ERROR;
case WARNING, MANDATORY_WARNING -> Level.WARN;
default -> Level.INFO;
};
}

private Supplier<String> messageGetter(Diagnostic<?> diagnostic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,28 @@ public JctFileManagerAnnotationProcessorClassPathConfigurer(JctCompiler compiler
public JctFileManager configure(JctFileManager fileManager) {
log.debug("Configuring annotation processor discovery mechanism");

switch (compiler.getAnnotationProcessorDiscovery()) {
case ENABLED:
return switch (compiler.getAnnotationProcessorDiscovery()) {
case ENABLED -> {
log.trace("Annotation processor discovery is enabled, ensuring empty location exists");

INHERITED_AP_PATHS.values().forEach(fileManager::createEmptyLocation);

return fileManager;
yield fileManager;
}

case INCLUDE_DEPENDENCIES:
case INCLUDE_DEPENDENCIES -> {
log.trace("Annotation processor discovery is enabled, copying classpath dependencies "
+ "into the annotation processor path");

INHERITED_AP_PATHS.forEach(fileManager::copyContainers);
INHERITED_AP_PATHS.values().forEach(fileManager::createEmptyLocation);

return fileManager;
yield fileManager;
}

default:
throw new JctIllegalInputException("Cannot configure annotation processor discovery");
}
default -> throw new JctIllegalInputException(
"Cannot configure annotation processor discovery");
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ public JctFileManagerLoggingProxyConfigurer(JctCompiler compiler) {
public JctFileManager configure(JctFileManager fileManager) {
log.debug("Configuring compiler operation audit logging");

switch (compiler.getFileManagerLoggingMode()) {
case STACKTRACES:
return switch (compiler.getFileManagerLoggingMode()) {
case STACKTRACES -> {
log.trace("Decorating file manager {} in a logger proxy with stack traces", fileManager);
return LoggingFileManagerProxy.wrap(fileManager, true);
case ENABLED:
yield LoggingFileManagerProxy.wrap(fileManager, true);
}
case ENABLED -> {
log.trace("Decorating file manager {} in a logger proxy", fileManager);
return LoggingFileManagerProxy.wrap(fileManager, false);
default:
throw new IllegalStateException("Cannot configure logger proxy");
}
yield LoggingFileManagerProxy.wrap(fileManager, false);
}
default -> throw new IllegalStateException("Cannot configure logger proxy");
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,9 @@ private static void appendQuoted(StringBuilder builder, @Nullable Object object)
for (var i = 0; i < objectStr.length(); ++i) {
var c = objectStr.charAt(i);
switch (c) {
case '\\':
builder.append("\\\\");
break;
case '"':
builder.append("\\\"");
break;
default:
builder.append(c);
case '\\' -> builder.append("\\\\");
case '"' -> builder.append("\\\"");
default -> builder.append(c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,13 @@ private static void appendToStringCharSequence(StringBuilder builder, CharSequen
for (var i = 0; i < len; ++i) {
var next = chars.charAt(i);
switch (next) {
case '\0':
builder.append("\\0");
break;
case '\r':
builder.append("\\r");
break;
case '\n':
builder.append("\\n");
break;
case '\t':
builder.append("\\t");
break;
case '\"':
builder.append("\\\"");
break;
case '\\':
builder.append("\\\\");
break;
default:
case '\0' -> builder.append("\\0");
case '\r' -> builder.append("\\r");
case '\n' -> builder.append("\\n");
case '\t' -> builder.append("\\t");
case '\"' -> builder.append("\\\"");
case '\\' -> builder.append("\\\\");
default -> {
if (0x20 <= next && next <= 0x7E || 0x80 <= next && next <= 0xFF) {
builder.append(next);
} else {
Expand All @@ -162,7 +150,7 @@ private static void appendToStringCharSequence(StringBuilder builder, CharSequen
.append("0".repeat(4 - hex.length()))
.append(hex);
}
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,13 @@ private static InputStream maybeBuffer(InputStream input, @Nullable String schem
? "unknown"
: scheme.toLowerCase(Locale.ENGLISH);

switch (scheme) {
case "classpath":
case "memory":
case "jrt":
case "ram":
return input;
default:
return switch (scheme) {
case "classpath", "memory", "jrt", "ram" -> input;
default -> {
log.trace("Decided to wrap input {} in a buffer - scheme was {}", input, scheme);
return new BufferedInputStream(input);
}
yield new BufferedInputStream(input);
}
};
}

private static ClassLoader currentCallerClassLoader() {
Expand Down

0 comments on commit d20dda9

Please sign in to comment.