简体   繁体   English

Java使用PropertyChangeSupport和PropertyChangeListener检测变量更改

[英]Java Detect Variable Change Using PropertyChangeSupport and PropertyChangeListener

I'm trying to print out debug statements when some third party code changes a variable. 当某些第三方代码更改变量时,我正在尝试打印出调试语句。 For example, consider the following: 例如,考虑以下内容:

public final class MysteryClass {

private int secretCounter;

public synchronized int getCounter() {
    return secretCounter;
}

public synchronized void incrementCounter() {
    secretCounter++;
}

}

public class MyClass {

public static void main(String[] args) {
    MysteryClass mysteryClass = new MysteryClass();
    // add code here to detect calls to incrementCounter and print a debug message
}

I don't have the ability to change the 3rd party MysteryClass, so I thought that I could use PropertyChangeSupport and PropertyChangeListener to detect changes to the secretCounter: 我没有能力更改第三方MysteryClass,所以我认为我可以使用PropertyChangeSupport和PropertyChangeListener来检测对secretCounter的更改:

public class MyClass implements PropertyChangeListener {

    private PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);

    public MyClass() {
        propertySupport.addPropertyChangeListener(this);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        System.out.println("property changing: " + evt.getPropertyName());
    }

    public static void main(String[] args) {
        MysteryClass mysteryClass = new MysteryClass();
        // do logic which involves increment and getting the value of MysteryClass
    }
}

Unfortunately, this did not work and I have no debug messages printed out. 不幸的是,这没有用,并且我没有打印出调试消息。 Does anyone see what is wrong with my implementation of the PropertyChangeSupport and Listener interfaces? 有人看到我的PropertyChangeSupport和Listener接口实现有什么问题吗? I want to print a debug statement whenever incrementCounter is called or the value of secretCounter changes. 我想在每次调用incrementCounter或secretCounter的值更改时打印调试语句。

I'm pretty sure the PropertyChangeListener mechanism only works if you set the properties through the PropertyEditor mechanis, not through getters and setters 我很确定PropertyChangeListener机制仅在通过PropertyEditor机制而不是通过getter和setter设置属性时才有效

I'd probably try it with AspectJ, but you can only advise the method calls, not the execution, as the third party class is final. 我可能会用AspectJ尝试一下,但是您只能建议方法调用,而不能建议执行,因为第三方类是最终的。

I am sorry to say this but in case of that MysteryClass implementation and if you are not able to change it's implementation you can not and by the way you got the PropertyChangeListener PropertyChangeSupport concept all wrong. 不好意思地说,但是对于MysteryClass实现,如果您不能更改它的实现,那么您将无法通过顺便将PropertyChangeListener PropertyChangeSupport概念弄错。 to make it work PropertyChangeSupport MysteryClass see the Java Beans Tutorial on Bound properties what you can do is to wrap the class with your own class lets say MysteryClassWithPrint that will implement all the public methods as delegation to the internal instance of MysteryClass AND print messages and then replace all new MysteryClass(); 要使其发挥作用,PropertyChangeSupport MysteryClass参见Java Beans Tutorial的Bound属性 ,您可以做的是用您自己的类包装该类,说MysteryClassWithPrint,它将实现所有公共方法作为对MysteryClass的内部实例的委托并打印消息,然后替换所有new MysteryClass(); with new MysteryClassWithPrint(); 带有新的MysteryClassWithPrint();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM