Skip to content

Instantly share code, notes, and snippets.

@nobeans
Last active December 19, 2015 23:59
Show Gist options
  • Save nobeans/6038343 to your computer and use it in GitHub Desktop.
Save nobeans/6038343 to your computer and use it in GitHub Desktop.
G*Workshop/MOP #jggug
String.metaClass.getLanguage = { "Groovy" }
String.metaClass.getLang = { "Groovy" }
assert "JGGUG".language == "Groovy"
assert "JGGUG".lang == "Groovy"
class Jggug {
// ズル
//String language = "Groovy"
//String lang = "Groovy"
def propertyMissing(String name, Object value) {
switch (name) {
case ~/language|lang/:
return "Groovy"
default:
//throw new MethodMissingException()
throw new RuntimeException()
}
}
}
assert new Jggug().language == "Groovy"
assert new Jggug().language == "Groovy"
class Foo {
String bar() {
"@"
}
String piyo() {
"@"
}
def propertyMissing(String name, value) {
this."$name"()
}
}
assert new Foo().bar() == new Foo().bar
assert new Foo().piyo() == new Foo().piyo
import java.util.regex.Matcher
String.metaClass.decapitalize = { ->
if (delegate.size() > 0) {
return delegate[0].toLowerCase() + delegate.substring(1)
}
return delegate
}
class Some {
def sexy = true
def strong = true
def propertyMissing(String name, value) {
switch (name) {
case ~/^isNot(.*)/:
def propName = Matcher.lastMatcher.group(1).decapitalize()
return !this[propName]
case ~/^is(.*)/:
def propName = Matcher.lastMatcher.group(1).decapitalize()
return this[propName]
}
}
}
assert new Some().isSexy
assert !new Some().isNotSexy
assert new Some().isStrong
assert !new Some().isNotStrong
import java.util.regex.Matcher
String.metaClass.decapitalize = { ->
if (delegate.size() > 0) {
return delegate[0].toLowerCase() + delegate.substring(1)
}
return delegate
}
class Some {
boolean sexy = true
boolean strong = true
def methodMissing(String name, value) {
switch (name) {
case ~/^isNot(.*)/:
def propName = Matcher.lastMatcher.group(1).decapitalize()
return !this[propName]
// case ~/^is(.*)/:
// def propName = Matcher.lastMatcher.group(1).decapitalize()
// return this[propName]
}
}
}
assert new Some().isSexy() // boolean型にしておけばこれはデフォでGroovyがサポート
assert !new Some().isNotSexy()
assert new Some().isStrong() // boolean型にしておけばこれはデフォでGroovyがサポート
assert !new Some().isNotStrong()
import java.util.regex.Matcher
String.metaClass.decapitalize = { ->
if (delegate.size() > 0) {
return delegate[0].toLowerCase() + delegate.substring(1)
}
return delegate
}
class Some {
boolean sexy = true
boolean strong = true
def propertyMissing(String name, value) {
switch (name) {
case ~/^not(.*)/:
def propName = Matcher.lastMatcher.group(1).decapitalize()
return !this[propName]
}
}
def methodMissing(String name, value) {
switch (name) {
case ~/^isNot(.*)/:
def propName = Matcher.lastMatcher.group(1).decapitalize()
return !this[propName]
// case ~/^is(.*)/:
// def propName = Matcher.lastMatcher.group(1).decapitalize()
// return this[propName]
}
}
}
assert new Some().isSexy() // boolean型にしておけばこれはデフォでGroovyがサポート
assert !new Some().isNotSexy()
assert new Some().isStrong() // boolean型にしておけばこれはデフォでGroovyがサポート
assert !new Some().isNotStrong()
// Groovyらしく、プロパティアクセスもできるように。
assert new Some().sexy
assert !new Some().notSexy
assert new Some().strong
assert !new Some().notStrong
import groovy.transform.Canonical
@Canonical
class Some {
int value
Some(value) {
this.value = value
}
}
//Integer.metaClass.static.new = { args ->
// new Integer(args)
//}
//Some.metaClass.static.new = { args ->
// new Some(args)
//}
//Integer.metaClass.static.new = { Object[] args ->
// delegate.newInstance(*args)
//}
//Some.metaClass.static.new = { Object[] args ->
// delegate.newInstance(*args)
//}
//Integer.metaClass.static.new = { Object[] args ->
// delegate.metaClass.invokeConstructor(*args)
//}
//Some.metaClass.static.new = { Object[] args ->
// delegate.metaClass.invokeConstructor(*args)
//}
Class.metaClass.static.new = { Object[] args ->
delegate.newInstance(*args)
}
assert 123 == Integer.new(123)
assert new Some(123) == Some.new(123)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment