-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wpiutil] Record/Enum struct generation fix (#7538)
ProceduralStructGenerator's genRecord and genEnum were package-private, and only extractClassStruct was made public. However, this package private visibility rendered them unable to be used by the rest of wpilib(and advanced users). Here, ProceduralStructGenerator is split into 2 classes: StructGenerator(which generates structs) and StructFetcher(the new namespace for extractClassStruct). In addition, genRecord and genEnum have been made public methods.
- Loading branch information
1 parent
e943424
commit 5e3dba6
Showing
3 changed files
with
77 additions
and
64 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
wpiutil/src/main/java/edu/wpi/first/util/struct/StructFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.util.struct; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A utility class for fetching the assigned struct of existing classes. These are usually public, | ||
* static, and final properties with the Struct type. | ||
*/ | ||
public final class StructFetcher { | ||
private StructFetcher() { | ||
throw new UnsupportedOperationException("This is a utility class!"); | ||
} | ||
|
||
/** | ||
* Returns a {@link Struct} for the given {@link StructSerializable} marked class. Due to the | ||
* non-contractual nature of the marker this can fail. If the {@code struct} field could not be | ||
* accessed for any reason, an empty {@link Optional} is returned. | ||
* | ||
* @param <T> The type of the class. | ||
* @param clazz The class object to extract the struct from. | ||
* @return An optional containing the struct if it could be extracted. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T extends StructSerializable> Optional<Struct<T>> fetchStruct( | ||
Class<? extends T> clazz) { | ||
try { | ||
var possibleField = Optional.ofNullable(clazz.getDeclaredField("struct")); | ||
return possibleField.flatMap( | ||
field -> { | ||
if (Struct.class.isAssignableFrom(field.getType())) { | ||
try { | ||
return Optional.ofNullable((Struct<T>) field.get(null)); | ||
} catch (IllegalAccessException e) { | ||
return Optional.empty(); | ||
} | ||
} else { | ||
return Optional.empty(); | ||
} | ||
}); | ||
} catch (NoSuchFieldException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a {@link Struct} for the given class. This does not do compile time checking that the | ||
* class is a {@link StructSerializable}. Whenever possible it is reccomended to use {@link | ||
* #fetchStruct(Class)}. | ||
* | ||
* @param clazz The class object to extract the struct from. | ||
* @return An optional containing the struct if it could be extracted. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static Optional<Struct<?>> fetchStructDynamic(Class<?> clazz) { | ||
if (StructSerializable.class.isAssignableFrom(clazz)) { | ||
return fetchStruct((Class<? extends StructSerializable>) clazz).map(struct -> struct); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters