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

Groovy example source code file (TupleListTest.java)

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

Java - Groovy tags/keywords

acc_public, acc_public, blockstatement, classnode, constantexpression, constantexpression, exception, exception, listexpression, mapexpression, statement, tuplelisttest, variableexpression, variableexpression

The Groovy TupleListTest.java source code

/*
 * Copyright 2003-2011 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
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.classgen;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.ForStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.codehaus.groovy.syntax.Token;

/**
 * @author <a href="mailto:james@coredevelopers.net">James Strachan
 * @version $Revision: 21496 $
 */
public class TupleListTest extends TestSupport {

    public void testIterateOverList() throws Exception {
        ListExpression listExpression = new ListExpression();
        listExpression.addExpression(new ConstantExpression("a"));
        listExpression.addExpression(new ConstantExpression("b"));
        listExpression.addExpression(new ConstantExpression("c"));
        listExpression.addExpression(new ConstantExpression("a"));
        listExpression.addExpression(new ConstantExpression("b"));
        listExpression.addExpression(new ConstantExpression("c"));
        assertIterate("iterateOverList", listExpression);
    }

    public void testIterateOverMap() throws Exception {
        MapExpression mapExpression = new MapExpression();
        mapExpression.addMapEntryExpression(new ConstantExpression("a"), new ConstantExpression("x"));
        mapExpression.addMapEntryExpression(new ConstantExpression("b"), new ConstantExpression("y"));
        mapExpression.addMapEntryExpression(new ConstantExpression("c"), new ConstantExpression("z"));
        assertIterate("iterateOverMap", mapExpression);
    }

    protected void assertIterate(String methodName, Expression listExpression) throws Exception {
        ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
        classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));

        Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));

        BlockStatement block = new BlockStatement();
        block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
        block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement));
        classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));

        Class fooClass = loadClass(classNode);
        assertTrue("Loaded a new class", fooClass != null);

        Object bean = fooClass.newInstance();
        assertTrue("Managed to create bean", bean != null);

        System.out.println("################ Now about to invoke method");

        try {
            InvokerHelper.invokeMethod(bean, methodName, null);
        }
        catch (InvokerInvocationException e) {
            System.out.println("Caught: " + e.getCause());
            e.getCause().printStackTrace();
            fail("Should not have thrown an exception");
        }
        System.out.println("################ Done");
    }

}

Other Groovy examples (source code examples)

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

new blog posts

 

Copyright 1998-2014 Alvin Alexander, alvinalexander.com
All Rights Reserved.