From acaee44a5353c5cab9f4ab738520f41ab89f2c1b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 22 Apr 2024 15:40:34 -0700 Subject: [PATCH] GUACAMOLE-374: Allow multiple PropertyValue annotations to be associated with each enum value for EnumGuacamoleProperty. --- .../properties/EnumGuacamoleProperty.java | 40 ++++++++++++++++++- .../properties/EnumGuacamolePropertyTest.java | 4 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/properties/EnumGuacamoleProperty.java b/guacamole-ext/src/main/java/org/apache/guacamole/properties/EnumGuacamoleProperty.java index 255e4b09a..a55299365 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/properties/EnumGuacamoleProperty.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/properties/EnumGuacamoleProperty.java @@ -20,6 +20,7 @@ package org.apache.guacamole.properties; import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -45,8 +46,10 @@ public abstract class EnumGuacamoleProperty> implements Guacam /** * Defines the string value which should be accepted and parsed into the - * annotated enum constant. + * annotated enum constant. This annotation is repeatable, and each enum + * constant may be associated with any any number of string values. */ + @Repeatable(PropertyValues.class) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public static @interface PropertyValue { @@ -63,6 +66,29 @@ public abstract class EnumGuacamoleProperty> implements Guacam } + /** + * Defines the string values which should be accepted and parsed into the + * annotated enum constant. Each enum constant may be associated with any + * any number of string values. + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public static @interface PropertyValues { + + /** + * Returns the {@link PropertyValue} annotations that represent the + * String values that should produce the annotated enum constant when + * parsed. + * + * @return + * The {@link PropertyValue} annotations that represent the String + * values that should produce the annotated enum constant when + * parsed. + */ + PropertyValue[] value(); + + } + /** * Mapping of valid property values to the corresponding enum constants * that those values parse to. @@ -103,7 +129,17 @@ public abstract class EnumGuacamoleProperty> implements Guacam + "match declared values.", e); } - // Map enum constant only if PropertyValue annotation is present + // Map enum constant only if one or more PropertyValue annotations + // are present + PropertyValues valuesAnnotation = field.getAnnotation(PropertyValues.class); + if (valuesAnnotation != null) { + for (PropertyValue valueAnnotation : valuesAnnotation.value()) + valueMapping.put(valueAnnotation.value(), value); + } + + // The PropertyValue annotation may appear as a separate, single + // annotation, or as a multi-valued annotation via PropertyValues + // (see above) PropertyValue valueAnnotation = field.getAnnotation(PropertyValue.class); if (valueAnnotation != null) valueMapping.put(valueAnnotation.value(), value); diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/properties/EnumGuacamolePropertyTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/properties/EnumGuacamolePropertyTest.java index 5b9a3d742..f2f313c00 100644 --- a/guacamole-ext/src/test/java/org/apache/guacamole/properties/EnumGuacamolePropertyTest.java +++ b/guacamole-ext/src/test/java/org/apache/guacamole/properties/EnumGuacamolePropertyTest.java @@ -70,6 +70,7 @@ public class EnumGuacamolePropertyTest { * @see Tuna (Wikipedia) */ @PropertyValue("tuna") + @PropertyValue("yellowfin") TUNA, /** @@ -135,6 +136,7 @@ public class EnumGuacamolePropertyTest { assertEquals(Fish.TROUT, FAVORITE_FISH.parseValue("trout")); assertEquals(Fish.MACKEREL, FAVORITE_FISH.parseValue("mackerel")); assertEquals(Fish.TUNA, FAVORITE_FISH.parseValue("tuna")); + assertEquals(Fish.TUNA, FAVORITE_FISH.parseValue("yellowfin")); assertEquals(Fish.SARDINE, FAVORITE_FISH.parseValue("sardine")); } @@ -164,7 +166,7 @@ public class EnumGuacamolePropertyTest { } catch (GuacamoleException e) { String message = e.getMessage(); - assertTrue(message.contains("\"mackerel\", \"salmon\", \"sardine\", \"trout\", \"tuna\"")); + assertTrue(message.contains("\"mackerel\", \"salmon\", \"sardine\", \"trout\", \"tuna\", \"yellowfin\"")); } }