3

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();
        }
    }
}
BenCole
  • 2,092
  • 3
  • 17
  • 26

1 Answers1

4

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

James P.
  • 19,313
  • 27
  • 97
  • 155
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148