| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ReflectionBeanMethod |
|
| 1.25;1.25 | ||||
| ReflectionBeanMethod$1 |
|
| 1.25;1.25 | ||||
| ReflectionBeanMethod$MakeMethodAccessible |
|
| 1.25;1.25 |
| 1 | package org.jtheque.core.managers.module.beans; | |
| 2 | ||
| 3 | import org.jtheque.core.managers.Managers; | |
| 4 | import org.jtheque.core.managers.beans.IBeansManager; | |
| 5 | import org.jtheque.core.managers.log.ILoggingManager; | |
| 6 | ||
| 7 | import java.lang.reflect.Method; | |
| 8 | import java.security.AccessController; | |
| 9 | import java.security.PrivilegedAction; | |
| 10 | ||
| 11 | /* | |
| 12 | * This file is part of JTheque. | |
| 13 | * | |
| 14 | * JTheque is free software: you can redistribute it and/or modify | |
| 15 | * it under the terms of the GNU General Public License as published by | |
| 16 | * the Free Software Foundation, either version 3 of the License. | |
| 17 | * | |
| 18 | * JTheque is distributed in the hope that it will be useful, | |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 21 | * GNU General Public License for more details. | |
| 22 | * | |
| 23 | * You should have received a copy of the GNU General Public License | |
| 24 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 25 | */ | |
| 26 | ||
| 27 | /** | |
| 28 | * A bean method who invoke a method by reflection. | |
| 29 | * | |
| 30 | * @author Baptiste Wicht | |
| 31 | */ | |
| 32 | public final class ReflectionBeanMethod implements BeanMethod { | |
| 33 | private final String beanName; | |
| 34 | private final String methodName; | |
| 35 | ||
| 36 | /** | |
| 37 | * Construct a new ReflectionBeanMethod. | |
| 38 | * | |
| 39 | * @param beanName The name of the bean to invoke the method from. | |
| 40 | * @param methodName The name of the method. | |
| 41 | */ | |
| 42 | public ReflectionBeanMethod(String beanName, String methodName) { | |
| 43 | 0 | super(); |
| 44 | ||
| 45 | 0 | this.beanName = beanName; |
| 46 | 0 | this.methodName = methodName; |
| 47 | 0 | } |
| 48 | ||
| 49 | @Override | |
| 50 | public void run() { | |
| 51 | 0 | Object bean = Managers.getManager(IBeansManager.class).getBean(beanName); |
| 52 | ||
| 53 | try { | |
| 54 | 0 | final Method method = bean.getClass().getDeclaredMethod(methodName); |
| 55 | ||
| 56 | 0 | AccessController.doPrivileged(new MakeMethodAccessible(method)); |
| 57 | ||
| 58 | 0 | method.invoke(bean); |
| 59 | 0 | } catch (Exception e) { |
| 60 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 61 | 0 | } |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * A privileged action to make a method accessible. | |
| 66 | * | |
| 67 | * @author Baptiste Wicht | |
| 68 | */ | |
| 69 | 0 | private static final class MakeMethodAccessible implements PrivilegedAction<Object> { |
| 70 | private final Method method; | |
| 71 | ||
| 72 | /** | |
| 73 | * Construct a new MakeMethodAccessible. | |
| 74 | * | |
| 75 | * @param method The method to make accessible. | |
| 76 | */ | |
| 77 | private MakeMethodAccessible(Method method) { | |
| 78 | 0 | super(); |
| 79 | ||
| 80 | 0 | this.method = method; |
| 81 | 0 | } |
| 82 | ||
| 83 | @Override | |
| 84 | public Object run() { | |
| 85 | 0 | method.setAccessible(true); |
| 86 | ||
| 87 | 0 | return null; |
| 88 | } | |
| 89 | } | |
| 90 | } |