如何使用aspectJ使我的JLabel变红?

时间:2015-03-28 09:37:45

标签: java aspectj pointcut

我的Project有一个MessageUtil类,它有显示消息的方法,我试图使用aspectJ使我的Jlabels的文本变红,而不使用aspectJ它足以添加' for循环&# 39;其中一个方法是制作multiLabel短信:

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注释的类,下面用pointCut将数组消息设置为红色,然后将消息发送到createMultiLabelBySlashN()和createMultiLabelByPixel()作为参数,但我不知道它是否正确或如何定义JLabel消息[]来自我的aspectJ类中的方法createMultiLabel,使用相同的for循环使其变为红色,并将String rezult发送到createMultiLabelBySlashN。

@Pointcut ("execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelBySlashN(..)) || execution(public static JLabel[] mehad.util.MessageUtil.createMultiLabelByPixel(..)" )

即使我正在调用pointCut,我的代码似乎也出现了错误:

no match for this type name: JLabel

1 个答案:

答案 0 :(得分:0)

  

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;
    }
}

现在如果你运行没有AspectJ的main方法,你会看到这个控制台输出:

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]

Voilà - 将标签着色的跨领域问题已经模块化为一个方面。