Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
@melix
melix / Foo.groovy
Created July 22, 2014 12:47
Dump AST transformations
import groovy.transform.ASTTest
import groovy.transform.CompileStatic
import groovy.transform.Immutable
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.control.CompilePhase
@ASTTest(phase=CompilePhase.INSTRUCTION_SELECTION,value={
ClassNode cn = node
def config = cn.compileUnit.config
def cu = cn.compileUnit.classLoader.createCompilationUnit(config, null)
@melix
melix / build.gradle
Created September 4, 2014 11:19
Workaround for VerifyError in Android+Groovy builds
// add this to build.gradle
gradle.taskGraph.whenReady {
allprojects {
tasks.withType(GroovyCompile) { task ->
logger.lifecycle("Task $task")
task.groovyOptions.forkOptions.jvmArgs=['-noverify']
}
}
}
@melix
melix / fatfingers.groovy
Last active August 29, 2015 14:06
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)

Keybase proof

I hereby claim:

  • I am melix on github.
  • I am melix (https://keybase.io/melix) on keybase.
  • I have a public key whose fingerprint is 3312 24E1 D7BE 883D 16E8 A685 825C 06C8 27AF 6B66

To claim this, I am signing this object:

@melix
melix / ast-matching.groovy
Created October 17, 2014 22:19
AST Pattern matching
// "macro" builds the AST of the corresponding block
def ast1 = macro { foo(a) + foo(a) }
def ast2 = macro { foo(a) + foo(foo(a)) }
// we build another AST which will be used as a pattern
def pattern = macro {
x + foo(x)
}.withConstraints {
// declare that x is not a variable, but a placeholder
placeholder x
@melix
melix / configurable.groovy
Created November 2, 2014 10:35
DelegatesTo + generic
import groovy.transform.CompileStatic
public <T> T configure(@DelegatesTo.Target Class<T> clazz, @DelegatesTo(genericTypeIndex = 0) Closure<Void> conf) {
def result = clazz.newInstance()
result.with(conf)
result
}
class Person {
String name
@melix
melix / ratpack-delegation.groovy
Last active August 29, 2015 14:08
DelegatesTo any type in Groovy 2.4+
trait Configurable<ConfigObject> {
ConfigObject configObject
void configure(Closure<Void> configSpec) {
configSpec.resolveStrategy = Closure.DELEGATE_FIRST
configSpec.delegate = configObject
configSpec()
}
}
@melix
melix / matchtest.groovy
Created November 12, 2014 07:27
AST pattern matching with relaxed constraints
void testRelaxedBinaryExpressionWithConstrainedToken() {
use(ASTMatcher) {
def ast1 = macro { a + b }
def ast2 = macro { a - b }
def ast3 = macro { a * b }
def ast4 = macro { a + c }
def pattern = macro {
a + b
}.withConstraints {
token {
@melix
melix / astrelax.groovy
Created November 12, 2014 10:01
AST pattern matching with relaxed constraints 2
void testInlineMacroCombinationWithConstraints() {
use(ASTMatcher) {
def ast1 = macro { a + b }
def ast2 = macro { b + b }
def ast3 = macro { b + c }
def ast4 = macro { b - b }
def pattern = macro {
$v { macro { a }.withConstraints { placeholder a } } + b
}.withConstraints {
anyToken()
@melix
melix / Fibonenchmark
Last active August 29, 2015 14:10
Fibonacci Benchmark
# Run complete. Total time: 00:34:14
Benchmark Mode Samples Score Error Units
o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_cs avgt 30 1.961 ± 0.055 ms/op
o.g.m.f.FibonacciMicroBenchmark.baseline_java_30 avgt 30 2.004 ± 0.084 ms/op
o.g.m.f.FibonacciMicroBenchmark.baseline_java_boxing_30 avgt 30 5.504 ± 0.036 ms/op
o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_30 avgt 30 5.733 ± 0.024 ms/op
o.g.m.f.FibonacciMicroBenchmark.groovy_indy_30 avgt 30 5.903 ± 0.033 ms/op
o.g.m.f.FibonacciMicroBenchmark.golo_30 avgt 30 6.545 ± 0.060 ms/op
o.g.m.f.FibonacciMicroBenchmark.nashorn_30 avgt 30 7.812 ± 0.061 ms/op