|
7 | 7 | import com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper; |
8 | 8 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
9 | 9 | import org.jetbrains.annotations.ApiStatus; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
10 | 12 | import org.jetbrains.annotations.UnmodifiableView; |
11 | 13 |
|
12 | 14 | import java.lang.reflect.Field; |
@@ -47,28 +49,52 @@ protected void addProperty(INamed property) { |
47 | 49 | @ApiStatus.OverrideOnly |
48 | 50 | public void initialize(GroovyContainer<?> owner) {} |
49 | 51 |
|
50 | | - protected void addPropertyFieldsOf(Object object, boolean privateToo) { |
| 52 | + protected void addPropertyFieldsOf(@NotNull Object object, boolean privateToo) { |
| 53 | + addPropertyFieldsOf(object, null, privateToo); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Searches all fields in an object and adds them with their aliases as a groovy bean property. |
| 58 | + * This method is automatically called for every external and internal compat with the property container as its first argument. |
| 59 | + * |
| 60 | + * @param object the object to search fields in. If this is a class only static fields are allowed. |
| 61 | + * @param untilSuperclass this determines how deep the class hierarchy should be searched (this class excluded). If this i null it will search up to |
| 62 | + * {@link GroovyPropertyContainer}.class if the object to search in is a subclass of {@link GroovyPropertyContainer} or |
| 63 | + * {@link Object}.class else. |
| 64 | + * @param privateToo true if non-public fields should be included too |
| 65 | + */ |
| 66 | + protected void addPropertyFieldsOf(@NotNull Object object, @Nullable Class<?> untilSuperclass, boolean privateToo) { |
51 | 67 | boolean staticOnly = false; |
52 | 68 | Class<?> clazz; |
53 | | - if (object instanceof Class<?>c) { |
| 69 | + if (object instanceof Class<?> c) { |
54 | 70 | clazz = c; |
55 | 71 | staticOnly = true; |
56 | 72 | } else { |
57 | 73 | clazz = object.getClass(); |
58 | 74 | } |
59 | | - for (Field field : clazz.getDeclaredFields()) { |
60 | | - boolean isStatic = Modifier.isStatic(field.getModifiers()); |
61 | | - if (!field.isAnnotationPresent(GroovyBlacklist.class) && INamed.class.isAssignableFrom(field.getType()) && (!staticOnly || isStatic) && (privateToo || (Modifier.isPublic(field.getModifiers())))) { |
62 | | - try { |
63 | | - if (!field.isAccessible()) field.setAccessible(true); |
64 | | - Object o = field.get(isStatic ? null : object); |
65 | | - if (o != null) { |
66 | | - addProperty((INamed) o); |
| 75 | + if (untilSuperclass == null) { |
| 76 | + if (GroovyPropertyContainer.class.isAssignableFrom(clazz)) { |
| 77 | + untilSuperclass = GroovyPropertyContainer.class; |
| 78 | + } else { |
| 79 | + untilSuperclass = Object.class; |
| 80 | + } |
| 81 | + } |
| 82 | + while (clazz != null && clazz != untilSuperclass) { |
| 83 | + for (Field field : clazz.getDeclaredFields()) { |
| 84 | + boolean isStatic = Modifier.isStatic(field.getModifiers()); |
| 85 | + if (!field.isAnnotationPresent(GroovyBlacklist.class) && INamed.class.isAssignableFrom(field.getType()) && (!staticOnly || isStatic) && (privateToo || (Modifier.isPublic(field.getModifiers())))) { |
| 86 | + try { |
| 87 | + if (!field.isAccessible()) field.setAccessible(true); |
| 88 | + Object o = field.get(isStatic ? null : object); |
| 89 | + if (o != null) { |
| 90 | + addProperty((INamed) o); |
| 91 | + } |
| 92 | + } catch (IllegalAccessException e) { |
| 93 | + GroovyLog.get().errorMC("Failed to register {} as named property", field.getName()); |
67 | 94 | } |
68 | | - } catch (IllegalAccessException e) { |
69 | | - GroovyLog.get().errorMC("Failed to register {} as named property", field.getName()); |
70 | 95 | } |
71 | 96 | } |
| 97 | + clazz = clazz.getSuperclass(); |
72 | 98 | } |
73 | 99 | } |
74 | 100 | } |
0 commit comments