001    // Copyright 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.ioc;
016    
017    /**
018     * A method invocation passed to a {@link org.apache.tapestry5.ioc.MethodAdvice}.
019     */
020    public interface Invocation
021    {
022        /**
023         * Returns the name of the method being invoked.
024         */
025        String getMethodName();
026    
027        /**
028         * Returns the type of the method result, which may be a primitive type (i.e., int.class) or even void
029         * (void.class).
030         */
031        Class getResultType();
032    
033        /**
034         * Returns the number of parameters passed to the method.
035         */
036        int getParameterCount();
037    
038        /**
039         * Returns the type of the parameter at the index.
040         */
041        Class getParameterType(int index);
042    
043        /**
044         * Returns the indicated parameter (may return null if the parameter is null).
045         */
046        Object getParameter(int index);
047    
048        /**
049         * Replaces a parameter in the invocation.
050         *
051         * @param index        of parameter to update
052         * @param newParameter new parameter value (may be null)
053         */
054        void override(int index, Object newParameter);
055    
056        /**
057         * Proceed with the invocation of the advised method.  If the invocation results in a <em>runtime</em> exception,
058         * that is thrown. A checked exception is detected by invoking {@link #isFail()}.
059         */
060        void proceed();
061    
062        /**
063         * If true, then the proceeded invocation threw a checked exception.
064         */
065        boolean isFail();
066    
067        /**
068         * After invoking {@link #proceed()}, used to obtain the thrown (checked) exception, if assignable to the provided
069         * type.
070         *
071         * @param throwableClass the type of exception to match
072         * @return the exception, if the proceeded invocation threw a checked exception, and the exception is assignable to
073         *         the provided type.  In other cases, null is returned.
074         */
075        <T extends Throwable> T getThrown(Class<T> throwableClass);
076    
077        /**
078         * Overrides the thrown exception. The passed exception should be a checked exception of the method. Note that for
079         * runtime exceptions, or even {@link Error}s, those can just be thrown. Sets the fail flag.
080         *
081         * @param thrown
082         * @throws IllegalArgumentException if thrown is null, or not a declared exception of the method
083         */
084        void overrideThrown(Exception thrown);
085    
086        /**
087         * The return value after {@link #proceed()}, which may be null.
088         */
089        Object getResult();
090    
091        /**
092         * Overrides the result. Clears the thrown exception (if any).
093         */
094        void overrideResult(Object newResult);
095    }