Java Method Call invokeCallback(String callbackName, String callbackParam)

Here you can find the source of invokeCallback(String callbackName, String callbackParam)

Description

invoke Callback

License

Apache License

Declaration

public static void invokeCallback(String callbackName, String callbackParam) 

Method Source Code

//package com.java2s;
/*//www .j  a v  a 2s  .c o  m
 * *
 *    Copyright 2011 Baptiste Wicht & Fr?d?ric Bapst
 *
 *    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.
 *
 */

import java.lang.reflect.Method;
import java.util.regex.Pattern;

public class Main {
    private static final Class<?>[] CALLBACK_PARAM_TYPES = { String.class };
    private static final Pattern METHOD_SEPARATOR = Pattern.compile("/");

    public static void invokeCallback(String callbackName, String callbackParam) {
        int a = callbackName.lastIndexOf('/');
        if (a < 0) {
            throw new IllegalStateException("bad callback format (should be ab/cd/className/methName)");
        }

        String className = METHOD_SEPARATOR.matcher(callbackName.substring(0, a)).replaceAll(".");
        String methodName = callbackName.substring(a + 1);

        try {
            Class<?> clazz = Class.forName(className);
            Method method = clazz.getMethod(methodName, CALLBACK_PARAM_TYPES);
            method.invoke(null, callbackParam);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. invoke_ex(Object obj, String method, Class[] params, Object[] args)
  2. invokeAccessableMethodWithArguments(Object instance, String method, Object... arguments)
  3. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)
  4. invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)
  5. invokeAnUnwrapException(final Method method, final Object[] args, final Object target)
  6. invokeCmd(String className, String[] args)
  7. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  8. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  9. invokeDeclaredMethod(Object object, String methodName, Object... parameters)