gpt4 book ai didi

java - AspectJ EDT-Checker 代码问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:41 25 4
gpt4 key购买 nike

我目前正在使用 Alexander Potochkin 的 AspectJ EDTChecker code (帖子底部的相关代码)。

此代码(基于我对 AspectJ 的一点了解)提示在 Swing EDT 中未发生的任何 JComponent 方法调用或构造函数调用。

但是,以下内容仅针对 JList 构造函数,而不是 JFrame 构造函数。谁能告诉我为什么?谢谢!

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 的 AspectJ 代码:

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 不是 JComponent 的子类,但 JList 是。

关于java - AspectJ EDT-Checker 代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com