Created
March 8, 2013 05:46
-
-
Save zzzfree/5114466 to your computer and use it in GitHub Desktop.
reflect groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReflectTest { | |
def name | |
def height | |
def add(){ | |
println "add" | |
} | |
def add(a){ | |
println "add "+a | |
} | |
static main(args) { | |
def show = {n-> | |
println "==================================" | |
ReflectTest.metaClass."$n".each{ println it.name } | |
} | |
show("properties") | |
show("methods") | |
Class.metaClass.static.fqn = { delegate.name } | |
String.metaClass.fqn << { println delegate } | |
assert String.fqn() == "java.lang.String" | |
"helo".fqn() | |
ReflectTest.metaClass.invokeMethod { name, gs -> | |
def metaMethod = delegate.metaClass.getMetaMethod(name, gs) | |
if (metaMethod) { | |
println "invoking " + name | |
metaMethod.doMethodInvoke(delegate, gs) | |
} else { | |
throw new MissingMethodException(name, delegate.class, gs) | |
} | |
} | |
new ReflectTest().add() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment