[jboss-user] [JBoss AOP] - Re: IllegalAccessException using reflection on private field

flavia.rainone@jboss.com do-not-reply at jboss.com
Wed May 23 17:08:13 EDT 2007


Hello,

I've taken a look at your problem and at the bug report you added to Jira (issue JBAOP-405).

The cause of your problem is not the fact that setAccessible doesn't take place. On the contrary, it does take place and has the desired effect (it allows calls to Field.get() and Field.set() methods). But, in your code, setting the field as accessible is unnecessary, since JBoss AOP will call a wrapper method it has generated instead of Field.set() and Field.get() methods. JBoss AOP will do this in order to trigger your TestAspect.

The problem here is that this wrapper is generated as a private method inside TestObject class, and therefore it is not accessible from ReflectionAspect, which is a bug.

While we take a look at the problem, you can intercept reflection calls using the other approach ReflectionAspect provides: to extend ReflectionAspect and override interceptFieldWrite and interceptFieldRead methods.


  | // TestAspect.java
  | package test;
  | 
  | public class TestAspect extends ReflectionAspect
  | {
  |    public Object interceptFieldRead(Invocation invocation, Field field, Object instance) throws Throwable
  |    {
  |       System.out.println(instance.getClass().getName() + "." + field.getName() + " being read on instance " + instance);
  |       return invocation.invokeNext();
  |    }
  | 
  |    public Object interceptFieldWrite(Invocation invocation, Field field, Object instance, Object arg) throws Throwable
  |    {
  |       System.out.println(instance.getClass().getName() + "." + field.getName() + " being set on instance " + instance + " with the following value \"" + arg + "\"");
  |       return invocation.invokeNext();
  |    }
  |  }
  | 

With the approach above, just replace your jboss-aop.xml file by the following:

  | <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  | <aop>
  |    <aspect name="MyReflectionAspect" class="MyReflectionAspect" scope="PER_VM"/>
  | 
  |    <bind pointcut="call(* java.lang.reflect.Field->get*(..))">
  |       <advice name="interceptFieldGet" aspect="MyReflectionAspect"/>
  |    </bind>
  | 
  |    <bind pointcut="call(* java.lang.reflect.Field->set*(..))">
  |       <advice name="interceptFieldSet" aspect="MyReflectionAspect"/>
  |    </bind>
  | </aop>
  | 

This way, JBoss AOP won't perform a call to the wrapper method; it will proceed to Field.set() and Field.get() executions. Notice that, because of this, you need to keep your setAccessible calls.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4048060#4048060

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4048060



More information about the jboss-user mailing list