GitHub user xxDark edited a comment on the discussion: Windows clipboard issues 
discussion

It is also worth noting that my agent does more than serializing calls from 
AWT-Windows to EDT: it adds proper `synchronized` block to 
`handleContentsChanged` method in WClipboard. That is why I wanted for NetBeans 
team to test the fix themselves. The JDK change is lacking. After patching, the 
method looks like this:
```java
        private void handleContentsChanged() {
                if (AppContext.getAppContext() != null && 
!EventQueue.isDispatchThread()) {
                        EventQueue.invokeLater(this::handleContentsChanged);
                        return;
                }
                if (!areFlavorListenersRegistered()) {
                        return;
                }
                
                long[] formats = null;
                synchronized (this) {
                        try {
                                openClipboard(null);
                                formats = getClipboardFormats();
                        } catch (IllegalStateException exc) {
                                // do nothing to handle the exception, call 
checkChange(null)
                        } finally {
                                closeClipboard();
                        }
                }
                checkChange(formats);
        }
```
The patched bytecode does essentially the same thing but without try/finally 
inserted by the compiler (synchronized is implemented using try/finally blocks 
in javac) because I did not want to spend time on properly re-implementing 
try/finally semantics, so it does simple branching instead to do monitor 
unlocking.

Instrumented bytecode:
```
        private void handleContentsChanged() {
                if (AppContext.getAppContext() != null && 
!EventQueue.isDispatchThread()) {
                        EventQueue.invokeLater(this::handleContentsChanged);
                        return;
                }
                boolean unlocked;
                block:
                {
                        unlocked = false;
                        // MONITORENTER : this
                        try {
                                if (!this.areFlavorListenersRegistered()) {
                                        if (unlocked) return;
                                        // MONITOREXIT : this
                                        return;
                                }
                                long[] formats = null;
                                try {
                                        this.openClipboard(null);
                                        formats = this.getClipboardFormats();
                                } catch (IllegalStateException 
illegalStateException) {
                                } finally {
                                        this.closeClipboard();
                                }
                                if (unlocked) break block;
                        } catch (Throwable t) {
                                if (unlocked) throw t;
                                // MONITOREXIT : this
                                unlocked = true;
                                throw t;
                        }
                        unlocked = true;
                }
                if (!unlocked) {
                        // MONITOREXIT : this
                        unlocked = true;
                }
                this.checkChange(formats);
                if (unlocked) return;
                // MONITOREXIT : this
        }
```

GitHub link: 
https://github.com/apache/netbeans/discussions/7051#discussioncomment-12764945

----
This is an automatically sent email for notifications@netbeans.apache.org.
To unsubscribe, please send an email to: 
notifications-unsubscr...@netbeans.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org
For additional commands, e-mail: notifications-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to