001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.aspects;
016    
017    import com.liferay.portal.kernel.util.ReflectionUtil;
018    
019    import org.aspectj.lang.ProceedingJoinPoint;
020    import org.aspectj.lang.annotation.Around;
021    import org.aspectj.lang.annotation.Aspect;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    @Aspect
027    public class ReflectionUtilAdvice {
028    
029            public static void setDeclaredFieldThrowable(
030                    Throwable declaredFieldThrowable) {
031    
032                    _declaredFieldThrowable = declaredFieldThrowable;
033            }
034    
035            public static void setDeclaredMethodThrowable(
036                            Throwable declaredMethodThrowable)
037                    throws ClassNotFoundException {
038    
039                    Class.forName(
040                            ReflectionUtil.class.getName(), true,
041                            ReflectionUtil.class.getClassLoader());
042    
043                    _declaredMethodThrowable = declaredMethodThrowable;
044            }
045    
046            @Around(
047                    "execution(public static java.lang.reflect.Field " +
048                            "com.liferay.portal.kernel.util.ReflectionUtil." +
049                                    "getDeclaredField(Class, String))"
050            )
051            public Object getDeclaredField(ProceedingJoinPoint proceedingJoinPoint)
052                    throws Throwable {
053    
054                    if (_declaredFieldThrowable != null) {
055                            throw _declaredFieldThrowable;
056                    }
057    
058                    return proceedingJoinPoint.proceed();
059            }
060    
061            @Around(
062                    "execution(public static java.lang.reflect.Method " +
063                            "com.liferay.portal.kernel.util.ReflectionUtil." +
064                                    "getDeclaredMethod(Class, String, Class...))"
065            )
066            public Object getDeclaredMethod(ProceedingJoinPoint proceedingJoinPoint)
067                    throws Throwable {
068    
069                    if (_declaredMethodThrowable != null) {
070                            throw _declaredMethodThrowable;
071                    }
072    
073                    return proceedingJoinPoint.proceed();
074            }
075    
076            private static Throwable _declaredFieldThrowable;
077            private static Throwable _declaredMethodThrowable;
078    
079    }