mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpilib] Fix OpModeRobot initialization (#8834)
There were 3 cases failing before. 1. An OpModeRobot with no annotation. 2. An OpModeRobot with an annotation but a parameterless constructor. 3. An OpMode with a UserControls constructor This PR solves both of these issues. The first one is solved by adding the null check before setting the user controls instance. That one will never have an opmode instance. The second one is solved by falling back to a parameterless constructor if one with a parameter is not found. The 3rd one is solved by using the annotation type rather than the instance for constructor lookup. Also fixes ExpansionHubSample's missing annotations.
This commit is contained in:
@@ -115,9 +115,8 @@ public abstract class OpModeRobot extends RobotBase {
|
||||
Optional<ConstructorMatch<T>> ctor;
|
||||
|
||||
// try 2-parameter constructor
|
||||
if (m_userControlsInstance != null) {
|
||||
ctor =
|
||||
ConstructorMatch.findBestConstructor(cls, getClass(), m_userControlsInstance.getClass());
|
||||
if (m_userControlsBaseClass.isPresent()) {
|
||||
ctor = ConstructorMatch.findBestConstructor(cls, getClass(), m_userControlsBaseClass.get());
|
||||
if (ctor.isPresent()) {
|
||||
return ctor;
|
||||
}
|
||||
@@ -130,8 +129,8 @@ public abstract class OpModeRobot extends RobotBase {
|
||||
}
|
||||
|
||||
// try 1-parameter constructor with UserControls parameter
|
||||
if (m_userControlsInstance != null) {
|
||||
ctor = ConstructorMatch.findBestConstructor(cls, m_userControlsInstance.getClass());
|
||||
if (m_userControlsBaseClass.isPresent()) {
|
||||
ctor = ConstructorMatch.findBestConstructor(cls, m_userControlsBaseClass.get());
|
||||
if (ctor.isPresent()) {
|
||||
return ctor;
|
||||
}
|
||||
|
||||
@@ -294,12 +294,15 @@ public abstract class RobotBase implements AutoCloseable {
|
||||
UserControlsInstance userControlsAttribute =
|
||||
robotClass.getDeclaredAnnotation(UserControlsInstance.class);
|
||||
UserControls userControlsInstance = null;
|
||||
Optional<ConstructorMatch<T>> constructorMatch;
|
||||
Optional<ConstructorMatch<T>> constructorMatch = Optional.empty();
|
||||
if (userControlsAttribute != null) {
|
||||
var userControlsClass = userControlsAttribute.value();
|
||||
userControlsInstance = userControlsClass.getDeclaredConstructor().newInstance();
|
||||
constructorMatch = ConstructorMatch.findBestConstructor(robotClass, userControlsClass);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (constructorMatch.isEmpty()) {
|
||||
// Try to find a constructor with no parameters if there is no UserControls constructor
|
||||
constructorMatch = ConstructorMatch.findBestConstructor(robotClass);
|
||||
}
|
||||
|
||||
@@ -310,7 +313,7 @@ public abstract class RobotBase implements AutoCloseable {
|
||||
|
||||
T robot = constructorMatch.get().newInstance(userControlsInstance);
|
||||
|
||||
if (robot instanceof OpModeRobot opModeRobot) {
|
||||
if (userControlsInstance != null && robot instanceof OpModeRobot opModeRobot) {
|
||||
// Insert the UserControls instance into the opModeRobot for use when constructing opmodes
|
||||
opModeRobot.setUserControlsInstance(userControlsInstance);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user