Java Reflection Method Name getMethodName()

Here you can find the source of getMethodName()

Description

Gets the name of the method that calls this method.

License

LGPL

Return

the name of the method that calls this method

Declaration

public static String getMethodName() 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//w  w w  .  j  a  va2 s .  c o m
     * Gets the name of the method that calls this method. Equal to calling
     * {@link #getMethodName(boolean, boolean, java.lang.Object[])} with <code>false</code> as value for the <code>
     * canonicalName</code> and <code>pringValues</code> parameter and <code>null</code> as value for the <code>
     * paramInstances</code> parameter.
     *
     * @return  the name of the method that calls this method
     *
     * @see     #getMethodName(boolean, boolean, java.lang.Object[])
     */
    public static String getMethodName() {
        return doGetMethodName(false, false, (Object[]) null);
    }

    /**
     * Gets the name of the method that calls this method. Equal to calling
     * {@link #getMethodName(boolean, boolean, java.lang.Object[])} with <code>false</code> as value for the <code>
     * canonicalName</code> and <code>pringValues</code> parameter.
     *
     * @param   paramInstances  the parameters that shall be printed with this method name
     *
     * @return  the name of the method that calls this method
     *
     * @see     #getMethodName(boolean, boolean, java.lang.Object[])
     */
    public static String getMethodName(final Object... paramInstances) {
        return doGetMethodName(false, false, paramInstances);
    }

    /**
     * Gets the name of the method that calls this method. Equal to calling
     * {@link #getMethodName(boolean, boolean, java.lang.Object[])} with <code>false</code> as value for the <code>
     * pringValues</code> parameter.
     *
     * @param   canonicalName   whether the class names of the <code>paramInstances</code> shall be canonical or simple
     * @param   paramInstances  the parameters that shall be printed with this method name
     *
     * @return  the name of the method that calls this method
     *
     * @see     #getMethodName(boolean, boolean, java.lang.Object[])
     */
    public static String getMethodName(final boolean canonicalName, final Object... paramInstances) {
        return doGetMethodName(canonicalName, false, paramInstances);
    }

    /**
     * Gets the name of the method that calls this method. If optionally provided with a list of parameters it prints
     * their type ({@link Class}), too, depending on the <code>canonicalName</code> flag, e.g.<br/>
     * <br/>
     * <code>myShinyMethod(String,boolean)</code> if <code>canonicalName</code> is <code>false</code>.<br/>
     * <br/>
     * If the <code>printValues</code> is <code>true</code> it additionally appends the results of the <code>
     * toString()</code> operations of the single methods like so:<br/>
     * <br/>
     * <code>myShinyMethod(String{myString})</code>.<br/>
     * <br/>
     * <b>NOTE:</b>This should mainly be used for debugging purposes as it is a rather slow implementation.
     *
     * @param   canonicalName   whether the class names of the <code>paramInstances</code> shall be canonical or simple
     * @param   printValues     whether the actual values of the <code>paramInstances</code> shall be printed
     * @param   paramInstances  the parameters that shall be printed with this method name
     *
     * @return  the name of the method that calls this method
     */
    public static String getMethodName(final boolean canonicalName, final boolean printValues,
            final Object... paramInstances) {
        return doGetMethodName(canonicalName, printValues, paramInstances);
    }

    /**
     * Actually does the build of the method name according to description at
     * {@link #getMethodName(boolean, boolean, java.lang.Object[]) }.
     *
     * @param   canonicalName   DOCUMENT ME!
     * @param   printValues     DOCUMENT ME!
     * @param   paramInstances  DOCUMENT ME!
     *
     * @return  DOCUMENT ME!
     */
    private static String doGetMethodName(final boolean canonicalName, final boolean printValues,
            final Object... paramInstances) {
        final StringBuilder sb = new StringBuilder(Thread.currentThread().getStackTrace()[3].getMethodName());

        sb.append('(');
        if (paramInstances != null) {
            for (final Object o : paramInstances) {
                final String cName = o.getClass().getCanonicalName();
                if (canonicalName) {
                    sb.append(cName);
                } else {
                    sb.append(cName.substring(cName.lastIndexOf('.') + 1));
                }

                if (printValues) {
                    sb.append('{');
                    sb.append(o.toString());
                    sb.append('}');
                }

                sb.append(',');
            }
        }

        if (',' == sb.charAt(sb.length() - 1)) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(')');

        return sb.toString();
    }
}

Related

  1. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  2. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  3. getMethodIncludingSuperClass(Class clazz, String methodName)
  4. getMethodInternal(Class clazz, String methodName)
  5. getMethodName()
  6. getMethodName(final int depth)
  7. getMethodName(Method method)
  8. getMethodName(Method method)
  9. getMethodName(Method method)