Skip to content
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 @@ -125,7 +125,9 @@ public final class PersistedConfiguration {
* @throws IllegalStateException If {@code preferenceName} was used for a different record class.
*/
public static <T extends Record> T fromPreferences(String preferenceName, T configWithDefaults) {
return fromPreferences(preferenceName, configWithDefaults, PATH_SEPARATOR);
@SuppressWarnings("unchecked")
Class<T> recordClass = (Class<T>) configWithDefaults.getClass();
return fromPreferences(preferenceName, recordClass, configWithDefaults);
}

/**
Expand All @@ -151,24 +153,17 @@ public static <T extends Record> T fromPreferences(String preferenceName, T conf
* @throws IllegalStateException If {@code preferenceName} was used for a different record class.
*/
public static <T extends Record> T fromPreferences(String preferenceName, Class<T> recordClass) {
return fromPreferences(preferenceName, recordClass, null, PATH_SEPARATOR);
}

static <T extends Record> T fromPreferences(
String preferenceName, T configWithDefaults, char pathSeparator) {
@SuppressWarnings("unchecked")
Class<T> recordClass = (Class<T>) configWithDefaults.getClass();
return fromPreferences(preferenceName, recordClass, configWithDefaults, pathSeparator);
return fromPreferences(preferenceName, recordClass, null);
}

private static <T extends Record> T fromPreferences(
String preferenceName, Class<T> recordClass, T configWithDefaults, char pathSeparator) {
String preferenceName, Class<T> recordClass, T configWithDefaults) {
NetworkTableInstance ntInstance = NetworkTableInstance.getDefault();
validatePreferenceName(preferenceName);
verifyNotRegisteredToAnotherClass(ntInstance, preferenceName, recordClass);

try {
return createFromPreferences(preferenceName, recordClass, configWithDefaults, pathSeparator);
return createFromPreferences(preferenceName, recordClass, configWithDefaults);
} catch (ReflectiveOperationException e) {
if (throwExceptions) {
throw new RuntimeException(e); // For self-tests.
Expand Down Expand Up @@ -197,8 +192,7 @@ private static void verifyNotRegisteredToAnotherClass(
}

NetworkTable preferencesTable = ntInstance.getTable("Preferences");
String key = String.format("%s%c.registeredTo", name, PATH_SEPARATOR);
NetworkTableEntry entry = preferencesTable.getEntry(key);
NetworkTableEntry entry = preferencesTable.getEntry(name + "/.registeredTo");
if (!entry.exists()) {
entry.setString(recordName);
} else {
Expand All @@ -213,15 +207,15 @@ private static void verifyNotRegisteredToAnotherClass(
}

private static <T> T createFromPreferences(
String prefix, Class<? extends T> clazz, T configWithDefaults, char pathSeparator)
String prefix, Class<? extends T> clazz, T configWithDefaults)
throws ReflectiveOperationException {
var components = clazz.getRecordComponents();
Object[] params = new Object[components.length];
Class<?>[] types = new Class[components.length];
int i = 0;
for (RecordComponent component : components) {
String name = component.getName();
String key = prefix + pathSeparator + name;
String key = prefix + PATH_SEPARATOR + name;
Class<?> type = component.getType();
types[i] = type;

Expand Down Expand Up @@ -250,7 +244,7 @@ private static <T> T createFromPreferences(
}

if (isRecordField) {
params[i] = createFromPreferences(key, type, componentValue, pathSeparator);
params[i] = createFromPreferences(key, type, componentValue);
} else if (factory == null) {
warn("Cannot store '%s' in Preferences; type %s is unsupported", name, type);
params[i] = componentValue;
Expand Down

This file was deleted.

Loading
Loading