RosettaCodeData/Task/Reflection-List-properties/Groovy/reflection-list-properties.groovy
2023-07-01 13:44:08 -04:00

24 lines
673 B
Groovy

import java.lang.reflect.Field
@SuppressWarnings("unused")
class ListProperties {
public int examplePublicField = 42
private boolean examplePrivateField = true
static void main(String[] args) {
ListProperties obj = new ListProperties()
Class clazz = obj.class
println "All public fields (including inherited):"
(clazz.fields).each { Field f ->
printf "%s\t%s\n", f, f.get(obj)
}
println()
println "All declared fields (excluding inherited):"
clazz.getDeclaredFields().each { Field f ->
f.accessible = true
printf "%s\t%s\n", f, f.get(obj)
}
}
}