Java Reflection Method Signature getMethodFromSignature(Class clazz, String methodSig)

Here you can find the source of getMethodFromSignature(Class clazz, String methodSig)

Description

Get method from signature

License

Apache License

Parameter

Parameter Description
clazz a parameter
methodSig a parameter

Exception

Parameter Description
ClassNotFoundException an exception
NoSuchMethodException an exception
SecurityException an exception

Declaration

public static Method getMethodFromSignature(Class<?> clazz, String methodSig)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException 

Method Source Code

//package com.java2s;
/*/*w w w.  j a  va2 s.  c om*/
 * Copyright (C) 2012-2018 Gregory Hedlund <https://www.phon.ca>
 * Copyright (C) 2012 Jason Gedge <http://www.gedge.ca>
 *
 * 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;

public class Main {
    /**
     * Get method from signature
     *
     * @param clazz
     * @param methodSig
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethodFromSignature(Class<?> clazz, String methodSig)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException {
        Method retVal = null;

        final String methodName = getMethodNameFromSignature(methodSig);
        final Class<?>[] paramTypes = getParametersFromSignature(methodSig);

        retVal = clazz.getMethod(methodName, paramTypes);

        return retVal;
    }

    /**
     * Get method name from signature
     *
     * @param sig
     * @return
     */
    public static String getMethodNameFromSignature(String sig) {
        return sig.substring(0, sig.indexOf('('));
    }

    /**
     * Get paramters from signature
     *
     * @param sig
     * @return
     * @throws ClassNotFoundException
     *
     */
    public static Class<?>[] getParametersFromSignature(String sig) throws ClassNotFoundException {
        Class<?> retVal[] = new Class<?>[0];
        final String paramString = sig.substring(sig.indexOf('(') + 1, sig.lastIndexOf(')')).trim();
        if (paramString.length() > 0) {
            final String[] paramClassNames = paramString.split(",");
            retVal = new Class<?>[paramClassNames.length];
            for (int i = 0; i < paramClassNames.length; i++) {
                final String paramClassName = paramClassNames[i];

                if (paramClassName.equals("int")) {
                    retVal[i] = int.class;
                } else if (paramClassName.equals("double")) {
                    retVal[i] = double.class;
                } else if (paramClassName.equals("float")) {
                    retVal[i] = float.class;
                } else {
                    retVal[i] = Class.forName(paramClassName.trim());
                }
            }
        }
        return retVal;
    }
}

Related

  1. getMethod(Class cls, String name, Class[] signature)
  2. getMethod(Signature signature)
  3. getMethodByFunctionSignature(Class clazz, String signature)
  4. getMethodSignature(Class[] paramTypes, Class retType)
  5. getMethodSignature(Method m)
  6. getMethodSignature(Method method)
  7. getMethodSignature(Method method)