my Project has a MessageUtil class, which has methods to show messages, I'm trying to make the texts of my Jlabels red using aspectJ, without use of aspectJ it is enough to add 'for loop' to one of the methods wich makes multiLabel text message:
public static JLabel[] createMultiLabel(String msg) {
JLabel[] messages = null;
if (msg.contains("\n")) {
messages = createMultiLabelBySlashN(msg);
} else {
messages = createMultiLabelByPixel(msg);
}
//this for loop makes the text red
for (int i = 0; i < messages.length; i++) {
messages[i].setForeground(Color.RED);
}
return messages;
}
The two methods createMultiLabelByPixel(msg) and createMultiLabelBySlashN(msg) are in this form:
private static JLabel[] createMultiLabelBySlashN(String msg) {
// the code here
}
I want to use aspectJ inorder to make the JLabels red, not using the for loop in the body of method createMultiLabel,I don't have any idea how to do this, I'm trying to make a class containing aspecJ annotation with the pointCut below to make the array messages red before the messages is sent to createMultiLabelBySlashN() and createMultiLabelByPixel() as their parameter, but I don't know if it is correct or how to define the JLabel messages[] from the method createMultiLabel in my aspectJ class to make it red using the same for loop and send the String rezult to createMultiLabelBySlashN.
@Pointcut ("execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelBySlashN(..)) || execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelByPixel(..)" )
even when I'm calling the pointCut it seems there are errors with my code that says:
no match for this type name: JLabel
This is because you do not use the fully qualified class name
javax.swing.JLabel
.Now let us assume your class looks like this (just a simple fake):
Now if you run the
main
method without AspectJ, you will see this console output:I.e. all labels have a default grey colour. Now add this aspect:
The aspect modifies the labels contained in the result array in place, i.e. the
for
loop now resides in your aspect code, no longer in the core code. The console output changes to:Voilà - the cross-cutting concern of colouring the labels has been modularised into an aspect.