Skip to content

Instantly share code, notes, and snippets.

Jim White jimwhite

View GitHub Profile
@jimwhite
jimwhite / MultipleIteratorDemo.groovy
Last active Aug 29, 2015
PoC for iterating over multiple collections in Groovy
View MultipleIteratorDemo.groovy
// Example of an iterator that works over a sequence of multiple collections.
def a = [1, 2]
def b = [3, 4]
def c = new CollectionSequence(a, b)
// The elements of the collections look like one concatenated list.
println (c as List)
// Mutating the elements of one of the collections changes what we get.
@jimwhite
jimwhite / SomeBean.groovy
Last active Aug 29, 2015
Example of using a BeanInfo class for custom getter/setter names for Groovy properties.
View SomeBean.groovy
class SomeBean {
private String _memo
String foo() { _memo }
void foo(String v) { _memo = v }
public static void main(String[] args) {
SomeBean bean = new SomeBean();
bean.foo = 'bar'
println bean.foo
View centos_python_env_setup
#!/bin/bash
# Source: http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/
# Install stuff #
#################
# Install development tools and some misc. necessary packages
yum -y groupinstall "Development tools"
yum -y install tar python-setuptools # Not included in Minimal group and Base is huge.
View Foo.java
package org.ifcx.snippets;
/**
* Created by jim on 9/13/14.
*/
public class Foo {
protected int x;
int getX() { return x + 1; }
}
View StringTemplateExample.groovy
def dat = ['NCC_TEST': [1234, 1345, 1232, 1133, 1100], 'NCC_TEST2': [9999,8765, 4321]]
println """update tablename set IsEnriched = 999 where ${
dat.collect { name, serials ->
"(ServerName = '$name' and ServerSerial in (${ serials.join(', ') }))"
}.join(" or ")
}"""
@jimwhite
jimwhite / SyntheticClassTest.groovy
Last active Aug 29, 2015
Synthetic class in Groovy using compiler API.
View SyntheticClassTest.groovy
// Fixed synthetic class snippet originally from <anonimista@outlook.com>.
import groovyjarjarasm.asm.Opcodes
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*
import org.codehaus.groovy.control.*
import org.codehaus.groovy.tools.GroovyClass
CompilationUnit compilationUnit = new CompilationUnit()
View CachedEvaluation.groovy
def evaluator = new CachingEvaluator()
assert evaluator.evaluate("1 + 2 * 3") == 7
assert evaluator.evaluate("1 + 2 * ${3}") == 7
assert evaluator.evaluate(new String("1 + 2 * 3")) == 7
assert evaluator.compiledScriptClasses.size() == 1
class CachingEvaluator {
GroovyShell shell = new GroovyShell()
View RedirectedScriptOutput.groovy
// Bind 'out' variable to redirect script printing methods.
out = new StringWriter()
// Child script inherits our bindings, including 'out'.
// def result = evaluate(new File('TargetScript.groovy'))
def result = evaluate '''
def a = 42
println "The answer is $a."
a ** 2
'''
View ThreadLocalScript.java
package groovyx.cli;
import groovy.lang.Binding;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import java.util.HashMap;
import java.util.Map;
/**
@jimwhite
jimwhite / GatherClassBytecode.groovy
Last active Aug 29, 2015
A GroovyClassLoader that gathers up the bytecode that defines the classes it has compiled.
View GatherClassBytecode.groovy
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.SourceUnit
class MyGroovyClassLoader extends GroovyClassLoader {
MyGroovyClassLoader(ClassLoader parent) { super(parent) }
Map bytecodes = [:]
class MyClassCollector extends GroovyClassLoader.ClassCollector {
protected MyClassCollector(GroovyClassLoader.InnerLoader cl, CompilationUnit unit, SourceUnit su) {
You can’t perform that action at this time.