| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (

This example Groovy source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

bytearrayoutputstream, bytearrayoutputstream, class, class, exception, groovylexer, io, object, object, printstream, reflection, sourcebuffer, string, string, unicodeescapingreader, visitor

The Groovy source code

package org.codehaus.groovy.antlr.treewalker;

import antlr.collections.AST;
import org.codehaus.groovy.antlr.AntlrASTProcessor;
import org.codehaus.groovy.antlr.SourceBuffer;
import org.codehaus.groovy.antlr.UnicodeEscapingReader;
import org.codehaus.groovy.antlr.parser.GroovyLexer;
import org.codehaus.groovy.antlr.parser.GroovyRecognizer;

import java.lang.reflect.Constructor;

public class TraversalTestHelper {
    public String traverse(String input, Class visitorClass) throws Exception {
        return traverse(input, visitorClass, null);

    // todo - the visitor doesn't always take PrintStreams as constructor params!  Could be a more reusable implementation than this...
    public String traverse(String input, Class visitorClass, Boolean extraParam) throws Exception {
        if (!Visitor.class.isAssignableFrom(visitorClass)) {
            throw new RuntimeException("Invalid class for traversal: " + visitorClass.getName());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GroovyRecognizer parser;
        SourceBuffer sourceBuffer = new SourceBuffer();
        UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
        GroovyLexer lexer = new GroovyLexer(unicodeReader);
        parser = GroovyRecognizer.make(lexer);
        String[] tokenNames = parser.getTokenNames();
        AST ast = parser.getAST();
        Class[] paramTypes;
        Object[] params;
        if (extraParam == null) {
            paramTypes = new Class[]{PrintStream.class, String[].class};
            params = new Object[]{new PrintStream(baos), tokenNames};
        } else {
            paramTypes = new Class[]{PrintStream.class, String[].class, Boolean.TYPE};
            params = new Object[]{new PrintStream(baos), tokenNames, extraParam};
        Constructor constructor = visitorClass.getConstructor(paramTypes);
        Visitor visitor = (Visitor) constructor.newInstance(params);
        AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
        return new String(baos.toByteArray());

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.