Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
def "lazy constant evaluation"() {
given:
def config = new CompilerConfiguration()
config.optimizationOptions.indy = true
def shell = new GroovyShell(config)
when:
def method = shell.evaluate '''import groovy.transform.CompileStatic
import java.lang.invoke.CallSite
import java.lang.invoke.ConstantCallSite
@melix
melix / body.tpl
Last active August 12, 2019 01:04
An example of improved template composition using fragments and the MarkupTemplateEngine
html {
head {
title(title)
}
body {
content()
}
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.0'
}
}
apply plugin: 'android'
@melix
melix / GroovyActivity.groovy
Created April 24, 2014 09:17
An Android activity, written in Groovy, and able to compile and run Groovy code dynamically on the device.
package me.champeau.groovydroid
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import android.widget.EditText
import com.android.dx.dex.DexFormat
import com.android.dx.dex.DexOptions
@melix
melix / ConversionTest.java
Created April 23, 2014 20:01
Conversion of MethodHandle
/*
* Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@melix
melix / README.adoc
Last active July 7, 2021 07:37
An automated solver for 2048

Automated solver for 2048

This is a script which runs a "bot" that tries to solve the incredibly addictive 2048 game. It makes use of Groovy and browser automation with Geb.

How to

  • First step, if you don’t have it, you need to install Groovy.

  • Second step is to install the Google Chrome WebDriver (just unzip the appropriate file to a local directory)

  • Next, edit the script to set the path to the Chrome Driver

  • then execute the script: groovy solver2048.groovy

@melix
melix / TransformLocalVariable.groovy
Created March 6, 2014 18:31
A demonstration on how to transform a local variable using a compilation customizer
import org.codehaus.groovy.ast.ClassCodeExpressionTransformer
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.DeclarationExpression
import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.classgen.GeneratorContext
import org.codehaus.groovy.control.CompilationFailedException
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.SourceUnit
@melix
melix / taglib_reuse.groovy
Created February 25, 2014 17:40
Reuse of Grails taglibs in the markup template engine
void testGrailsTagLibCompatibility() {
TemplateConfiguration config = new TemplateConfiguration()
MarkupTemplateEngine engine = new MarkupTemplateEngine(this.class.classLoader, config)
def template = engine.createTemplate '''g.emoticon(happy:'true') { 'Hi John' }
'''
StringWriter rendered = new StringWriter()
def model = [:]
def tpl = template.make(model)
model.g = new TagLibAdapter(tpl)
model.g.registerTagLib(SimpleTagLib)
@melix
melix / bench_markupbuilder.groovy
Created February 14, 2014 11:30
Micro-Benchmarks for the MarkupTemplateEngine in Groovy
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
@Grab('org.freemarker:freemarker:2.3.9')
import groovy.text.markup.MarkupTemplateEngine
import freemarker.template.*
MarkupTemplateEngine engine = new MarkupTemplateEngine()
def mkpTemplate1 = engine.createTemplate '''
html {
body('It works!')
}
@melix
melix / Beurk.java
Last active January 4, 2016 19:39
Lambda and checked exceptions
/*
* Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software