001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.aspectj;
018
019import java.io.Serializable;
020import java.lang.reflect.Method;
021
022import org.aopalliance.intercept.MethodInterceptor;
023import org.aopalliance.intercept.MethodInvocation;
024
025import org.springframework.aop.AfterAdvice;
026
027/**
028 * Spring AOP advice wrapping an AspectJ after-throwing advice method.
029 *
030 * @author Rod Johnson
031 * @since 2.0
032 */
033@SuppressWarnings("serial")
034public class AspectJAfterThrowingAdvice extends AbstractAspectJAdvice
035                implements MethodInterceptor, AfterAdvice, Serializable {
036
037        public AspectJAfterThrowingAdvice(
038                        Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
039
040                super(aspectJBeforeAdviceMethod, pointcut, aif);
041        }
042
043
044        @Override
045        public boolean isBeforeAdvice() {
046                return false;
047        }
048
049        @Override
050        public boolean isAfterAdvice() {
051                return true;
052        }
053
054        @Override
055        public void setThrowingName(String name) {
056                setThrowingNameNoCheck(name);
057        }
058
059        @Override
060        public Object invoke(MethodInvocation mi) throws Throwable {
061                try {
062                        return mi.proceed();
063                }
064                catch (Throwable ex) {
065                        if (shouldInvokeOnThrowing(ex)) {
066                                invokeAdviceMethod(getJoinPointMatch(), null, ex);
067                        }
068                        throw ex;
069                }
070        }
071
072        /**
073         * In AspectJ semantics, after throwing advice that specifies a throwing clause
074         * is only invoked if the thrown exception is a subtype of the given throwing type.
075         */
076        private boolean shouldInvokeOnThrowing(Throwable ex) {
077                return getDiscoveredThrowingType().isAssignableFrom(ex.getClass());
078        }
079
080}