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

Groovy example source code file (MethodTest.java)

This example Groovy source code file (MethodTest.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, classnode, constantexpression, constructornode, methodnode, methodnode, methodtest, object, object, result, returnstatement, statement, statement

The Groovy MethodTest.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,
 * 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.ConstantExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.runtime.InvokerHelper;

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

    public void testMethods() throws Exception {
        ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

        Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
        Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
        Statement emptyStatement = new BlockStatement();

        classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
        classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));

        classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
        classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));

        classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));

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

        Object bean = fooClass.newInstance();
        assertTrue("Created instance of class: " + bean, bean != null);

        assertCallMethod(bean, "a", "calledA");
        assertCallMethod(bean, "b", "calledB");
        assertCallMethod(bean, "noReturnMethodA", null);
        assertCallMethod(bean, "noReturnMethodB", null);
        assertCallMethod(bean, "c", null);

    protected void assertCallMethod(Object object, String method, Object expected) {
        Object value = InvokerHelper.invokeMethod(object, method, new Object[0]);
        assertEquals("Result of calling method: " + method + " on: " + object + " with empty list", expected, value);

        value = InvokerHelper.invokeMethod(object, method, null);
        assertEquals("Result of calling method: " + method + " on: " + object + " with null", expected, value);

Other Groovy examples (source code examples)

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

new blog posts


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