Skip to content

Commit 1ad2df4

Browse files
committed
fix reflection for MetaBeanProperty
1 parent 24c892b commit 1ad2df4

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cleanroommc.groovyscript;
2+
3+
public class TestClass {
4+
5+
private static int next = 20;
6+
7+
private final Inner inner;
8+
private final int id;
9+
10+
public TestClass(String name) {
11+
this.inner = new Inner(name);
12+
this.id = next++;
13+
}
14+
15+
public Inner getInner() {
16+
return inner;
17+
}
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public class Inner {
24+
25+
private final String name;
26+
27+
public Inner(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public int getId() {
36+
return id;
37+
}
38+
}
39+
}

src/main/java/com/cleanroommc/groovyscript/helper/MetaClassExpansion.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.cleanroommc.groovyscript.helper;
22

33
import com.cleanroommc.groovyscript.api.GroovyLog;
4-
import groovy.lang.MetaClass;
5-
import groovy.lang.MetaMethod;
6-
import groovy.lang.MetaProperty;
4+
import groovy.lang.*;
75
import org.codehaus.groovy.ast.ClassNode;
86
import org.codehaus.groovy.reflection.CachedField;
97
import org.codehaus.groovy.reflection.CachedMethod;
@@ -22,8 +20,14 @@ public class MetaClassExpansion {
2220
public static void makePublic(MetaClass mc, String memberName) {
2321
boolean success = false;
2422
MetaProperty mp = mc.getMetaProperty(memberName);
25-
if (mp instanceof CachedField cachedField) {
26-
ReflectionHelper.makeFieldPublic(cachedField.getCachedField());
23+
CachedField field = null;
24+
if (mp instanceof MetaBeanProperty beanProperty) {
25+
field = beanProperty.getField();
26+
} else if (mp instanceof CachedField cachedField) {
27+
field = cachedField;
28+
}
29+
if (field != null) {
30+
ReflectionHelper.makeFieldPublic(field.getCachedField());
2731
success = true;
2832
}
2933
for (MetaMethod mm : mc.getMethods()) {
@@ -48,9 +52,18 @@ public static void makePublic(MetaClass mc, String memberName) {
4852
* @param fieldName name of field to make non-final
4953
*/
5054
public static void makeMutable(MetaClass mc, String fieldName) {
55+
/*while (mc instanceof DelegatingMetaClass delegatingMetaClass) {
56+
mc = delegatingMetaClass.getAdaptee();
57+
}*/
5158
MetaProperty mp = mc.getMetaProperty(fieldName);
52-
if (mp instanceof CachedField cachedField) {
53-
ReflectionHelper.setFinal(cachedField.getCachedField(), false);
59+
CachedField field = null;
60+
if (mp instanceof MetaBeanProperty beanProperty) {
61+
field = beanProperty.getField();
62+
} else if (mp instanceof CachedField cachedField) {
63+
field = cachedField;
64+
}
65+
if (field != null) {
66+
ReflectionHelper.setFinal(field.getCachedField(), false);
5467
return;
5568
}
5669
GroovyLog.get().error("Failed to make member '{}' of class {} mutable, because no field was found!", fieldName, getName(mc));

0 commit comments

Comments
 (0)