8074763: Remove API references to java.awt.dnd.peer
authorserb
Fri, 17 Apr 2015 16:57:30 +0300
changeset 30470 bd197b8eda21
parent 30469 bac0a7ff7e1e
child 30471 c1568a2416a8
8074763: Remove API references to java.awt.dnd.peer Reviewed-by: alanb, ant, prr
jdk/src/java.desktop/share/classes/java/awt/Component.java
jdk/src/java.desktop/share/classes/java/awt/dnd/DragSource.java
jdk/src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java
jdk/src/java.desktop/share/classes/java/awt/dnd/DropTarget.java
jdk/src/java.desktop/share/classes/java/awt/dnd/DropTargetContext.java
jdk/src/java.desktop/share/classes/sun/awt/AWTAccessor.java
jdk/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java
jdk/test/java/awt/dnd/DragSourceListenerSerializationTest/DragSourceListenerSerializationTest.java
--- a/jdk/src/java.desktop/share/classes/java/awt/Component.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/Component.java	Fri Apr 17 16:57:30 2015 +0300
@@ -1085,7 +1085,7 @@
         DropTarget old;
 
         if ((old = dropTarget) != null) {
-            if (peer != null) dropTarget.removeNotify(peer);
+            dropTarget.removeNotify();
 
             DropTarget t = dropTarget;
 
@@ -1103,12 +1103,12 @@
         if ((dropTarget = dt) != null) {
             try {
                 dropTarget.setComponent(this);
-                if (peer != null) dropTarget.addNotify(peer);
+                dropTarget.addNotify();
             } catch (IllegalArgumentException iae) {
                 if (old != null) {
                     try {
                         old.setComponent(this);
-                        if (peer != null) dropTarget.addNotify(peer);
+                        dropTarget.addNotify();
                     } catch (IllegalArgumentException iae1) {
                         // ignore it!
                     }
@@ -7011,7 +7011,7 @@
                 popup.addNotify();
             }
 
-            if (dropTarget != null) dropTarget.addNotify(peer);
+            if (dropTarget != null) dropTarget.addNotify();
 
             peerFont = getFont();
 
@@ -7098,7 +7098,7 @@
                     ((FlipBufferStrategy)bufferStrategy).destroyBuffers();
                 }
 
-                if (dropTarget != null) dropTarget.removeNotify(peer);
+                if (dropTarget != null) dropTarget.removeNotify();
 
                 // Hide peer first to stop system events such as cursor moves.
                 if (visible) {
--- a/jdk/src/java.desktop/share/classes/java/awt/dnd/DragSource.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/dnd/DragSource.java	Fri Apr 17 16:57:30 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,13 +35,15 @@
 import java.awt.datatransfer.FlavorMap;
 import java.awt.datatransfer.SystemFlavorMap;
 import java.awt.datatransfer.Transferable;
-import java.awt.dnd.peer.DragSourceContextPeer;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.security.AccessController;
 import java.util.EventListener;
+
+import sun.awt.AWTAccessor;
+import sun.awt.AWTAccessor.DragSourceContextAccessor;
 import sun.awt.dnd.SunDragSourceContextPeer;
 import sun.security.action.GetIntegerAction;
 
@@ -303,22 +305,16 @@
         try {
             if (flavorMap != null) this.flavorMap = flavorMap;
 
-            DragSourceContextPeer dscp = Toolkit.getDefaultToolkit().createDragSourceContextPeer(trigger);
-
-            DragSourceContext     dsc = createDragSourceContext(dscp,
-                                                                trigger,
-                                                                dragCursor,
-                                                                dragImage,
-                                                                imageOffset,
-                                                                transferable,
-                                                                dsl
-                                                                );
+            DragSourceContext dsc = createDragSourceContext(trigger, dragCursor,
+                                                            dragImage,
+                                                            imageOffset,
+                                                            transferable, dsl);
 
             if (dsc == null) {
                 throw new InvalidDnDOperationException();
             }
-
-            dscp.startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); // may throw
+            DragSourceContextAccessor acc = AWTAccessor.getDragSourceContextAccessor();
+            acc.getPeer(dsc).startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); // may throw
         } catch (RuntimeException e) {
             SunDragSourceContextPeer.setDragDropInProgress(false);
             throw e;
@@ -442,7 +438,6 @@
      * is registered with the created <code>DragSourceContext</code>,
      * but <code>NullPointerException</code> is not thrown.
      *
-     * @param dscp          The <code>DragSourceContextPeer</code> for this drag
      * @param dgl           The <code>DragGestureEvent</code> that triggered the
      *                      drag
      * @param dragCursor     The initial {@code Cursor} for this drag operation
@@ -473,8 +468,13 @@
      *         event are equal to <code>DnDConstants.ACTION_NONE</code>.
      */
 
-    protected DragSourceContext createDragSourceContext(DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl) {
-        return new DragSourceContext(dscp, dgl, dragCursor, dragImage, imageOffset, t, dsl);
+    protected DragSourceContext createDragSourceContext(DragGestureEvent dgl,
+                                                        Cursor dragCursor,
+                                                        Image dragImage,
+                                                        Point imageOffset,
+                                                        Transferable t,
+                                                        DragSourceListener dsl) {
+        return new DragSourceContext(dgl, dragCursor, dragImage, imageOffset, t, dsl);
     }
 
     /**
--- a/jdk/src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java	Fri Apr 17 16:57:30 2015 +0300
@@ -29,20 +29,19 @@
 import java.awt.Cursor;
 import java.awt.Image;
 import java.awt.Point;
-
+import java.awt.Toolkit;
 import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.Transferable;
 import java.awt.datatransfer.UnsupportedFlavorException;
-
 import java.awt.dnd.peer.DragSourceContextPeer;
-
 import java.io.IOException;
 import java.io.InvalidObjectException;
+import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.ObjectInputStream;
 import java.io.Serializable;
+import java.util.TooManyListenersException;
 
-import java.util.TooManyListenersException;
+import sun.awt.AWTAccessor;
 
 /**
  * The <code>DragSourceContext</code> class is responsible for managing the
@@ -123,6 +122,10 @@
 
     protected static final int CHANGED = 3;
 
+    static {
+        AWTAccessor.setDragSourceContextAccessor(dsc -> dsc.peer);
+    }
+
     /**
      * Called from <code>DragSource</code>, this constructor creates a new
      * <code>DragSourceContext</code> given the
@@ -155,7 +158,6 @@
      * If <code>DragSourceListener</code> is <code>null</code> no exception
      * is thrown.
      *
-     * @param dscp       the <code>DragSourceContextPeer</code> for this drag
      * @param trigger    the triggering event
      * @param dragCursor     the initial {@code Cursor} for this drag operation
      *                       or {@code null} for the default cursor handling;
@@ -179,10 +181,12 @@
      * @throws NullPointerException if dscp, trigger, or t are null, or
      *         if dragImage is non-null and offset is null
      */
-    public DragSourceContext(DragSourceContextPeer dscp,
-                             DragGestureEvent trigger, Cursor dragCursor,
+    public DragSourceContext(DragGestureEvent trigger, Cursor dragCursor,
                              Image dragImage, Point offset, Transferable t,
                              DragSourceListener dsl) {
+        DragSourceContextPeer dscp = Toolkit.getDefaultToolkit()
+                .createDragSourceContextPeer(trigger);
+
         if (dscp == null) {
             throw new NullPointerException("DragSourceContextPeer");
         }
@@ -623,8 +627,7 @@
     /*
      * fields
      */
-
-    private transient DragSourceContextPeer peer;
+    private final transient DragSourceContextPeer peer;
 
     /**
      * The event which triggered the start of the drag.
--- a/jdk/src/java.desktop/share/classes/java/awt/dnd/DropTarget.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/dnd/DropTarget.java	Fri Apr 17 16:57:30 2015 +0300
@@ -207,19 +207,13 @@
         if (component == c || component != null && component.equals(c))
             return;
 
-        Component     old;
-        ComponentPeer oldPeer = null;
+        final Component old = component;
 
-        if ((old = component) != null) {
+        if (old  != null) {
             clearAutoscroll();
 
             component = null;
-
-            if (componentPeer != null) {
-                oldPeer = componentPeer;
-                removeNotify(componentPeer);
-            }
-
+            removeNotify();
             old.setDropTarget(null);
 
         }
@@ -229,7 +223,7 @@
         } catch (Exception e) { // undo the change
             if (old != null) {
                 old.setDropTarget(this);
-                addNotify(oldPeer);
+                addNotify();
             }
         }
     }
@@ -497,16 +491,16 @@
      * association of the ComponentPeer with the Component may result in
      * a malfunction of the DnD system.
      **********************************************************************
-     *
-     * @param peer The Peer of the Component we are associated with!
-     *
      */
-
-    public void addNotify(ComponentPeer peer) {
-        if (peer == componentPeer) return;
+    public void addNotify() {
+        final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
+        ComponentPeer peer = acc.getPeer(component);
+        if (peer == null || peer == componentPeer) {
+            return;
+        }
 
         componentPeer = peer;
-        final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
+
 
         for (Component c = component;
              c != null && peer instanceof LightweightPeer; c = c.getParent()) {
@@ -514,7 +508,7 @@
         }
 
         if (peer instanceof DropTargetPeer) {
-            nativePeer = peer;
+            nativePeer = (DropTargetPeer) peer;
             ((DropTargetPeer)peer).addDropTarget(this);
         } else {
             nativePeer = null;
@@ -533,15 +527,14 @@
      * disassociation of the ComponentPeer from the Component may result in
      * a malfunction of the DnD system.
      **********************************************************************
-     *
-     * @param peer The Peer of the Component we are being disassociated from!
      */
 
-    public void removeNotify(ComponentPeer peer) {
-        if (nativePeer != null)
-            ((DropTargetPeer)nativePeer).removeDropTarget(this);
-
-        componentPeer = nativePeer = null;
+    public void removeNotify() {
+        if (nativePeer != null) {
+            nativePeer.removeDropTarget(this);
+        }
+        componentPeer = null;
+        nativePeer = null;
 
         synchronized (this) {
             if (isDraggingInside) {
@@ -837,7 +830,7 @@
     /*
      * That Component's "native" Peer
      */
-    private transient ComponentPeer nativePeer;
+    private transient DropTargetPeer nativePeer;
 
 
     /**
--- a/jdk/src/java.desktop/share/classes/java/awt/dnd/DropTargetContext.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/dnd/DropTargetContext.java	Fri Apr 17 16:57:30 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,8 @@
 import java.util.Arrays;
 import java.util.List;
 
+import sun.awt.AWTAccessor;
+import sun.awt.AWTAccessor.DropTargetContextAccessor;
 
 /**
  * A <code>DropTargetContext</code> is created
@@ -58,6 +60,19 @@
 
     private static final long serialVersionUID = -634158968993743371L;
 
+    static {
+        AWTAccessor.setDropTargetContextAccessor(new DropTargetContextAccessor() {
+            @Override
+            public void reset(DropTargetContext dtc) {
+                dtc.reset();
+            }
+            @Override
+            public void setDropTargetContextPeer(DropTargetContext dtc,
+                                                 DropTargetContextPeer dtcp) {
+                dtc.setDropTargetContextPeer(dtcp);
+            }
+        });
+    }
     /**
      * Construct a <code>DropTargetContext</code>
      * given a specified <code>DropTarget</code>.
@@ -90,20 +105,9 @@
     public Component getComponent() { return dropTarget.getComponent(); }
 
     /**
-     * Called when associated with the <code>DropTargetContextPeer</code>.
-     *
-     * @param dtcp the <code>DropTargetContextPeer</code>
-     */
-
-    public void addNotify(DropTargetContextPeer dtcp) {
-        dropTargetContextPeer = dtcp;
-    }
-
-    /**
      * Called when disassociated with the <code>DropTargetContextPeer</code>.
      */
-
-    public void removeNotify() {
+    void reset() {
         dropTargetContextPeer = null;
         transferable          = null;
     }
@@ -282,12 +286,18 @@
      *
      * @return the platform peer
      */
-
     DropTargetContextPeer getDropTargetContextPeer() {
         return dropTargetContextPeer;
     }
 
     /**
+     * Sets the {@code DropTargetContextPeer}
+     */
+    void setDropTargetContextPeer(final DropTargetContextPeer dtcp) {
+        dropTargetContextPeer = dtcp;
+    }
+
+    /**
      * Creates a TransferableProxy to proxy for the specified
      * Transferable.
      *
@@ -412,7 +422,7 @@
      *
      * @serial
      */
-    private DropTarget dropTarget;
+    private final DropTarget dropTarget;
 
     private transient DropTargetContextPeer dropTargetContextPeer;
 
--- a/jdk/src/java.desktop/share/classes/sun/awt/AWTAccessor.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/AWTAccessor.java	Fri Apr 17 16:57:30 2015 +0300
@@ -29,6 +29,10 @@
 
 import javax.accessibility.AccessibleContext;
 import java.awt.*;
+import java.awt.dnd.DragSourceContext;
+import java.awt.dnd.DropTargetContext;
+import java.awt.dnd.peer.DragSourceContextPeer;
+import java.awt.dnd.peer.DropTargetContextPeer;
 import java.awt.event.InputEvent;
 import java.awt.event.InvocationEvent;
 import java.awt.event.KeyEvent;
@@ -785,6 +789,31 @@
     }
 
     /*
+     * An accessor object for the DragSourceContext class
+     */
+    public interface DragSourceContextAccessor {
+        /**
+         * Returns the peer of the DragSourceContext.
+         */
+        DragSourceContextPeer getPeer(DragSourceContext dsc);
+    }
+
+    /*
+     * An accessor object for the DropTargetContext class
+     */
+    public interface DropTargetContextAccessor {
+        /**
+         * Resets the DropTargetContext.
+         */
+        void reset(DropTargetContext dtc);
+        /**
+         * Sets the {@code DropTargetContextPeer}
+         */
+        void setDropTargetContextPeer(DropTargetContext dtc,
+                                      DropTargetContextPeer dtcp);
+    }
+
+    /*
      * Accessor instances are initialized in the static initializers of
      * corresponding AWT classes by using setters defined below.
      */
@@ -815,6 +844,8 @@
     private static InvocationEventAccessor invocationEventAccessor;
     private static SystemColorAccessor systemColorAccessor;
     private static AccessibleContextAccessor accessibleContextAccessor;
+    private static DragSourceContextAccessor dragSourceContextAccessor;
+    private static DropTargetContextAccessor dropTargetContextAccessor;
 
     /*
      * Set an accessor object for the java.awt.Component class.
@@ -1275,4 +1306,39 @@
     public static void setAccessibleContextAccessor(AccessibleContextAccessor accessor) {
         AWTAccessor.accessibleContextAccessor = accessor;
     }
+
+    /*
+     * Get the accessor object for the java.awt.dnd.DragSourceContext class.
+     */
+    public static DragSourceContextAccessor getDragSourceContextAccessor() {
+        if (dragSourceContextAccessor == null) {
+            unsafe.ensureClassInitialized(DragSourceContext.class);
+        }
+        return dragSourceContextAccessor;
+    }
+
+    /*
+     * Set the accessor object for the java.awt.dnd.DragSourceContext class.
+     */
+    public static void setDragSourceContextAccessor(DragSourceContextAccessor accessor) {
+        AWTAccessor.dragSourceContextAccessor = accessor;
+    }
+
+    /*
+     * Get the accessor object for the java.awt.dnd.DropTargetContext class.
+     */
+    public static DropTargetContextAccessor getDropTargetContextAccessor() {
+        if (dropTargetContextAccessor == null) {
+            unsafe.ensureClassInitialized(DropTargetContext.class);
+        }
+        return dropTargetContextAccessor;
+    }
+
+    /*
+     * Set the accessor object for the java.awt.dnd.DropTargetContext class.
+     */
+    public static void setDropTargetContextAccessor(DropTargetContextAccessor accessor) {
+        AWTAccessor.dropTargetContextAccessor = accessor;
+    }
+
 }
--- a/jdk/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java	Fri Apr 17 16:57:30 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,8 @@
 import java.util.Map;
 import java.util.Arrays;
 
+import sun.awt.AWTAccessor;
+import sun.awt.AWTAccessor.DropTargetContextAccessor;
 import sun.util.logging.PlatformLogger;
 
 import java.io.IOException;
@@ -313,9 +315,10 @@
         Point      hots = event.getPoint();
 
         local = getJVMLocalSourceTransferable();
-
+        DropTargetContextAccessor acc =
+                AWTAccessor.getDropTargetContextAccessor();
         if (currentDTC != null) { // some wreckage from last time
-            currentDTC.removeNotify();
+            acc.reset(currentDTC);
             currentDTC = null;
         }
 
@@ -323,7 +326,7 @@
             currentDT  = dt;
             currentDTC = currentDT.getDropTargetContext();
 
-            currentDTC.addNotify(this);
+            acc.setDropTargetContextPeer(currentDTC, this);
 
             currentA   = dt.getDefaultActions();
 
@@ -370,13 +373,15 @@
         Component         c   = (Component)event.getSource();
         DropTarget        dt  = c.getDropTarget();
         DropTargetContext dtc = null;
+        DropTargetContextAccessor acc =
+                AWTAccessor.getDropTargetContextAccessor();
 
         if (dt == null) {
             currentDT = null;
             currentT  = null;
 
             if (currentDTC != null) {
-                currentDTC.removeNotify();
+                acc.reset(currentDTC);
             }
 
             currentDTC = null;
@@ -387,13 +392,13 @@
         if (dt != currentDT) {
 
             if (currentDTC != null) {
-                currentDTC.removeNotify();
+                acc.reset(currentDTC);
             }
 
             currentDT  = dt;
             currentDTC = dt.getDropTargetContext();
 
-            currentDTC.addNotify(this);
+            acc.setDropTargetContextPeer(currentDTC, this);
         }
 
         dtc = currentDTC;
@@ -409,7 +414,7 @@
             currentDT = null;
             currentT  = null;
 
-            currentDTC.removeNotify();
+            acc.reset(currentDTC);
             currentDTC = null;
 
             local = null;
@@ -440,11 +445,13 @@
         int               id   = event.getID();
         DropTarget        dt   = c.getDropTarget();
         DropTargetContext dtc  = null;
+        DropTargetContextAccessor acc =
+                AWTAccessor.getDropTargetContextAccessor();
 
         if (c.isShowing() && (dt != null) && dt.isActive()) {
             if (currentDT != dt) {
                 if (currentDTC != null) {
-                    currentDTC.removeNotify();
+                    acc.reset(currentDTC);
                 }
 
                 currentDT  = dt;
@@ -454,11 +461,11 @@
             dtc = currentDT.getDropTargetContext();
             if (dtc != currentDTC) {
                 if (currentDTC != null) {
-                    currentDTC.removeNotify();
+                    acc.reset(currentDTC);
                 }
 
                 currentDTC = dtc;
-                currentDTC.addNotify(this);
+                acc.setDropTargetContextPeer(currentDTC, this);
             }
 
             currentA = currentDT.getDefaultActions();
@@ -518,13 +525,15 @@
             DropTargetContext dtc = dt.getDropTargetContext();
 
             currentDT = dt;
+            DropTargetContextAccessor acc =
+                    AWTAccessor.getDropTargetContextAccessor();
 
             if (currentDTC != null) {
-                currentDTC.removeNotify();
+                acc.reset(currentDTC);
             }
 
             currentDTC = dtc;
-            currentDTC.addNotify(this);
+            acc.setDropTargetContextPeer(currentDTC, this);
             currentA = dt.getDefaultActions();
 
             synchronized(_globalLock) {
@@ -687,7 +696,9 @@
             throw new InvalidDnDOperationException("No Drop pending");
         }
 
-        if (currentDTC != null) currentDTC.removeNotify();
+        if (currentDTC != null) {
+            AWTAccessor.getDropTargetContextAccessor().reset(currentDTC);
+        }
 
         currentDT  = null;
         currentDTC = null;
--- a/jdk/test/java/awt/dnd/DragSourceListenerSerializationTest/DragSourceListenerSerializationTest.java	Fri Apr 17 16:54:13 2015 +0300
+++ b/jdk/test/java/awt/dnd/DragSourceListenerSerializationTest/DragSourceListenerSerializationTest.java	Fri Apr 17 16:57:30 2015 +0300
@@ -74,7 +74,6 @@
                 new Point(100, 100),
                 Arrays.asList(me));
         DragSourceContext dsc = new DragSourceContext(
-                Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge),
                 dge,
                 new Cursor(Cursor.HAND_CURSOR),
                 null, null, new StringSelection("TEXT"), null);