AspectJ EDT-Checker代码问题 [英] AspectJ EDT-Checker Code Question

查看:133
本文介绍了AspectJ EDT-Checker代码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Alexander Potochkin的AspectJ EDTChecker代码



这段代码(从我对AspectJ的一点了解),在任何JComponent方法调用或构造函数调用中都不会发生Swing EDT。



但是,下面只是对JList构造函数,而不是JFrame构造函数。任何人都可以告诉我为什么?谢谢!

 包testEDT; 

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;

public class TestEDT {

JList list;
final JFrame frame;

public TestEDT(){
DefaultListModel dlm = new DefaultListModel();
list = new JList(dlm);
frame = new JFrame(TestEDT);
}

public static void main(String args []){
TestEDT t = new TestEDT();
t.frame.setVisible(true);
}
}

Alexander Potochkin的AspectJ代码:

 包testEDT; 

import javax.swing。*;

/ **
* AspectJ代码,用于检查正在执行的Swing组件方法在事件分派线程之外。
*
*(有关为什么这是坏的信息,请参阅:http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html)
*
*从Alexander Potochkin的博客文章:
* http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
*
* /
aspect EdtRuleChecker {
/ **使用标志* /
private boolean isStressChecking = true;

/ **定义任何Swing方法* /
public pointcut anySwingMethods(JComponent c):
target(c)&& call(* *(..));

/ **定义线程安全方法* /
public pointcut threadSafeMethods():
call(* repaint(..))||
call(* revalidate())||
call(* invalidate())||
call(* getListeners(..))||
call(* add * Listener(..))||
call(* remove * Listener(..));

/ **任何JComponent方法的调用,包括子类* /
before(JComponent c):anySwingMethods(c)&&
!threadSafeMethods()&&
!within(EdtRuleChecker){
if(!SwingUtilities.isEventDispatchThread()&&(isStressChecking || c.isShowing())){
System.err.println(thisJoinPoint。 getSourceLocation());
System.err.println(thisJoinPoint.getSignature());
System.err.println();
}
}

/ **任何JComponent构造函数的调用,包括子类* /
before():call(JComponent + .new(..)){
if(isStressChecking&&!SwingUtilities.isEventDispatchThread()){
System.err.println(thisJoinPoint.getSourceLocation());
System.err.println(thisJoinPoint.getSignature()+* constructor *);
System.err.println();
}
}
}


解决方案>

JFrame 不是 JComponent 的子类,而是 JList 是。


I am currently using Alexander Potochkin's AspectJ EDTChecker code (relevant code at bottom of post).

This code (from what little I understand of AspectJ) complains on any JComponent method call or constructor call that does not occur within the Swing EDT.

However, the following only complains on the JList constructor, NOT the JFrame constructor. Can anyone tell me why? Thanks!

package testEDT;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;

public class TestEDT{

    JList list;
    final JFrame frame;

    public TestEDT() {
        DefaultListModel dlm = new DefaultListModel();
        list = new JList(dlm);
        frame = new JFrame("TestEDT");
    }

    public static void main(String args[]) {
        TestEDT t = new TestEDT();
        t.frame.setVisible(true);
    }
}

Alexander Potochkin's AspectJ code:

package testEDT;

import javax.swing.*;

/**
 * AspectJ code that checks for Swing component methods being executed OUTSIDE the Event-Dispatch-Thread.
 * 
 * (For info on why this is bad, see: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html)
 * 
 * From Alexander Potochkin's blog post here:
 * http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
 * 
 */
aspect EdtRuleChecker{
    /** Flag for use */
    private boolean isStressChecking = true;

    /** defines any Swing method */
    public pointcut anySwingMethods(JComponent c):
         target(c) && call(* *(..));

    /** defines thread-safe methods */
    public pointcut threadSafeMethods():         
         call(* repaint(..)) || 
         call(* revalidate()) ||
         call(* invalidate()) ||
         call(* getListeners(..)) ||
         call(* add*Listener(..)) ||
         call(* remove*Listener(..));

    /** calls of any JComponent method, including subclasses */
    before(JComponent c): anySwingMethods(c) && 
                          !threadSafeMethods() &&
                          !within(EdtRuleChecker) {
        if ( !SwingUtilities.isEventDispatchThread() && (isStressChecking || c.isShowing())) {
            System.err.println(thisJoinPoint.getSourceLocation());
            System.err.println(thisJoinPoint.getSignature());
            System.err.println();
        }
    }

    /** calls of any JComponent constructor, including subclasses */
    before(): call(JComponent+.new(..)) {
        if (isStressChecking && !SwingUtilities.isEventDispatchThread()) {
            System.err.println(thisJoinPoint.getSourceLocation());
            System.err.println(thisJoinPoint.getSignature() + " *constructor*");
            System.err.println();
        }
    }
}

解决方案

JFrame is not a subclass of JComponent, but JList is.

这篇关于AspectJ EDT-Checker代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆