我的项目有一个MessageUtil类,它有方法来显示消息,我试图让我的JLabel红使用AspectJ的文本,无需用AspectJ的就足够了“for循环”增加的方法之一至极使得多标签短信:
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;
}
这两种方法createMultiLabelByPixel(MSG)和createMultiLabelBySlashN(MSG)在此形式:
private static JLabel[] createMultiLabelBySlashN(String msg) {
// the code here
}
我想用AspectJ序做出的JLabel红色,在方法createMultiLabel的身体不使用for循环,我没有任何想法如何做到这一点,我试图让含有aspecJ注释与切入点一类下面,使被送到createMultiLabelBySlashN()和createMultiLabelByPixel()作为参数的消息之前红数组的消息,但我不知道这是否是正确的还是如何来定义我的AspectJ从方法createMultiLabel选择JLabel消息[]类,使其使用相同的for循环红色和发送字符串rezult到createMultiLabelBySlashN。
@Pointcut ("execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelBySlashN(..)) || execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelByPixel(..)" )
甚至当我打电话的切入点似乎有我的代码,说的错误:
no match for this type name: JLabel
no match for this type name: JLabel
这是因为你不使用完全合格的类名javax.swing.JLabel
。
现在让我们假设你的类看起来像这样(只是一个简单的假的):
package mehad.util;
import javax.swing.JLabel;
public class MessageUtil {
public static void main(String[] args) {
for (JLabel label : createMultiLabel("Albert Einstein\nWerner Heisenberg\nMax Planck"))
System.out.println(label.getText() + " / " + label.getForeground());
System.out.println();
for (JLabel label : createMultiLabel("Albert Einstein, Werner Heisenberg, Max Planck"))
System.out.println(label.getText() + " / " + label.getForeground());
}
public static JLabel[] createMultiLabel(String msg) {
return msg.contains("\n")
? createMultiLabelBySlashN(msg)
: createMultiLabelByPixel(msg);
}
private static JLabel[] createMultiLabelBySlashN(String msg) {
String[] lines = msg.split("\n+");
JLabel[] labels = new JLabel[lines.length];
for (int i = 0; i < lines.length; i++)
labels[i] = new JLabel(lines[i]);
return labels;
}
private static JLabel[] createMultiLabelByPixel(String msg) {
String[] words = msg.split("[, ]+");
JLabel[] labels = new JLabel[words.length];
for (int i = 0; i < words.length; i++)
labels[i] = new JLabel(words[i]);
return labels;
}
}
现在,如果你运行的main
没有AspectJ的方法,你会看到这个控制台输出:
Albert Einstein / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Werner Heisenberg / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Max Planck / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Albert / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Einstein / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Werner / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Heisenberg / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Max / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
Planck / sun.swing.PrintColorUIResource[r=51,g=51,b=51]
即所有的标签都有一个默认的灰色。 现在增加这方面:
package de.scrum_master.aspect;
import java.awt.Color;
import javax.swing.JLabel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class RedLabelAspect {
@Pointcut("execution(static javax.swing.JLabel[] createMultiLabel(..))")
public void multilabelCreation() {}
@Around("multilabelCreation()")
public JLabel[] changeLabelColours(ProceedingJoinPoint thisJoinPoint) throws Throwable {
JLabel[] labels = (JLabel[]) thisJoinPoint.proceed();
for (JLabel label : labels)
label.setForeground(Color.RED);
return labels;
}
}
纵横修改包含在地方的结果数组中的标签,即在for
循环现在驻留在方面代码,不再是核心代码。 控制台输出更改为:
Albert Einstein / java.awt.Color[r=255,g=0,b=0]
Werner Heisenberg / java.awt.Color[r=255,g=0,b=0]
Max Planck / java.awt.Color[r=255,g=0,b=0]
Albert / java.awt.Color[r=255,g=0,b=0]
Einstein / java.awt.Color[r=255,g=0,b=0]
Werner / java.awt.Color[r=255,g=0,b=0]
Heisenberg / java.awt.Color[r=255,g=0,b=0]
Max / java.awt.Color[r=255,g=0,b=0]
Planck / java.awt.Color[r=255,g=0,b=0]
瞧 - 着色标签的横切关注点已被模块化的进一个方面。