Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » Can't get javax.swing.* and java.awt.* to weave
Can't get javax.swing.* and java.awt.* to weave [message #689438] Mon, 27 June 2011 16:34 Go to next message
Steve Cohen is currently offline Steve CohenFriend
Messages: 46
Registered: July 2009
Member
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()));
		}	
	} 
	pointcut logKeyEvent(KeyRemapper m, KeyEvent e) 
		: target(m)
		&& args(e)
		&& execution (public boolean KeyRemapper.dispatchKeyEvent(KeyEvent));
	
	before(KeyRemapper m, KeyEvent e) : logKeyEvent(m, e) {
    	System.out.println(String.format("INPUT: %s", e.toString()));    	
	}
	after(KeyRemapper m, KeyEvent e) : logKeyEvent(m, e) {
    	System.out.println(String.format("OUTPUT: %s", e.toString()));    	
	}
	
	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 many other iterations.

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?





Re: Can't get javax.swing.* and java.awt.* to weave [message #689484 is a reply to message #689438] Mon, 27 June 2011 18:00 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
Weaving into the jre classes is very, very tricky and should be avoided unless absolutely necessary. The reason is that the weaver uses (a sub-set of) jre classes to bootstrap. And these can't be woven.

You can also try using the -Xset:weaveJavaxPackages=true option, which may help things a bit. But, it may just be that the weaver is being created too late to weave the swing and awt classes. I don't know of any workaround, but you can try asking on the aspectj users mailing list to see if someone there has an idea.

http://eclipse.org/aspectj/userlists.php
Re: Can't get javax.swing.* and java.awt.* to weave [message #689494 is a reply to message #689484] Mon, 27 June 2011 18:27 Go to previous messageGo to next message
Steve Cohen is currently offline Steve CohenFriend
Messages: 46
Registered: July 2009
Member
I understand what you're saying but presumably the subset of jre classes used to bootstrap would not include javax.swing.* or java.awt.*. These are the only packages I'm trying to weave in. So I don't really get the restriction here.

Someone on Stack Overflow suggested I try AOP as a way to gain the knowledge I was looking for but it appears not to be the answer here. I suppose I could try the custom classloader approach, but this is getting to be too much work for the task - which is basically to help some swing newbies (one of whom is myself) understand which component in the Swing hierarchy is handling each keystroke.
Re: Can't get javax.swing.* and java.awt.* to weave [message #689512 is a reply to message #689494] Mon, 27 June 2011 18:43 Go to previous message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
As I mentioned, this is tricky since there may be some swing classes loaded before the weaver is started. There may be a way to get this working, so asking the aspectj-users mailing list is your best bet at this point.
Previous Topic:Equinox Weaving & Indigo
Next Topic:TomCat running from Eclipse shows old cached data
Goto Forum:
  


Current Time: Wed Apr 24 22:15:31 GMT 2024

Powered by FUDForum. Page generated in 0.03340 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top