Java – swing validation code on the event dispatch thread at run time

Is there any library that can use code to verify that the swing component is called in the event scheduling thread? Writing some basic code may not be too difficult, but I'm sure there are edge cases that others don't deal with I'm looking for this at runtime, not unit tests

Solution

The Fest framework has a tool to detect swing usage from EDT It is basically a repaintmanager installation The framework is test - oriented, but the repaintmanager can be used during deployment

>See Fest – swing's event dispatch thread

Alternatively, to check that all methods, such as getters and setters, can only be accessed on EDT, you can use AspectJ and load time weaving to put swingutilities Isdisaptchthread() recommends adding each method to your swing component (and JDK swing component)

@Aspect
public class EDTCheck {

    @pointcut("call (* javax.swing..*+.*(..)) || " +
              "call (javax.swing..*+.new(..))")
    public void swingMethods() {}

    @pointcut("call (* com.mystuff.swing..*+.*(..)) || " +
              "call (com.mystuff.swing..*+.new(..))")
    public void mySwingMethods() {}


    @pointcut("call (* javax.swing..*+.add*Listener(..)) || " +
              "call (* javax.swing..*+.remove*Listener(..)) || " +
              "call (void javax.swing.JComponent+.setText(java.lang.String))")
    public void safeMethods() {}

    @Before("(swingMethods() || mySwingMethods()) && !safeMethods()")
    public void checkCallingThread(JoinPoint.StaticPart thisJoinPointStatic) {
        if(!SwingUtilities.isDispatchThread()) {
            System.out.println(
                    "Swing single thread rule violation: " 
                    + thisJoinPointStatic);
            Thread.dumpStack();
            // or you might throw an unchecked exception
        }
    }

}

(slightly modified from the article – add the myswingmethods pointcut and use swingutilites. Isdispatchthread() to actually connect with eventqueue Isdispatchthread() is the same, but the abstraction is cleaner.)

>See using AspectJ to detect violations of the swing single thread rule

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>