alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ReflectionMethodInvoker.java)

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

class, class, method, method, object, object, reflection, reflectionmethodinvoker, reflectionmethodinvoker, string, throwable

The Groovy ReflectionMethodInvoker.java source code

/*
 * Copyright 2003-2007 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.runtime;

import java.lang.reflect.Method;


/**
 * Utility class to call methods through reflection, and falls through using the <code>Invoker to call the method if it fails.
 * The class is particularly useful for Groovy classes implementing <code>GroovyInterceptable,
 * since it is not possible to call any method from this class,
 * because it is intercepted by the <code>invokeMethod() method.
 *
 * @author Guillaume Laforge
 */
public class ReflectionMethodInvoker {

    /**
     * Invoke a method through reflection.
     * Falls through to using the Invoker to call the method in case the reflection call fails..
     *
     * @param object the object on which to invoke a method
     * @param methodName the name of the method to invoke
     * @param parameters the parameters of the method call
     * @return the result of the method call
     */
    public static Object invoke(Object object, String methodName, Object[] parameters) {
        try {
            Class[] classTypes = new Class[parameters.length];
            for (int i = 0; i < classTypes.length; i++) {
                classTypes[i] = parameters[i].getClass();
            }
            Method method = object.getClass().getMethod(methodName, classTypes);
            return method.invoke(object, parameters);
        } catch (Throwable t) {
            return InvokerHelper.invokeMethod(object, methodName,  parameters);
        }
    }

}

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
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.