Created
October 8, 2012 12:48
-
-
Save e2kaneko/3852337 to your computer and use it in GitHub Desktop.
GroovyのMOP(Meta Object Protocol)
This file contains hidden or 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
| package com.e2info.groovy | |
| // groovy.lang.GroovyObject#getMetaClass | |
| String.metaClass.define { | |
| hello { return "hello " + delegate } | |
| bye { return "bye " + delegate } | |
| } | |
| println "world".hello() | |
| println "world".bye() | |
| String.metaClass.and = {return delegate + it} | |
| println "xxx".and("yyy") | |
| /* | |
| hello world | |
| bye world | |
| xxxyyy | |
| */ | |
| // groovy.lang.GroovyObject#invokeMethod | |
| class InvokeMethodSample{ | |
| def invokeMethod(String method, Object params) { | |
| println method | |
| if(params != null){ | |
| params.each{ println it } | |
| } | |
| } | |
| def getProperty(String property){ | |
| println property | |
| } | |
| } | |
| def im1 = new InvokeMethodSample(); | |
| im1.aiueo("a", "b"); | |
| /* | |
| aiueo | |
| a | |
| b | |
| */ | |
| // groovy.lang.GroovyObject#getProperty | |
| def im2 = new InvokeMethodSample(); | |
| im2.name | |
| /* | |
| name | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment