3535import java .util .stream .Collectors ;
3636
3737import com .google .gson .Gson ;
38+ import net .minecraftforge .fml .loading .moddiscovery .ModAnnotation ;
3839import org .apache .logging .log4j .LogManager ;
3940import org .apache .logging .log4j .Logger ;
4041import org .objectweb .asm .Type ;
@@ -110,8 +111,7 @@ public static class AnnotationData {
110111 private final Type clazz ;
111112 private final String memberName ;
112113
113- //lazy evaluated
114- private Map <String , Object > annotationData ;
114+ private Map <String , Object > elements ;
115115
116116 public AnnotationData (
117117 final Type annotationType , final ElementType targetType ,
@@ -123,6 +123,17 @@ public AnnotationData(
123123 this .memberName = memberName ;
124124 }
125125
126+ public AnnotationData (
127+ Type annotationType , ElementType targetType , Type clazz ,
128+ String memberName , Map <String , Object > elements
129+ ) {
130+ this .annotationType = annotationType ;
131+ this .targetType = targetType ;
132+ this .clazz = clazz ;
133+ this .memberName = memberName ;
134+ this .elements = elements ;
135+ }
136+
126137 public Type getAnnotationType () {
127138 return annotationType ;
128139 }
@@ -140,15 +151,15 @@ public String getMemberName() {
140151 }
141152
142153 public Map <String , Object > getAnnotationData () {
143- if (annotationData == null ) {
154+ if (elements == null ) {
144155 initAnnotationData ();
145156 }
146157
147- return annotationData ;
158+ return elements ;
148159 }
149160
150161 private void initAnnotationData () {
151- annotationData = new HashMap <>();
162+ elements = new HashMap <>();
152163
153164 try {
154165 // TODO: This *may* load classes in the wrong order, but it shouldn't be an issue
@@ -163,22 +174,21 @@ private void initAnnotationData() {
163174 return ;
164175 }
165176
166- Method [] argMethods = annotationObject .getClass ().getDeclaredMethods ();
177+ Method [] elementGetters = annotationObject .getClass ().getDeclaredMethods ();
167178
168- for (Method argMethod : argMethods ) {
169- if (isArgumentMethod (argMethod )) {
170- annotationData .put (
171- argMethod .getName (),
172- argMethod .invoke (annotationObject )
173- );
179+ for (Method elementGetter : elementGetters ) {
180+ if (isElementGetter (elementGetter )) {
181+ Object value = elementGetter .invoke (annotationObject );
182+
183+ elements .put (elementGetter .getName (), processElementObject (value ));
174184 }
175185 }
176186 } catch (Throwable e ) {
177187 LOGGER .catching (e );
178188 }
179189 }
180190
181- private static boolean isArgumentMethod (Method method ) {
191+ private static boolean isElementGetter (Method method ) {
182192 String name = method .getName ();
183193 if (name .equals ("toString" )) return false ;
184194 if (name .equals ("hashCode" )) return false ;
@@ -199,15 +209,14 @@ private Annotation getAnnotationObject(Class<?> clazzObj, Class annotationType)
199209 case METHOD :
200210 String methodName = memberName .substring (0 , memberName .indexOf ('(' ));
201211 Method [] methods = Arrays .stream (clazzObj .getDeclaredMethods ())
202- .filter (method -> method .getName ().equals (methodName ))
203- .toArray (Method []::new );
212+ .filter (method -> method .getName ().equals (methodName ))
213+ .toArray (Method []::new );
204214 if (methods .length == 0 ) {
205215 throw new RuntimeException ("Cannot find method " + methodName );
206216 }
207217
208218 if (methods .length > 1 ) {
209219 //TODO handle overloaded methods
210-
211220 throw new RuntimeException ("Currently Cannot Handle Overloaded Methods" );
212221 }
213222
@@ -235,4 +244,23 @@ public int hashCode() {
235244 return Objects .hash (annotationType , targetType , clazz , memberName );
236245 }
237246 }
247+
248+ private static Object processElementObject (Object object ) {
249+ if (object instanceof Object []) {
250+ return Arrays .stream ((Object []) object )
251+ .map (ModFileScanData ::processElementObject )
252+ .collect (Collectors .toList ());
253+ }
254+
255+ if (object instanceof Enum ) {
256+ Enum enumObject = (Enum ) object ;
257+ String className = enumObject .getDeclaringClass ().getName ();
258+ return new ModAnnotation .EnumHolder (
259+ className .replace ('.' , '/' ),
260+ enumObject .toString ()
261+ );
262+ }
263+
264+ return object ;
265+ }
238266}
0 commit comments