Skip to content

Instantly share code, notes, and snippets.

@melix
Last active August 29, 2015 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/5bc7c5db7ab986924181 to your computer and use it in GitHub Desktop.
Save melix/5bc7c5db7ab986924181 to your computer and use it in GitHub Desktop.
Fat fingers correction in Groovy
import org.codehaus.groovy.reflection.ClassInfo
import org.codehaus.groovy.runtime.MethodRankHelper
trait FatFingers {
def methodMissing(String name, args) {
def ci = ClassInfo.getClassInfo(this.class)
def methods = [*ci.metaClass.methods,*ci.metaClass.metaMethods]
def sugg = MethodRankHelper.rankMethods(name,args,methods)
if (sugg) {
sugg[0].invoke(this, args)
} else {
super.methodMissing(name, args)
}
}
}
class Boo implements FatFingers {
void boo() { println "Boo" }
}
def b = new Boo()
b.booo()
@bdkosher
Copy link

Nice. I'd always wanted to design a language where you could suppress compiler errors and warnings by adding more ! characters to your statements. If the compiler complained about b.booo() not existing, you could call b.booo!() and force it to call some similarly named method. This code would work well for that.

@timyates
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment