--- old/src/java.desktop/share/classes/java/awt/Component.java 2015-03-25 18:23:17.384767500 +0400 +++ new/src/java.desktop/share/classes/java/awt/Component.java 2015-03-25 18:23:17.197567200 +0400 @@ -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! } @@ -7007,7 +7007,7 @@ popup.addNotify(); } - if (dropTarget != null) dropTarget.addNotify(peer); + if (dropTarget != null) dropTarget.addNotify(); peerFont = getFont(); @@ -7094,7 +7094,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) { --- old/src/java.desktop/share/classes/java/awt/dnd/DragSource.java 2015-03-25 18:23:19.881772000 +0400 +++ new/src/java.desktop/share/classes/java/awt/dnd/DragSource.java 2015-03-25 18:23:19.663371600 +0400 @@ -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 DragSourceContext, * but NullPointerException is not thrown. * - * @param dscp The DragSourceContextPeer for this drag * @param dgl The DragGestureEvent that triggered the * drag * @param dragCursor The initial {@code Cursor} for this drag operation @@ -473,8 +468,13 @@ * event are equal to DnDConstants.ACTION_NONE. */ - 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); } /** --- old/src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java 2015-03-25 18:23:21.722575200 +0400 +++ new/src/java.desktop/share/classes/java/awt/dnd/DragSourceContext.java 2015-03-25 18:23:21.535374900 +0400 @@ -29,21 +29,20 @@ 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.ObjectOutputStream; import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; - import java.util.TooManyListenersException; +import sun.awt.AWTAccessor; + /** * The DragSourceContext class is responsible for managing the * initiator side of the Drag and Drop protocol. In particular, it is responsible @@ -123,6 +122,10 @@ protected static final int CHANGED = 3; + static { + AWTAccessor.setDragSourceContextAccessor(dsc -> dsc.peer); + } + /** * Called from DragSource, this constructor creates a new * DragSourceContext given the @@ -155,7 +158,6 @@ * If DragSourceListener is null no exception * is thrown. * - * @param dscp the DragSourceContextPeer 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. --- old/src/java.desktop/share/classes/java/awt/dnd/DropTarget.java 2015-03-25 18:23:23.610178500 +0400 +++ new/src/java.desktop/share/classes/java/awt/dnd/DropTarget.java 2015-03-25 18:23:23.391778100 +0400 @@ -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; /** --- old/src/java.desktop/share/classes/java/awt/dnd/DropTargetContext.java 2015-03-25 18:23:25.632091600 +0400 +++ new/src/java.desktop/share/classes/java/awt/dnd/DropTargetContext.java 2015-03-25 18:23:25.413079000 +0400 @@ -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 DropTargetContext 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 DropTargetContext * given a specified DropTarget. @@ -90,20 +105,9 @@ public Component getComponent() { return dropTarget.getComponent(); } /** - * Called when associated with the DropTargetContextPeer. - * - * @param dtcp the DropTargetContextPeer - */ - - public void addNotify(DropTargetContextPeer dtcp) { - dropTargetContextPeer = dtcp; - } - - /** * Called when disassociated with the DropTargetContextPeer. */ - - 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; --- old/src/java.desktop/share/classes/sun/awt/AWTAccessor.java 2015-03-25 18:23:27.472196800 +0400 +++ new/src/java.desktop/share/classes/sun/awt/AWTAccessor.java 2015-03-25 18:23:27.272185400 +0400 @@ -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; + } + } --- old/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java 2015-03-25 18:23:29.398307000 +0400 +++ new/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java 2015-03-25 18:23:29.205295900 +0400 @@ -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; --- old/test/java/awt/dnd/DragSourceListenerSerializationTest/DragSourceListenerSerializationTest.java 2015-03-25 18:23:31.283414800 +0400 +++ new/test/java/awt/dnd/DragSourceListenerSerializationTest/DragSourceListenerSerializationTest.java 2015-03-25 18:23:31.082403300 +0400 @@ -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);