Java Reflection Method Signature getMethod(Signature signature)

Here you can find the source of getMethod(Signature signature)

Description

Returns the reference of the Method of the advice.

License

Apache License

Parameter

Parameter Description
signature The advice signature.

Exception

Parameter Description
Exception If it's not possible to invoke the private method in the JVM.

Return

The method of the advice. It will be null if the joinpoint's is not a .

Declaration

public static Method getMethod(Signature signature) throws Exception 

Method Source Code

//package com.java2s;
/**/*from  w w  w  .j  av  a  2 s  .  c  o m*/
 * Copyright 2013 Contributors
 *
 *    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.
 *
 *    Contributors:
 *          Alessandro Ferreira Leite - the initial implementation.
 */

import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;

import org.aspectj.lang.reflect.MethodSignature;

public class Main {
    /**
     * Returns the reference of the {@link Method} of the advice.
     * 
     * @param signature
     *            The advice signature.
     * @return The method of the advice. It will be <code>null</code> if the joinpoint's {@link Signature} is not a {@link MethodSignature}.
     * @throws Exception
     *             If it's not possible to invoke the private method in the JVM.
     */
    public static Method getMethod(Signature signature) throws Exception {
        if (signature instanceof MethodSignature) {
            Method m = signature.getClass().getDeclaredMethod("getMethod");
            m.setAccessible(true);
            return (Method) m.invoke(signature);
        }
        return null;
    }

    /**
     * Returns the reference of the {@link Method} of the advice.
     * 
     * @param joinPoint
     *            The joinpoint reference.
     * @return The method of the advice. It's never <code>null</code>.
     * @throws Exception
     *             If it's not possible to invoke the private method in the JVM.
     */
    public static Method getMethod(ProceedingJoinPoint joinPoint) throws Exception {
        return getMethod(joinPoint.getSignature());
    }
}

Related

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