Skip to content

Instantly share code, notes, and snippets.

@grimrose
Last active December 19, 2015 23:59
Show Gist options
  • Save grimrose/6038422 to your computer and use it in GitHub Desktop.
Save grimrose/6038422 to your computer and use it in GitHub Desktop.
MOPハンズオン - JGGUG G*ワークショップZ Jul 2013
package jggug
import groovy.transform.Canonical
class JGGUG {
Object propertyMissing(String name) {
if (name =~ /language/) return "Groovy"
if (name =~ /lang/) return "Groovy"
""
}
}
assert new JGGUG().language == "Groovy"
assert new JGGUG().lang == "Groovy"
class Foo {
String bar() {
"@"
}
String piyo() {
"f"
}
String hoge(int times) {
"X" * times
}
def getProperty(String name) {
if (metaClass.hasProperty(this, name)) return metaClass.getProperty(this, name)
invokeMethod name, null
}
}
assert new Foo().bar() == new Foo().bar
assert new Foo().piyo() == new Foo().piyo
assert new Foo().hoge(3) == "XXX"
class Some {
def sexy = true
def methodMissing(String name, args) {
if (name =~ /isSexy/) return this.sexy
if (name =~ /isNotSexy/) return !this.sexy
}
}
assert new Some().isSexy()
assert !new Some().isNotSexy()
def a = "aaaa"
assert a.metaClass =~ /(?i)MetaClassImpl/
a.metaClass.hoge = {}
assert a.metaClass =~ /(?i)expandoMetaClass/
@Canonical
class SomeOne {
def value
}
Class.metaClass.static.make = { Object[] args ->
delegate.metaClass.invokeConstructor(* args)
}
assert 123 == Integer.make(123)
assert new SomeOne(123) == SomeOne.make(123)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment