Skip to content

Accept valid source actions in drop target events - #70

Open
duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-drop-event-actions
Open

duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-drop-event-actions

Conversation

@duncanmcqueen

Copy link
Copy Markdown
Contributor

Split from #57.

Problem and fix

The constructors of DropTargetDragEvent and DropTargetDropEvent test the source actions with ~(actions ^ mask) != 0. This test is true for all values except ~mask. Thus both constructors throw IllegalArgumentException for all valid actions, for example ACTION_COPY_OR_MOVE, and a drop target never gets an event.

Now the constructors reject only bits that are not in the mask: (actions & ~mask) != 0. I found this bug when I wrote an AWT toolkit for Haiku, which supplies drag and drop peers.

How to reproduce

javac DropEventActions.java
java DropEventActions
DropEventActions.java
import java.awt.*;
import java.awt.dnd.*;

/**
 * DropTargetDragEvent and DropTargetDropEvent must accept valid source
 * actions, and reject bits that are not actions.
 */
public class DropEventActions
{
  static boolean failed;

  static void check(String what, boolean ok, String detail)
  {
    System.out.println((ok ? "PASS " : "FAIL ") + what + ": " + detail);
    failed |= !ok;
  }

  public static void main(String[] args)
  {
    DropTarget dt = new DropTarget(new Canvas(), new DropTargetAdapter()
      {
        public void drop(DropTargetDropEvent e) {}
      });
    DropTargetContext dtc = dt.getDropTargetContext();
    Point p = new Point(1, 1);
    int copy = DnDConstants.ACTION_COPY;
    int copyOrMove = DnDConstants.ACTION_COPY_OR_MOVE;
    try
      {
        new DropTargetDragEvent(dtc, p, copy, copyOrMove);
        check("new DropTargetDragEvent(ACTION_COPY_OR_MOVE)", true, "accepted");
      }
    catch (IllegalArgumentException e)
      {
        check("new DropTargetDragEvent(ACTION_COPY_OR_MOVE)", false, e.toString());
      }
    try
      {
        new DropTargetDropEvent(dtc, p, copy, copyOrMove);
        check("new DropTargetDropEvent(ACTION_COPY_OR_MOVE)", true, "accepted");
      }
    catch (IllegalArgumentException e)
      {
        check("new DropTargetDropEvent(ACTION_COPY_OR_MOVE)", false, e.toString());
      }
    try
      {
        new DropTargetDragEvent(dtc, p, copy, 0x100);
        check("new DropTargetDragEvent(0x100)", false, "accepted an invalid action");
      }
    catch (IllegalArgumentException e)
      {
        check("new DropTargetDragEvent(0x100)", true, "rejected");
      }
    System.exit(failed ? 1 : 0);
  }
}

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 new DropTargetDragEvent(ACTION_COPY_OR_MOVE): java.lang.IllegalArgumentException
FAIL new DropTargetDropEvent(ACTION_COPY_OR_MOVE): java.lang.IllegalArgumentException
PASS new DropTargetDragEvent(0x100): rejected

After:
PASS new DropTargetDragEvent(ACTION_COPY_OR_MOVE): accepted
PASS new DropTargetDropEvent(ACTION_COPY_OR_MOVE): accepted
PASS new DropTargetDragEvent(0x100): rejected

JDK 8 (1.8.0_504) gives the "After" result.

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
DropTargetDragEvent and DropTargetDropEvent tested the source actions
with ~(actions ^ mask) != 0. That test is true for every value except
~mask, so both constructors threw IllegalArgumentException for all
valid actions, for example ACTION_COPY_OR_MOVE. Reject only bits that
are not in the mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
@duncanmcqueen
duncanmcqueen force-pushed the fix-drop-event-actions branch from a1186fe to 13434e1 Compare October 15, 2026 13:51
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