I am creating a gradle plugin in groovy, but I can't access the fields of the class. Here's what I have:
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("myClass", MyClass)
println project.myClass.getClass().getName()
for(Field field : project.myClass.getClass().getFields()) {
println field.getName()
println field.getType()
}
}
}
class MyClass {
@MyAnnotation("Hello world")
String myFeild
}
Output
MyClass_Decorated
__$stMC
boolean
Expected
MyClass
myField
String
I forget how groovy works... if you don't specify a scope on a field is it public?
Class.getFields()
only returns the public fields in the class (and super classes). You might need to use a combination of Class.getDeclaredFields()
and Class.getSuperclass()
to get the private/protected/default scoped fields all the way up the class hierarchy.
If you don't want to reflect the _Decorated
class, you can reference the class directly instead of using the instance.
Eg: MyClass.fields
or MyClass.declaredFields
in groovy
Or: MyClass.class.getFields()
or MyClass.class.getDeclaredFields()
in java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With