Java Reflection Method Name getMethodNames(Class cls, boolean insertDefaultValues)

Here you can find the source of getMethodNames(Class cls, boolean insertDefaultValues)

Description

Returns a Vector of String s that are the full Method names, with parameters for the given class.

License

Open Source License

Parameter

Parameter Description
insertDefaultValues if true then default values will be used instead of the paramter class names for primitive types and strings. For example the empty string is inserted for strings, and 0 is inserted for numbers.

Return

Vector of String method names.

Declaration

public static Vector getMethodNames(Class cls,
        boolean insertDefaultValues) 

Method Source Code

//package com.java2s;

import java.lang.reflect.Method;

import java.util.Vector;

public class Main {
    /**/*from  ww w . j a va 2  s  . com*/
     * Returns a {@link Vector} of {@link String}s that are the full
     * {@link Method} names, with parameters for the given class.
     * @param insertDefaultValues if true then default values will be used instead of the paramter class names
     *  for primitive types and strings. For example the empty string is inserted for strings, and 0 is inserted for numbers.
     * @return Vector of String method names.
     */
    public static Vector getMethodNames(Class cls,
            boolean insertDefaultValues) {
        Method[] methodsArray = cls.getMethods();
        Vector/*<String>*/methods = new Vector/*<String>*/(
                methodsArray.length);
        for (int i = 0; i < methodsArray.length; i++) {
            String term = getMethodName(methodsArray[i],
                    insertDefaultValues);
            methods.add(term);
        }
        return methods;
    }

    /**
     * Returns the string name and parameters for the {@link Method}.
     * @param insertDefaultValues if true then default values will be used instead of the paramter class names
     *  for primitive types and strings. For example the empty string is inserted for strings, and 0 is inserted for numbers.
     */
    public static String getMethodName(Method method,
            boolean insertDefaultValues) {
        Class[] params = method.getParameterTypes();
        StringBuffer term = new StringBuffer();
        term.append(method.getName());
        term.append("(");
        if (params.length > 0) {
            for (int j = 0; j < params.length; j++) {
                if (j > 0) {
                    term.append(", ");
                }
                Class cls = params[j];
                String className = cls.getName();
                boolean set = false;
                if (insertDefaultValues) {
                    set = true;
                    if (cls == String.class) {
                        term.append("\"\"");
                    } else if (cls == boolean.class) {
                        term.append("true");
                    } else if ((cls == long.class) || (cls == int.class)
                            || (cls == short.class) || (cls == byte.class)) {
                        term.append("0");
                    } else if ((cls == float.class)
                            || (cls == double.class)) {
                        term.append("0.0");
                    } else if (cls == char.class) {
                        term.append("''");
                    } else {
                        set = false;
                    }
                }
                if (!set) {
                    int dot = className.lastIndexOf(".");
                    if (dot >= 0) {
                        className = className.substring(dot + 1);
                    }
                    // special case for arrays
                    if (className.endsWith(";")) {
                        className = className.substring(0,
                                className.length() - 1)
                                + "[]";
                    }
                    term.append(className);
                }
            }
        }
        term.append(")");
        return term.toString();
    }
}

Related

  1. getMethodName(Method method, boolean useSegment, String segment, String value)
  2. getMethodName(String prefix, String fieldName)
  3. getMethodNameAndDescriptor(Method m)
  4. getMethodNameList(Class theClass)
  5. getMethodNameMinusGet(Method aMethod)
  6. getMethodNames(Class cls)
  7. getMethodNameWithClassName(Method a_Method)
  8. getMethodOnlyByName(Class c, String methodName)
  9. getMethodOrNull(Class cls, String name, Class[] args)