Skip to content

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

Merged
guillerodriguez merged 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-flavors
Oct 28, 2026
Merged

guillerodriguez merged 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-flavors

Conversation

@duncanmcqueen

@duncanmcqueen duncanmcqueen commented Oct 14, 2026 •

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. Without a peer, or if the peer returns null (the GTK peer does this, because its getTransferDataFlavors() is a stub), it returns an empty array. Thus getCurrentDataFlavorsAsList() and isDataFlavorSupported() do not throw NullPointerException. I found this bug when I wrote an AWT toolkit for Haiku, which supplies drag and drop peers.

How to reproduce

The methods are not public, so the test calls them with reflection. It tests the context without a peer, with a peer that returns null (NullFlavorPeer), and with a peer that offers text (StubDropPeer).

javac StubDropPeer.java NullFlavorPeer.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, or an empty array when there is no peer or the peer gives
 * null. getCurrentDataFlavorsAsList() and isDataFlavorSupported() use
 * it. The methods are not public, so the test calls them with reflection.
 */
public class DropContextFlavors
{
  static boolean failed;
  static Method flavors, supported;

  static void test(String what, DropTargetContext dtc, int expected)
  {
    String got;
    boolean ok;
    try
      {
        DataFlavor[] f = (DataFlavor[]) flavors.invoke(dtc);
        boolean s = (Boolean) supported.invoke(dtc, DataFlavor.stringFlavor);
        got = (f == null ? "null" : Arrays.asList(f).toString())
              + ", isDataFlavorSupported(stringFlavor) = " + s;
        ok = f != null && f.length == expected && s == (expected == 1)
             && (expected == 0 || f[0].equals(DataFlavor.stringFlavor));
      }
    catch (Exception e)
      {
        Throwable t = e.getCause() != null ? e.getCause() : e;
        got = t.toString();
        ok = false;
      }
    System.out.println((ok ? "PASS " : "FAIL ") + what + ": " + got);
    failed |= !ok;
  }

  static void addNotify(DropTargetContext dtc, Object peer) throws Exception
  {
    Method m = DropTargetContext.class.getDeclaredMethod("addNotify",
      Class.forName("java.awt.dnd.peer.DropTargetContextPeer"));
    m.setAccessible(true);
    m.invoke(dtc, peer);
  }

  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();
    flavors = DropTargetContext.class.getDeclaredMethod("getCurrentDataFlavors");
    supported = DropTargetContext.class.getDeclaredMethod("isDataFlavorSupported",
                                                          DataFlavor.class);
    flavors.setAccessible(true);
    supported.setAccessible(true);

    test("without a peer", dtc, 0);
    addNotify(dtc, new NullFlavorPeer(dt));
    test("peer gives null", dtc, 0);
    addNotify(dtc, new StubDropPeer(dt, DnDConstants.ACTION_COPY));
    test("peer gives stringFlavor", dtc, 1);
    System.exit(failed ? 1 : 0);
  }
}
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) {}
}
NullFlavorPeer.java
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;

/** A StubDropPeer whose getTransferDataFlavors() returns null, as the GTK peer does. */
public class NullFlavorPeer extends StubDropPeer
{
  NullFlavorPeer(DropTarget target) { super(target, DnDConstants.ACTION_COPY); }
  public DataFlavor[] getTransferDataFlavors() { return null; }
}

Results

Haiku R1/beta6 x86_64 (hrev60228), JamVM with the Haiku port (#25 in ingelabs/jamvm), and ingelabs/classpath built on Haiku. I compiled each version of the changed classes with javac from OpenJDK 8 and put them first on the boot class path (-Xbootclasspath/p). "Before" is master. "Previous" is the first version of this pull request. "After" is this version.

Before:
FAIL without a peer: java.lang.NullPointerException
FAIL peer gives null: java.lang.NullPointerException
FAIL peer gives stringFlavor: java.lang.NullPointerException

Previous:
FAIL without a peer: java.lang.NullPointerException
FAIL peer gives null: java.lang.NullPointerException
PASS peer gives stringFlavor: [java.awt.datatransfer.DataFlavor[representationClass=java.lang.String,mimeType=application/x-java-serialized-object,humanPresentableName=Java Unicode String]], isDataFlavorSupported(stringFlavor) = true

After:
PASS without a peer: [], isDataFlavorSupported(stringFlavor) = false
PASS peer gives null: [], isDataFlavorSupported(stringFlavor) = false
PASS peer gives stringFlavor: [java.awt.datatransfer.DataFlavor[representationClass=java.lang.String,mimeType=application/x-java-serialized-object,humanPresentableName=Java Unicode String]], isDataFlavorSupported(stringFlavor) = true

The first version gave the "Previous" result for the stub peer also on Linux, and JDK 8 (1.8.0_504) also returns the string flavor. OpenJDK 27 on Linux returns an empty array without a peer.

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
@guillerodriguez

Copy link
Copy Markdown
Contributor

Thanks, returning the peer’s result is the right fix.

One minor thing. Without a peer, getCurrentDataFlavors() still returns null. The GTK peer also returns null, because its getTransferDataFlavors() is a stub. In those cases getCurrentDataFlavorsAsList() and isDataFlavorSupported() still fail with an NPE. This isn’t a regression, since before the fix they failed in every case, but since we will be touching this code, we can fix that as well and return an empty array instead:

protected DataFlavor[] getCurrentDataFlavors ()
{
  DataFlavor[] flavors = null;
  if (dtcp != null)
    flavors = dtcp.getTransferDataFlavors();
  return flavors != null ? flavors : new DataFlavor[0];
}

What do you think?

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.

Without a peer, or if the peer returns null (as the GTK peer does),
return an empty array, so that getCurrentDataFlavorsAsList() and
isDataFlavorSupported() do not throw NullPointerException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
@duncanmcqueen

Copy link
Copy Markdown
Contributor Author

Thank you. I agree, and I used your code. Without a peer, or if the peer returns null, getCurrentDataFlavors() now returns an empty array. The commit is amended.

I added two cases to the test: no peer, and a peer that returns null. On Haiku with JamVM, all three cases pass. Before this change, getCurrentDataFlavorsAsList() and isDataFlavorSupported() threw NullPointerException in the two new cases. OpenJDK 27 also returns an empty array without a peer. I updated the description.

@guillerodriguez
guillerodriguez merged commit f78b4c0 into ingelabs:master Oct 28, 2026
3 checks passed
@duncanmcqueen
duncanmcqueen deleted the fix-dtc-flavors branch October 29, 2026 02:13
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.

2 participants