src/share/classes/java/awt/datatransfer/Clipboard.java

Print this page

        

@@ -65,11 +65,11 @@
     /**
      * An aggregate of flavor listeners registered on this local clipboard.
      *
      * @since 1.5
      */
-    private final Set<FlavorListener> flavorListeners = new HashSet<>();
+    private Set<FlavorListener> flavorListeners;
 
     /**
      * A set of <code>DataFlavor</code>s that is available on
      * this local clipboard. It is used for tracking changes
      * of <code>DataFlavor</code>s available on this clipboard.

@@ -83,11 +83,10 @@
      * @param name for the clipboard
      * @see java.awt.Toolkit#getSystemClipboard
      */
     public Clipboard(String name) {
         this.name = name;
-        currentDataFlavors = getAvailableDataFlavorSet();
     }
 
     /**
      * Returns the name of this clipboard object.
      * @return the name of this clipboard object

@@ -255,10 +254,16 @@
      */
     public synchronized void addFlavorListener(FlavorListener listener) {
         if (listener == null) {
             return;
         }
+
+        if (flavorListeners == null) {
+            flavorListeners = new HashSet<>();
+            currentDataFlavors = getAvailableDataFlavorSet();
+        }
+
         flavorListeners.add(listener);
     }
 
     /**
      * Removes the specified <code>FlavorListener</code> so that it no longer

@@ -276,11 +281,11 @@
      * @see FlavorListener
      * @see FlavorEvent
      * @since 1.5
      */
     public synchronized void removeFlavorListener(FlavorListener listener) {
-        if (listener == null) {
+        if (listener == null || flavorListeners == null) {
             return;
         }
         flavorListeners.remove(listener);
     }
 

@@ -295,21 +300,26 @@
      * @see FlavorListener
      * @see FlavorEvent
      * @since 1.5
      */
     public synchronized FlavorListener[] getFlavorListeners() {
-        return flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
+        return flavorListeners == null ? new FlavorListener[0] :
+            flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
     }
 
     /**
      * Checks change of the <code>DataFlavor</code>s and, if necessary,
      * notifies all listeners that have registered interest for notification
      * on <code>FlavorEvent</code>s.
      *
      * @since 1.5
      */
     private void fireFlavorsChanged() {
+        if (flavorListeners == null) {
+            return;
+        }
+
         Set<DataFlavor> prevDataFlavors = currentDataFlavors;
         currentDataFlavors = getAvailableDataFlavorSet();
         if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
             return;
         }