The answers of this question about the Groovy way to dynamically invoke a static method were very helpful but I\'m having trouble with the following case:
I defined
This works, using the underlying GroovyClassLoader:
def instance = this.class.classLoader.loadClass( 'Item', true, false )?.newInstance()
I just had to do this and found an interesting way--so I thought I'd come back and mention it.
I had A problem with this because I wanted to pass a value to newInstance (use a non-default constructor) and all the solutions seemed to be a little bit of work (I'm lazy, okay?)
Anyway, suppose you want to create a new Integer(5)... try this:
c = "java.lang.Integer"
p = "5"
def result = Eval.me("return new ${c}(${p})")
assert(result == 5)
Worked really well although I'm sure it's about the slowest solution possible. Has the advantage that the method is applicable to many other situations.