mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[epilogue,ntcore,wpiunits] Use pattern matching switch expressions where possible (#8925)
Use [pattern matching switch expressions](https://docs.oracle.com/en/java/javase/17/language/pattern-matching-switch.html) where possible. This is a JVM 21+ feature which wasn't available until recently. If you look at the Java bytecode, this improves performance from an O(n) runtime check of literally each of the `if (x instanceof y)` checks to instead be an O(1) tableswitch. --------- Signed-off-by: Jonah Snider <jonah@jonahsnider.com>
This commit is contained in:
@@ -47,13 +47,11 @@ public abstract class ElementHandler {
|
||||
* @return the logged datatype
|
||||
*/
|
||||
protected TypeMirror dataType(Element element) {
|
||||
if (element instanceof VariableElement field) {
|
||||
return field.asType();
|
||||
} else if (element instanceof ExecutableElement method) {
|
||||
return method.getReturnType();
|
||||
} else {
|
||||
throw new IllegalStateException("Unexpected" + element.getClass().getName());
|
||||
}
|
||||
return switch (element) {
|
||||
case VariableElement field -> field.asType();
|
||||
case ExecutableElement method -> method.getReturnType();
|
||||
default -> throw new IllegalStateException("Unexpected" + element.getClass().getName());
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,13 +116,11 @@ public abstract class ElementHandler {
|
||||
* @return the generated access snippet
|
||||
*/
|
||||
public String elementAccess(Element element, TypeElement loggedClass) {
|
||||
if (element instanceof VariableElement field) {
|
||||
return fieldAccess(field, loggedClass);
|
||||
} else if (element instanceof ExecutableElement method) {
|
||||
return methodAccess(method);
|
||||
} else {
|
||||
throw new IllegalStateException("Unexpected" + element.getClass().getName());
|
||||
}
|
||||
return switch (element) {
|
||||
case VariableElement field -> fieldAccess(field, loggedClass);
|
||||
case ExecutableElement method -> methodAccess(method);
|
||||
default -> throw new IllegalStateException("Unexpected" + element.getClass().getName());
|
||||
};
|
||||
}
|
||||
|
||||
private static String fieldAccess(VariableElement field, TypeElement loggedClass) {
|
||||
|
||||
@@ -107,14 +107,12 @@ public class LoggableHandler extends ElementHandler {
|
||||
*/
|
||||
private static String cacheVariableName(Element element) {
|
||||
// Generate unique names in case a field and a method share the same name
|
||||
if (element instanceof VariableElement) {
|
||||
return "$$%s".formatted(element.getSimpleName().toString());
|
||||
} else if (element instanceof ExecutableElement) {
|
||||
return "__%s".formatted(element.getSimpleName().toString());
|
||||
} else {
|
||||
return switch (element) {
|
||||
case VariableElement field -> "$$%s".formatted(field.getSimpleName().toString());
|
||||
case ExecutableElement method -> "__%s".formatted(method.getSimpleName().toString());
|
||||
// Generic fallback (shouldn't get here, since only fields and methods are logged)
|
||||
return element.getSimpleName().toString();
|
||||
}
|
||||
default -> element.getSimpleName().toString();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user