AspectJ EDT-Checker Code Question

可紊 提交于 2019-12-06 08:33:21

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/7586311/aspectj-edt-checker-code-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!