Code example for Method

Methods: isAccessiblegetDeclaringClassgetModifiersgetParameterTypesgetReturnType

0
            throw msg.nullParameter("method"); 
        } 
        this.method = method;
        this.interceptorInstance = interceptorInstance;
        checkMethodType(interceptorInstance);
        withContext = method.getParameterTypes().length == 1;
    } 
 
    /** 
     * Construct a new instance.  The given method should be a proper interceptor method; otherwise invocation may fail. 
     * 
     * @param interceptorInstance the interceptor object instance 
     * @param method the interceptor method 
     */ 
    public MethodInterceptor(final Object interceptorInstance, final Method method) {
        this(interceptorInstance, method, false);
    } 
 
    /** {@inheritDoc} */ 
    public Object processInvocation(final InterceptorContext context) throws Exception {
        try { 
            Method method = this.method;
            if (withContext) {
                if (changeMethod) {
                    final Method oldMethod = context.getMethod();
                    context.setMethod(method);
                    try { 
                        return method.invoke(interceptorInstance, context.getInvocationContext());
                    } finally { 
                        context.setMethod(oldMethod);
                    } 
                } else { 
                    return method.invoke(interceptorInstance, context.getInvocationContext());
                } 
            } else { 
                method.invoke(interceptorInstance, null);
                return context.proceed();
            } 
        } catch (IllegalAccessException e) {
            final IllegalAccessError n = new IllegalAccessError(e.getMessage());
            n.setStackTrace(e.getStackTrace());
            throw n;
        } catch (InvocationTargetException e) {
            throw Interceptors.rethrow(e.getCause());
        } 
    } 
 
    private void checkMethodType(final Object interceptorInstance) {
        final Method method = this.method;
        if (! method.getDeclaringClass().isInstance(interceptorInstance)) {
            throw msg.wrongInterceptorType(); 
        } 
        final int modifiers = method.getModifiers();
        if (! Modifier.isPublic(modifiers) && ! method.isAccessible()) {
            throw msg.interceptorInaccessible(); 
        } 
        final Class<?>[] parameterTypes = method.getParameterTypes();
        final int length = parameterTypes.length;
        if (length > 1) {
            throw msg.interceptorTargetOneParam(); 
        } 
        // allow contravariant parameter types 
        if (length == 1 && ! parameterTypes[0].isAssignableFrom(InvocationContext.class)) {
            throw msg.interceptorTargetAssignableFrom(InvocationContext.class); 
        } 
        // allow covariant return types (but not primitives, which are not Objects); also allow void for lifecycle interceptors 
        final Class<?> returnType = method.getReturnType();
        if (returnType != void.class && ! Object.class.isAssignableFrom(returnType)) {
            throw msg.interceptorReturnObject(); 
        } 
    } 
}