Skip to content

DropTargetContext.getCurrentDataFlavors(): return the flavors - #71

Open
duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-flavors
Open

duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-flavors

Conversation

@duncanmcqueen

Copy link
Copy Markdown
Contributor

Split from #57.

Problem and fix

DropTargetContext.getCurrentDataFlavors() calls getTransferDataFlavors() of the peer, but it does not return the result. It always returns null. Thus DropTargetDragEvent.getCurrentDataFlavors() and DropTargetDropEvent.getCurrentDataFlavors() return null, and isDataFlavorSupported() is always false.

Now the method returns the flavors of the peer. I found this bug when I wrote an AWT toolkit for Haiku, which supplies drag and drop peers.

How to reproduce

The method is protected, so the test calls it with reflection. It gives the context a peer that offers text (StubDropPeer, below).

javac StubDropPeer.java DropContextFlavors.java
java DropContextFlavors
DropContextFlavors.java
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * DropTargetContext.getCurrentDataFlavors() must return the flavors of
 * its peer. DropTargetDragEvent.getCurrentDataFlavors() and
 * isDataFlavorSupported() use it. The method is protected, so the test
 * calls it with reflection.
 */
public class DropContextFlavors
{
  public static void main(String[] args) throws Exception
  {
    DropTarget dt = new DropTarget(new Canvas(), new DropTargetAdapter()
      {
        public void drop(DropTargetDropEvent e) {}
      });
    DropTargetContext dtc = dt.getDropTargetContext();
    dtc.addNotify(new StubDropPeer(dt, DnDConstants.ACTION_COPY));
    Method m = DropTargetContext.class.getDeclaredMethod("getCurrentDataFlavors");
    m.setAccessible(true);
    DataFlavor[] f = (DataFlavor[]) m.invoke(dtc);
    boolean ok = f != null && f.length == 1
                 && f[0].equals(DataFlavor.stringFlavor);
    System.out.println((ok ? "PASS" : "FAIL") + ": getCurrentDataFlavors() = "
                       + (f == null ? "null" : Arrays.asList(f).toString()));
    System.exit(ok ? 0 : 1);
  }
}
StubDropPeer.java
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.dnd.peer.DropTargetContextPeer;

/** A DropTargetContextPeer that offers text and the given target actions. */
public class StubDropPeer implements DropTargetContextPeer
{
  int actions;
  DropTarget target;

  StubDropPeer(DropTarget target, int actions)
  {
    this.target = target;
    this.actions = actions;
  }

  public void setTargetActions(int a) { actions = a; }
  public int getTargetActions() { return actions; }
  public DropTarget getDropTarget() { return target; }
  public DataFlavor[] getTransferDataFlavors()
  {
    return new DataFlavor[] { DataFlavor.stringFlavor };
  }
  public Transferable getTransferable() { return new StringSelection("text"); }
  public boolean isTransferableJVMLocal() { return true; }
  public void acceptDrag(int a) {}
  public void rejectDrag() {}
  public void acceptDrop(int a) {}
  public void rejectDrop() {}
  public void dropComplete(boolean ok) {}
}

Results

Linux x86_64, ingelabs/classpath master (0406074), JamVM master, javac from JDK 8, autoconf 2.73, automake 1.19, the configure options of the CI Linux job, out-of-tree build. "Before" is master. "After" is master with this commit.

Before:
FAIL: getCurrentDataFlavors() = null

After:
PASS: getCurrentDataFlavors() = [java.awt.datatransfer.DataFlavor[representationClass=java.lang.String,mimeType=application/x-java-serialized-object,humanPresentableName=Java Unicode String]]

JDK 8 (1.8.0_504) also returns the string flavor.

Platforms

Not specific to Haiku. The problem is in java.awt.dnd and occurs with all toolkits. I reproduced it on Linux.

AI assistance

An AI model (Claude Code, by Anthropic) wrote this change, the test and this text. The repository owner directed the work and checked the results. Please review it as you review other contributions.

🤖 Generated with Claude Code

@duncanmcqueen
duncanmcqueen marked this pull request as ready for review October 14, 2026 23:52
getCurrentDataFlavors() called the peer but did not return its result,
so it always returned null. Thus DropTargetDragEvent and
DropTargetDropEvent did not show the offered data flavors, and
isDataFlavorSupported() was always false. Return the peer's flavors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant