|
5 | 5 |
|
6 | 6 | package org.jboss.as.ejb3.component.stateful;
|
7 | 7 |
|
| 8 | +import java.lang.reflect.Field; |
8 | 9 | import java.lang.reflect.InvocationHandler;
|
9 | 10 |
|
10 | 11 | import org.jboss.as.ee.component.ProxyInvocationHandler;
|
11 | 12 | import org.jboss.invocation.proxy.ProxyFactory;
|
12 | 13 | import org.kohsuke.MetaInfServices;
|
13 | 14 | import org.wildfly.clustering.ee.Immutability;
|
| 15 | +import org.wildfly.security.ParametricPrivilegedAction; |
| 16 | +import org.wildfly.security.manager.WildFlySecurityManager; |
14 | 17 |
|
15 | 18 | /**
|
16 | 19 | * Immutability test for EJB proxies, whose serializable placeholders are immutable.
|
17 | 20 | * @author Paul Ferraro
|
18 | 21 | */
|
19 | 22 | @MetaInfServices(Immutability.class)
|
20 | 23 | public class StatefulSessionBeanImmutability implements Immutability {
|
| 24 | + private static final ParametricPrivilegedAction<InvocationHandler, Object> GET_INVOCATION_HANDLER = new ParametricPrivilegedAction<>() { |
| 25 | + @Override |
| 26 | + public InvocationHandler run(Object object) { |
| 27 | + // Since there is a low probability that this object is actually a SFSB proxy, avoid Class.getDeclaredField(...) which will typically throw/catch a NoSuchFieldException |
| 28 | + for (Field field : object.getClass().getDeclaredFields()) { |
| 29 | + if (field.getName().equals(ProxyFactory.INVOCATION_HANDLER_FIELD)) { |
| 30 | + field.setAccessible(true); |
| 31 | + try { |
| 32 | + return (InvocationHandler) field.get(object); |
| 33 | + } catch (IllegalAccessException e) { |
| 34 | + throw new IllegalStateException(e); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return null; |
| 39 | + } |
| 40 | + }; |
21 | 41 |
|
22 | 42 | @Override
|
23 | 43 | public boolean test(Object object) {
|
24 |
| - try { |
25 |
| - InvocationHandler handler = ProxyFactory.getInvocationHandlerStatic(object); |
26 |
| - return handler instanceof ProxyInvocationHandler; |
27 |
| - } catch (RuntimeException e) { |
28 |
| - return false; |
29 |
| - } |
| 44 | + InvocationHandler handler = WildFlySecurityManager.doUnchecked(object, GET_INVOCATION_HANDLER); |
| 45 | + return handler instanceof ProxyInvocationHandler; |
30 | 46 | }
|
31 | 47 | }
|
0 commit comments