Package de.Keyle.MyPet.api.util
Class AnnotationLookup
java.lang.Object
de.Keyle.MyPet.api.util.AnnotationLookup
Resolve an annotation-carried name (e.g.
@SkillName("Damage"),
@LeashFlagName("BelowHP"), @RequirementName("PetLevel"))
by walking a class's superclass chain and implemented interfaces.
The annotation is only honored on classes assignable to a given
marker type — e.g. @SkillName on a class that doesn't implement
Skill is ignored. This matches the historical behavior of the
three near-identical walkers that lived on
SkillManager#getSkillName, LeashFlagManager#getLeashFlagName,
and SkilltreeManager#getRequirementName.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Annotation>
ASearchesclazz, its superclass chain and every implemented interface for an annotation of the given type, regardless of any marker type.static <A extends Annotation>
StringfindName(Class<?> clazz, Class<A> annotationType, Class<?> markerType, Function<A, String> nameExtractor) Recursively searchclazz, its superclass chain, and every implemented interface for an instance ofannotationTypecarried on a class assignable tomarkerType.
-
Method Details
-
find
Searchesclazz, its superclass chain and every implemented interface for an annotation of the given type, regardless of any marker type. The class itself is checked first, then the superclass chain, then the interfaces; the first match wins.- Type Parameters:
A- the annotation type- Parameters:
clazz- the class to start searching from, may benullannotationType- the annotation class to look for, may benull- Returns:
- the annotation instance if found, or
null
-
findName
public static <A extends Annotation> String findName(Class<?> clazz, Class<A> annotationType, Class<?> markerType, Function<A, String> nameExtractor) Recursively searchclazz, its superclass chain, and every implemented interface for an instance ofannotationTypecarried on a class assignable tomarkerType. The first match wins.- Type Parameters:
A- the annotation carrying the name- Parameters:
clazz- the class to inspect (or any ancestor reached during recursion)annotationType- the annotation class to look formarkerType- the marker interface/class the annotated type must extend or implementnameExtractor- pulls thevalue()(or equivalent) off the annotation instance- Returns:
- the extracted name, or
nullif no annotated ancestor is found
-