mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
Useful for eg OpModes, where names have a maximum length Also includes validations for values in opmode annotations like `@Autonomous(name = "...")`; name, group, and description all have maximum allowable lengths
47 lines
1.6 KiB
Java
47 lines
1.6 KiB
Java
// 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 org.wpilib.annotation;
|
|
|
|
import java.lang.annotation.Documented;
|
|
import java.lang.annotation.ElementType;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
|
|
/**
|
|
* Place on a method parameter of type String. Any string literals passed to that parameter will be
|
|
* checked at compile-time to be no longer than the maximum allowed length. Note that this cannot
|
|
* check dynamically generated string values - it is strongly recommended to pair this annotation
|
|
* with a runtime check to cover cases where dynamic values are used.
|
|
*
|
|
* <p>Errors generated by this annotation cannot be suppressed.
|
|
*
|
|
* <pre>{@code
|
|
* void acceptString(@MaxLength(5) String str) {
|
|
* if (str.length() > 5) {
|
|
* throw new IllegalArgumentException("String is too long");
|
|
* }
|
|
* // ...
|
|
* }
|
|
*
|
|
* acceptString("12345"); // OK - length is 5
|
|
* acceptString("123456"); // Compile-time error: length is 6
|
|
* acceptString("123" + "456"); // Compile-time error: length is 6
|
|
* acceptString(" ".repeat(16)); // Runtime error - string argument is not a literal
|
|
* }</pre>
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.PARAMETER)
|
|
@Documented
|
|
public @interface MaxLength {
|
|
/**
|
|
* The maximum allowable length of string literals passed to the annotated parameter. Must be a
|
|
* positive integer.
|
|
*
|
|
* @return The maximum length of allowed strings.
|
|
*/
|
|
int value();
|
|
}
|