Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Getting javax.swing to weave

Thanks. I missed the weaveJavaxPackages thing, but I do recall seeing earlier today some place where I wondered why they were saying weaveJavaPackages=true twice, probably misread it.

> For cases like this it can be simpler just to compile time weave the
> library containing the code you are interested in:
>
> ajc -inpath existingLibrary.jar MyDebugAspect.aj -outjar wovenLibrary.jar

will that work for rt.jar?

This is probably too much work for my purposes. I already solved the immediate problem that led me down this path. Which widget was processing the PageUp/PageDown keys? It wasn't the JScrollPane, it was its JScrollBars, which my code didn't even know about. If I could have woven javax.swing, I might have saved a little headscratching. I'm an old java hand but a Swing newbie.

At any rate, I think this exercise wasn't a total waste of time. We may use AOP to do the logging we can do instead of System.out.println sprinkling or log4j.

Steve



Alas, changing that alone was not enough
On 06/27/2011 03:47 PM, Andy Clement wrote:
Hi,

A couple of things.  In addition to the weaveJavaPackages there is a
weaveJavaxPackages option you may need to set:

<weaver options="-verbose
-Xset:weaveJavaPackages=true,weaveJavaxPackages=true -showWeaveInfo
-debug">

but that may still not be enough.  It depends on who loads the types
you are trying to weave.  If it is done by certain classloaders,
AspectJ doesn't see the loading and so cannot modify the code.   You
are supplying -debug which means you should be getting the 'weaving
XXX' messages out, are you seeing them for the types you are
targetting with your aspect?

For cases like this it can be simpler just to compile time weave the
library containing the code you are interested in:

ajc -inpath existingLibrary.jar MyDebugAspect.aj -outjar wovenLibrary.jar

and then run with your new library, perhaps putting it earlier on the
classpath so that the VM uses it rather than the standard (something
like):

java -bootclasspath/p:wovenLibrary.jar Main

(can't 100% remember the bootcp syntax, something like that).

cheers
Andy

On 27 June 2011 13:11, Steve Cohen<scohen@xxxxxxxxxxxxxxx>  wrote:
[first posted at the AJDT forum where I was urged to try here]

I am a newbie to AOP, AspectJ, and AJDT.

I am trying to set up a debug time usage of AOP to help our team debug a
complicated and somewhat odd Swing application that does a lot of strange
keyboard remapping, since it needs to emulate and existing application.

To determine why some things don't work, I need to know which class is
handling a given keystroke.

My pattern is to wrap the existing Swing project in an AspectJ project
(putting the Swing project on the AspectJ project's Build Path), and
defining an aspect that pointcuts at the appropriate places. The aspect is
as follows:

package com.whatever.gui.aop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

import com.whatever.gui.Login;
import com.whatever.gui.kbd.KeyRemapper;


aspect CapLogger {

        pointcut logActionSearch(JComponent jc, KeyStroke ks)
                : target (jc)
                &&  args(ks)
                &&  execution( protected
JComponent+.processKeyBinding(KeyStroke, KeyEvent,
                int, boolean));

        before(JComponent jc, KeyStroke ks) : logActionSearch(jc, ks) {
                Class klass = jc.getClass();
                System.out.println(String.format("Searching for handler for
%s in %s",
                                ks, klass));
        }
        pointcut logAction(ActionListener a, ActionEvent e)
                : target(a)
                &&  args(e)
                &&  execution(* actionPerformed(ActionEvent));

        after(ActionListener a, ActionEvent e) : logAction(a, e) {
                Object source = e.getSource();
                Class klass = source.getClass();
                if (a instanceof Action) {
                        System.out.println(String.format("action %s performed
by an object of class %s", ((Action)a).getValue(Action.NAME),
klass.getName()));
                } else {
                        System.out.println(String.format("action performed by
an object of class %s", klass.getName()));
                }
        }
...
        public static void main(String[] args) {
                Login.main(args);
        }
}

I try to enable runtime weaving with this aop.xml file on the classpath
under META-INF:

         <aspectj>

            <aspects>
              <!-- declare two existing aspects to the weaver -->
              <aspect name="com.whatever.gui.aop.CapLogger"/>

            </aspects>

            <weaver options="-verbose -Xset:weaveJavaPackages=true
-showWeaveInfo -debug">
              <include within="javax.swing.*"/>
              <include within="java.awt.*"/>
              <include within="com.whatever.*"/>
            </weaver>

          </aspectj>


I run the application in the AspectJ project with the following JVM argument
within Eclipse:

-javaagent:${eclipse_home}\plugins\org.aspectj.weaver_1.6.11.20110304135300\aspectjweaver.jar

and I've tried several other variations.

No matter what I do, I find that my advices fire when the triggering code is
within my com.whatever.* code but never fire when the triggering code is
within javax.swing.* or java.awt.*.

What, if anything, am I doing wrong?  Can someone help?  Is this even
possible?

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users





Back to the top