Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » Is it possible to weave aspects on anonymous inner classes?
Is it possible to weave aspects on anonymous inner classes? [message #71162] Tue, 05 February 2008 16:36
Eclipse UserFriend
Originally posted by: jean-claude.tarby.univ-lille1.fr

Hi.

Here is my problem... I have a class which is a GUI (graphical User
Interface) and that contains several Swing components, for instance
JButton, JTree, JTextField, and so on. My aim is to weave aspects on these
components to trace some information. I know how to weave aspects on usual
methods, but my question is how to select precisely the methods, in
anonymous inner classes, that I want to trace. For example, I want to
trace the button X when the user clicks on it (like an "onCick" call), and
the textField Y when the user enters text and when the cursor enters or
leaves the textfield, and when the user open a node in a JTree, and so on.

I tried a lot of solutions, but I don’t want to modify the GUI class
structure. I know that I could write other code for the GUI that could be
weavable, but my preoccupation is to not modify programmers’ habits. Here
are two cases and an idea of solution.

Solution 1:
Here, I suppose that an instance "myObject" of "MyClass" is represented
by the View GUI class.

/* ======== View class ========*/
public class View extends JFrame {

public View() {
/* ...some code...... */

buttonOn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// some code, for instance….
System.out.println("on");
myObject.setEvt(1);
}
});

buttonOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// some code, for instance….
System.out.println("off");
myObject.setEvt(2);
}
});

buttonDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// some code, for instance….
System.out.println("delete");
myObject.setEvt(3);
}
});

/* ....other code.... */

}
}



Solution 2:
Here, a window displays 2 buttons and 3 text fields, but the inner classes
are not anonymous... and a "init" method allows to work more "properly"...
Unfortunately, this structure is not always used by programmers...

/* ======== View class ========*/
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// complete code source at
http://www.java2s.com/Code/Java/Swing-JFC/TextfieldsandJavae vents.htm

import ...;
public class TextFields extends JApplet {
private JButton b1 = new JButton("Get Text"), b2 = new JButton("Set
Text");
private JTextField t1 = new JTextField(30), t2 = new JTextField(30),
t3 = new JTextField(30);
private String s = new String();
private UpperCaseDocument ucd = new UpperCaseDocument();

public void init() {
t1.setDocument(ucd);
ucd.addDocumentListener(new T1());
b1.addActionListener(new B1());
b2.addActionListener(new B2());
DocumentListener dl = new T1();
t1.addActionListener(new T1A());
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(t1);
cp.add(t2);
cp.add(t3);
}

//...some code...

class T1A implements ActionListener {
private int count = 0;

public void actionPerformed(ActionEvent e) {
t3.setText("t1 Action Event " + count++);
}
}

class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (t1.getSelectedText() == null)
s = t1.getText();
else
s = t1.getSelectedText();
t1.setEditable(true);
}
}

class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
ucd.setUpperCase(false);
t1.setText("Inserted by Button 2: " + s);
ucd.setUpperCase(true);
t1.setEditable(false);
}
}

public static void main(String[] args) {
//...some code...
}

// ... some code...
}


====== aspect for trace ======
I wrote the following aspect, but it weaves on ALL actionPerformed :-( As
I said at the beginning, I would like trace only some widget and only some
actions… (click on button X, and when user enters text, and so on...).

Do you have solutions ? Is it possible with AspectJ ?

Sincerely

JC


package traces;

import ...;

public aspect trace_actionPerformed {
pointcut myClass(Object obj): this(obj) ;

void around(ActionEvent e) :
execution( void *.*actionPerformed( ActionEvent ) ) && args( e )
{

AbstractButton button = (AbstractButton) e.getSource();

System.out.println("clic !" +
button.getTopLevelAncestor().getClass());

System.out.println("trace_actionPerformed" + " "
+ button.getTopLevelAncestor().getClass().getSimpleName() +
" "
+ button.getText() + " start process" + " ");

// call the button’s code
proceed(e);
//

System.out.println("trace_actionPerformed " + " "
+ button.getTopLevelAncestor().getClass().getSimpleName() +
" "
+ button.getText() + " end process");

System.out.println("other data......" );

String methodName = thisJoinPoint.getSignature().getName();
String typeMethodeNom =
thisJoinPoint.getSignature().getDeclaringType().getName();
String typeMethode =
thisJoinPoint.getSignature().getDeclaringTypeName();
String typeMethodeLong =
thisJoinPoint.getSignature().toLongString();
String typeMethodeCourt =
thisJoinPoint.getSignature().toShortString();
String typeMethodeNormal = thisJoinPoint.getSignature().toString();

System.out.println("type méthode = " + typeMethode);
System.out.println("type méthode long = " + typeMethodeLong);
System.out.println("type méthode court = " + typeMethodeCourt);
System.out.println("type méthode normal = " + typeMethodeNormal);
System.out.println("type méthode nom = " + typeMethodeNom);

Object[] args = thisJoinPoint.getArgs();
Object caller = thisJoinPoint.getThis();
Object callee = thisJoinPoint.getTarget();

System.out.println("thisJoinPoint.getThis().getClass() = "
+ thisJoinPoint.getThis().getClass());
System.out.println("thisJoinPoint.getTarget().getClass() = "
+ thisJoinPoint.getTarget().getClass());

System.out
.println("thisJoinPoint.getKind() " +
thisJoinPoint.getKind());
System.out.println("thisJoinPoint.getSourceLocation() "
+ thisJoinPoint.getSourceLocation());

System.out.println("-> Begin of method " + methodName);
System.out.print("-> " + args.length + " parametre(s) ");
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
}
System.out.println();
System.out.println("-> " + caller + " vers " + callee);

System.out.println("<- End of method " + methodName);

}
}
Previous Topic:Developing a Aspectj based plugin - Newbie questions
Next Topic:Is it possible to weave aspects on anonymous inner classes?
Goto Forum:
  


Current Time: Thu Apr 25 01:04:20 GMT 2024

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

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

Back to the top