Java Reflection Method Name getMethod(String name, String methodDesc, Class actual)

Here you can find the source of getMethod(String name, String methodDesc, Class actual)

Description

get Method

License

Apache License

Declaration

public static Method getMethod(String name, String methodDesc, Class actual) 

Method Source Code

//package com.java2s;
/*//from  www . j  a  va  2 s  . co m
 * Copyright 2016, Stuart Douglas, and individual contributors as indicated
 * by the @authors tag.
 *
 * 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.ArrayList;
import java.util.List;

public class Main {
    public static Method getMethod(String name, String methodDesc, Class actual) {
        try {
            return actual.getMethod(name, argumentStringToClassArray(methodDesc, actual));
        } catch (Exception e) {
            // this should not happen
            throw new RuntimeException(e);
        }

    }

    /**
     * returns an array of class types based on the method parameters this allows
     * getMethod to be called based on descriptor data We also pass the class
     * that the method We assume the descriptor is well formed
     *
     */
    public static Class<?>[] argumentStringToClassArray(String methodDescriptor, Class<?> methodClass)
            throws ClassNotFoundException {
        int i = 1; // char 0 is a '('
        List<Class<?>> classes = new ArrayList<Class<?>>();
        int arraystart = -1;
        while (methodDescriptor.charAt(i) != ')') {
            Class<?> type = null;
            if (methodDescriptor.charAt(i) == '[') {
                if (arraystart == -1) {
                    arraystart = i;
                }
            } else {

                if (methodDescriptor.charAt(i) == 'L') {
                    i++;
                    int start = i;
                    while (methodDescriptor.charAt(i) != ';') {
                        ++i;
                    }
                    if (arraystart == -1) {
                        String className = methodDescriptor.substring(start, i).replace('/', '.');
                        try {
                            type = methodClass.getClassLoader().loadClass(className);
                        } catch (ClassNotFoundException e) {
                            type = Class.forName(className);
                        }
                    } else {
                        // apparently the array class syntax for
                        // getting an array class is the somewhat
                        // retarded Class.forName("[Ljava.lang.String;")
                        // a weird mix of internal and external formats
                        String className = methodDescriptor.substring(arraystart, i + 1).replace("/", ".");

                        try {
                            type = methodClass.getClassLoader().loadClass(className);
                        } catch (ClassNotFoundException e) {
                            type = Class.forName(className);
                        }
                    }

                } else {

                    if (arraystart == -1) {
                        type = primitiveType(methodDescriptor.charAt(i));
                    } else {
                        String className = methodDescriptor.substring(arraystart, i + 1);
                        try {
                            type = methodClass.getClassLoader().loadClass(className);
                        } catch (ClassNotFoundException e) {
                            type = Class.forName(className);
                        }
                    }

                }

                arraystart = -1;
                classes.add(type);
            }
            ++i;
        }
        Class<?>[] ret = new Class[classes.size()];
        for (i = 0; i < ret.length; ++i) {
            ret[i] = classes.get(i);
        }
        return ret;

    }

    static Class<?> primitiveType(char c) {
        switch (c) {
        case 'V':
            return void.class;

        case 'B':
            return byte.class;

        case 'C':
            return char.class;

        case 'D':
            return double.class;

        case 'F':
            return float.class;

        case 'I':
            return int.class;

        case 'J':
            return long.class;

        case 'S':
            return short.class;

        case 'Z':
            return boolean.class;

        }
        return null;
    }
}

Related

  1. getMethod(String methodName, Class[] paramTypes, Class targetClass)
  2. getMethod(String methodName, Class[] params)
  3. getMethod(String methodName, Method method, boolean fromComponentType)
  4. getMethod(String name, Class pojoClass)
  5. getMethod(String name, Method method)
  6. getMethod(String strMethodPrefix, A instance, String strAttributeName, Class clazz)
  7. getMethodAsAccessible(String methodName, Class clazz)
  8. getMethodByName(Class clazz, String methodName)
  9. getMethodByName(Class aClass, String methodName, Class... params)