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

Groovy example source code file (MethodCallExpressionTest.java)

This example Groovy source code file (MethodCallExpressionTest.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

arraylist, asttest, classcodevisitorsupport, classcodevisitorsupport, exception, list, methodcallexpressiontest, methodcallexpressiontest, methodcallvisitor, modulenode, modulenode, sourceunit, sourceunit, util

The Groovy MethodCallExpressionTest.java source code

package org.codehaus.groovy.ast;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;


/**
 * Tests the MethodCallExpression
 * 
 * @author <a href="mailto:martin.kempf@gmail.com">Martin Kempf
 *
 */

public class MethodCallExpressionTest extends ASTTest {
    
    private boolean isImplicitThis;
    
    /*
     * To make sure the MethodCallExpression is visited and we do not test against
     * the default value of isImplicitThis
     */
    private boolean visited; 
    
    private List<String> defaultScriptMethods = new ArrayList();
    
    private ClassCodeVisitorSupport MethodCallVisitor = new ClassCodeVisitorSupport() {
        
        public void visitMethodCallExpression(MethodCallExpression methodCall) {
            if (defaultScriptMethods.contains(methodCall.getMethodAsString())) {
                visited = true;
                isImplicitThis = methodCall.isImplicitThis();
            }
        }
        
        protected SourceUnit getSourceUnit() {
            return null;
        }
    };
    
    public MethodCallExpressionTest() {
        defaultScriptMethods.add("substring");
        defaultScriptMethods.add("println");
    }
    
    protected void setUp() throws Exception {
        visited = false;
    }
    
    public void testIsImplicitThisOnObject() {
        ModuleNode root = getAST("string.substring(2)", Phases.SEMANTIC_ANALYSIS);
        MethodCallVisitor.visitClass(root.getClasses().get(0));
        assertTrue(visited);
        assertFalse(isImplicitThis);
    }
    
    public void testIsImplicitThisExplicitThis() {
        ModuleNode root = getAST("this.println()", Phases.SEMANTIC_ANALYSIS);
        MethodCallVisitor.visitClass(root.getClasses().get(0));
        assertTrue(visited);
        assertFalse(isImplicitThis);
    }
    
    public void testIsImplicitThisNoObject() {
        ModuleNode root = getAST("println()", Phases.SEMANTIC_ANALYSIS);
        MethodCallVisitor.visitClass(root.getClasses().get(0));
        assertTrue(visited);
        assertTrue(isImplicitThis);
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

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