From 5c95966d44f29140772b913936be8943ebcd4a0d Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Sat, 7 Dec 2024 13:56:05 -0500 Subject: [PATCH] Fix epilogue loading optional types from the newcommands vendordep (#7505) --- .../epilogue/processor/SendableHandler.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/epilogue-processor/src/main/java/edu/wpi/first/epilogue/processor/SendableHandler.java b/epilogue-processor/src/main/java/edu/wpi/first/epilogue/processor/SendableHandler.java index 3e9af00801..d25ce09d31 100644 --- a/epilogue-processor/src/main/java/edu/wpi/first/epilogue/processor/SendableHandler.java +++ b/epilogue-processor/src/main/java/edu/wpi/first/epilogue/processor/SendableHandler.java @@ -4,24 +4,32 @@ package edu.wpi.first.epilogue.processor; +import java.util.Optional; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; public class SendableHandler extends ElementHandler { - private final TypeMirror m_sendableType; - private final TypeMirror m_commandType; - private final TypeMirror m_subsystemType; + private final Optional m_sendableType; + private final Optional m_commandType; + private final Optional m_subsystemType; protected SendableHandler(ProcessingEnvironment processingEnv) { super(processingEnv); m_sendableType = - lookupTypeElement(processingEnv, "edu.wpi.first.util.sendable.Sendable").asType(); + Optional.ofNullable( + lookupTypeElement(processingEnv, "edu.wpi.first.util.sendable.Sendable")) + .map(TypeElement::asType); m_commandType = - lookupTypeElement(processingEnv, "edu.wpi.first.wpilibj2.command.Command").asType(); + Optional.ofNullable( + lookupTypeElement(processingEnv, "edu.wpi.first.wpilibj2.command.Command")) + .map(TypeElement::asType); m_subsystemType = - lookupTypeElement(processingEnv, "edu.wpi.first.wpilibj2.command.SubsystemBase").asType(); + Optional.ofNullable( + lookupTypeElement(processingEnv, "edu.wpi.first.wpilibj2.command.SubsystemBase")) + .map(TypeElement::asType); } @Override @@ -30,20 +38,28 @@ public class SendableHandler extends ElementHandler { // Accept any sendable type. However, the log invocation will return null // for sendable types that should not be logged (commands, subsystems) - return m_processingEnv.getTypeUtils().isAssignable(dataType, m_sendableType); + return m_sendableType + .map(t -> m_processingEnv.getTypeUtils().isAssignable(dataType, t)) + .orElse(false); } @Override public String logInvocation(Element element) { var dataType = dataType(element); - if (m_processingEnv.getTypeUtils().isAssignable(dataType, m_commandType) - || m_processingEnv.getTypeUtils().isAssignable(dataType, m_subsystemType)) { - // Do not log commands or subsystems via their sendable implementations - // We accept all sendable objects to prevent them from being reported as not loggable, - // but their sendable implementations do not include helpful information. - // Users are free to provide custom logging implementations for commands, and tag their - // subsystems with @Logged to log their contents automatically + // Do not log commands or subsystems via their sendable implementations + // We accept all sendable objects to prevent them from being reported as not loggable, + // but their sendable implementations do not include helpful information. + // Users are free to provide custom logging implementations for commands, and tag their + // subsystems with @Logged to log their contents automatically + if (m_commandType + .map(t -> m_processingEnv.getTypeUtils().isAssignable(dataType, t)) + .orElse(false)) { + return null; + } + if (m_subsystemType + .map(t -> m_processingEnv.getTypeUtils().isAssignable(dataType, t)) + .orElse(false)) { return null; }