2010-12-17 8 views

Antwort

7

Voila

void callMethod(Class c, String staticmethodname){ 
    c."$staticmethodname"() 
} 

class test { 
    static someMethod() { 
    println "me" 
    } 
} 

callMethod(test, "someMethod") 
2

Sie tun können, ist es sicherlich die Java-Weg:

c.getMethod(staticmethodname).invoke(null); 
+0

Scheint nicht arbeite in der groovigen Konsole. – mfloryan

+0

Die richtige Version wäre: c.getMethod (staticmethodname) .invoke (null); – mfloryan

+0

korrekt. Jetzt funktioniert es in der Konsole – Bozho

2

Sie es wie folgt tun: zu

def callMethod(Class c, String staticmethodname, args = null) { 
    args ? c."$staticmethodname"(args) : c."$staticmethodname"() 
} 

println callMethod(String.class, 'valueOf', 1) 
println callMethod(Calendar.class, 'getInstance') 
Verwandte Themen