aspectj does not support JDK class weaving

Hi,

I am using AspectJ to trace calls to graphics class.So far, I have used a pre-weaved set of javax.swing.* classes and upon loading I tell JVM to use these weaved classes and not the ones from JRE by using -Xbootclasspath/p switch.

I would like to switch to load time weaving mode.Can anyone help me how to weave javax.swing on load time. I have searched the net but still I cannot figure it out how to do it. I know that by default, AspectJ load time weaver will not weave java.* and javax.* classes. Someone suggested using

-Xset:weaveJavaPackages=true,weaveJavaxPackages=true

in aop.xml but none of this helped because javax.swing classes are loaded before the weaver is attached to the classloader. I guess that the weaver does not see these classes at all.

How can I manage to dynamically weave javax.swing classes? Should I implement a custom class loader that first registers a weaver then does the class loading?

Can someone please suggest any solution?

Reply from the aspectj developer:

I don't have a good answer for you, only that weaving java and javax classes is very tricky for exactly the reason you say (they are loaded before the weaver is loaded. A custom class loader may help, but no guarantees. If at all possible, I'd stick to compile time weaving. –  Andrew Eisenberg Feb 11 at 4:08



//===================================another post about the workarounds of not instrumenting the jdk classes.

  • From: Wes Isberg <wes@xxxxxxxxxxxxxx>
  • Date: Fri, 08 Aug 2003 00:09:53 -0700
  • Delivered-to: aspectj-users@eclipse.org
  • User-agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)

Hi -

> I too am interested in aspecting java.* classes. I have seen several
> postings on this issue looking for an answer, But haven't seen any
> replies yet :(

Eclipse.org lets you search the mailing lists.  E.g., for
"do not use bytecode weaving" on aspectj-users, this was first:

  http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg00846.html

>> I'm trying to implement a java.awt.Toolkit (and all that it produces)
>> so that AWT and Swing applications can be
>> accessed locally, or remotely.  <snip>

Swingaway looks like a great idea that could fill a strong need.
I know nothing about jiapi, but would also recommend JMangler
if you really want to do load-time transformations.  It seems
safe and easy to write for.

  http://javalab.cs.uni-bonn.de/research/jmangler/

But do you have to instrument sun classes?  AspectJ would let you
put around advice on any call from the client code to java.awt,
and redirect it to your server.  So you'd only need to weave
the client code, and you can do that as a condition of their use.

Here's around advice that replaces all java.awt calls with
the result of the serverProxy(JoinPoint) method:

   Object around() : call(* java.awt..*.*(..))
            || call(java.awt..*.new(..)) {
        return serverProxy(thisJoinPoint); // has target, args, etc.
   }

Now that would be an interesting method to write!
So perhaps it would be your backup, lowest-precedence advice.
You could iteratively develop by implementing specific calls,
handling fewer and fewer in the backup case:

aspect HandleAwt {
   ...
   // handle actionPerformed...
   void around(ActionListener listener, Object caller) :
            this(caller) && target(listener) &&
            call(void ActionListener.actionPerformed()) {
        serverProxy_actionPerformed(caller, listener);
   }

   // handle others...

   // last advice handles any remaining...
   Object around() : call(* java.awt..*.*(..))
       ...
}

Aside from around advice, proxying could work well.
Replace the clients AWT instance with your own subclass,
and put all the proxying in the subclass.  It's best
to do this on construction:

    // replace any button constructed with our proxy
    Button around(String label) : call(Button.new(String)) {
        return ButtonProxy(label);
    }

But (in case your client doesn't create the instances)
you can also do it at other lifecycle points, e.g., when
components are added to containers:

    // replace any Component added to a Container with ours
    Component around(Component toAdd) : args(toAdd)
            && call(Component Container.add(Component)) {
        return proceed(ComponentProxies.make(toAdd));
    }

You can do cool things in pointcuts with proxies.  For
example, you can skip any advice for calls to your proxies,
easily if there's a common supertype, e.g., IServerProxy

    interface IServerProxy{}
    declare parents: ButtonProxy implements ServerProxy;

then you can use it to skip calls to instance methods:

   void around(ActionListener listener, Object caller) :
            !target(IServerProxy) &&
            this(caller) && target(listener) &&
            ...

or to issue warnings:

   before() : call(* Component.*(..))
            && !args(IServerProxy) {
       // bug! all components should be proxied at this point
   }

and you can declare fields or methods on it:

    Server IServerProxy.server;
    void IServerProxy.connect() {
        try {
             server.connect();
        ...
    }

which you can use in your advice:

   // handle actionPerformed...
   void around(IServerProxy proxy, Object caller) :
            this(caller) && target(proxy) &&
            call(void ActionListener.actionPerformed()) {
	Connection connection = proxy.connect();
        ...
   }

, etc.

David Walend last year used AspectJ to capture all calls
into and out of a certain scope, in order to be able
to replay them (interestingly, he replayed them by
generating the corresponding java code, compiling it,
and then running it).  I suspect he got a job or got
married or something silly like that because the project
has been dormant recently.  In any case, you can check
out his code to see if some of his ideas or approaches
help you.  It's at

   http://cricketcage.sourceforge.net

Please let us know whether you decide to use AspectJ.

Wes





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值