为什么Potochkin的EDT检查器会标记此代码?

时间:2012-10-24 19:24:25

标签: java swing user-interface aspectj event-dispatch-thread

我不明白为什么我的代码会在这个实例中被标记。

myPlot.plot(serviceRef, frameMax, frameMin);

该行是错误的源位置。由于代码行中没有Swing代码,因此它对我没有任何影响。为什么会发生这种情况?

我在下面附上了Potochkin的EDT违规检查代码:

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

1 个答案:

答案 0 :(得分:0)

“myPlot”变量是一个扩展JPanel的AbstractPlot实例!这导致EDT检查器标记在此实例上发出的所有调用超过EDT,即使我只是计算一个int i = 1 + 1的私有函数。现在我明白了:)