Add an annotation to skip config enums

This commit is contained in:
James Seibel
2023-06-12 20:24:49 -05:00
parent a00671c2e5
commit 12d5d9e32b
4 changed files with 68 additions and 0 deletions
@@ -0,0 +1,17 @@
package com.seibel.lod.api.enums.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Add this annotation to enum values that
* are valid config options, but shouldn't be selectable
* when toggling through the options. <br><br>
*
* Example: A preset's "custom" option shouldn't be selectable
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface DisallowSelectingViaConfigGui
{
}
@@ -19,6 +19,8 @@
package com.seibel.lod.api.enums.config.quickOptions;
import com.seibel.lod.api.enums.config.DisallowSelectingViaConfigGui;
/**
* CUSTOM, <br><br>
*
@@ -34,6 +36,7 @@ public enum EQualityPreset
// when adding items up the API minor version
// when removing items up the API major version
@DisallowSelectingViaConfigGui
CUSTOM,
MINIMUM,
@@ -19,6 +19,8 @@
package com.seibel.lod.api.enums.config.quickOptions;
import com.seibel.lod.api.enums.config.DisallowSelectingViaConfigGui;
/**
* CUSTOM, <br><br>
*
@@ -34,6 +36,7 @@ public enum EThreadPreset
// when adding items up the API minor version
// when removing items up the API major version
@DisallowSelectingViaConfigGui
CUSTOM,
MINIMAL_IMPACT,
@@ -0,0 +1,45 @@
package com.seibel.lod.core.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.lang.reflect.Field;
public class AnnotationUtil
{
private static final Logger LOGGER = LogManager.getLogger();
/** A quick method to test if an enum value has specific runtime annotation. */
public static <TEnum extends Enum<?>, TAnno extends java.lang.annotation.Annotation> boolean doesEnumHaveAnnotation(TEnum enumValue, Class<TAnno> annotationToSearchFor)
{
try
{
// fields will contain all possible enums
// unfortunately James isn't sure of a way to do this without looping through all enum values
// (although since enums should only have ~10 items at most, this shouldn't be a big deal)
Field[] fields = enumValue.getClass().getFields();
for (Field field : fields)
{
// only test for annotations for the
@SuppressWarnings("unchecked")
TEnum testEnumValue = (TEnum) field.get(enumValue);
if (testEnumValue == enumValue)
{
return field.getAnnotation(annotationToSearchFor) != null;
}
}
// should never happen
// if we got here Java screwed up getting us the enums
throw new IllegalStateException("Enum missing expected value. Enum: ["+enumValue.getClass()+"] doesn't contain the value: ["+enumValue.name()+"].");
}
catch (IllegalAccessException | IllegalArgumentException | ClassCastException e)
{
// shouldn't happen, but just in case
LOGGER.error("Unable to get annotation for enum: ["+enumValue.getClass()+"]. Unexpected exception: ["+e+"], message: ["+e.getMessage()+"].", e);
return false;
}
}
}