I'm trying to generate classes dynamically in Groovy. My approach is this:
class MetaClassTest {
static class TestClass {
String getName() {
return "Jake"
}
}
public static void main(String[] args) {
def testClass = TestClass
def metaMethod = testClass.metaClass.getMetaMethod('getName', [] as Class[])
testClass.metaClass.getName = {
metaMethod.invoke(delegate) + " and the Fatman"
}
assert testClass.newInstance().name == "Jake and the Fatman"
}
}
This however changes the behavior of the original class which is what I don't want. So my question is:
How can I clone (and probably rename) an existing class dynamically so that I'd be able to create multiple derivates?
Aucun commentaire:
Enregistrer un commentaire