napulitano
napulitano

Reputation: 61

Why sun.swing.AccessibleMethod is gone from JDK 8?

I'm wondering if someone know why sun.swing.AccessibleMethod is gone from JDK 8 and if there is some alternative to this class in JDK 8? I can't find any information about that anywhere. I use this class in my own implementation DropHandler. Code snippet where I use sun.swing.AccessibleMethod looks like this:

private DropLocation getDropLocation(DropTargetEvent e)
{
    DropLocation dropLocation = null;
    if (this.component != null)
    {
        try
        {
            Point p = e instanceof DropTargetDragEvent ? ((DropTargetDragEvent)e).getLocation() : ((DropTargetDropEvent) e).getLocation();
            AccessibleMethod method = new AccessibleMethod(JComponent.class,
            "dropLocationForPoint",
            Point.class);

            dropLocation = (DropLocation) method.invokeNoChecked(this.component, p);
        }
        catch (NoSuchMethodException ex)
        {
            LOGGER.info(ex.getMessage());
        }
    }

    return dropLocation;
}

Upvotes: 2

Views: 464

Answers (2)

Holger
Holger

Reputation: 298579

What’s the point about this AccessibleMethod class?

The following uses standard Java API which exists since Java 1.2:

try {
    Method method = JComponent.class
                              .getDeclaredMethod("dropLocationForPoint", Point.class);
    method.setAccessible(true);
    dropLocation = (DropLocation) method.invoke(this.component, p);
} catch(NoSuchMethodException|IllegalAccessException|InvocationTargetException ex) {
    Logger.info(ex.getMessage());
}

However, don’t come back and ask why JComponent.dropLocationForPoint has been removed, if that happens in the future. If you are accessing non-standard APIs you may encounter such problems. To be exact, there were always Java implementations not having these features your code relies on...

Upvotes: 5

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85799

As explained in this official post from Oracle: Why Developers Should Not Write Programs That Call 'sun' Packages (thanks to @greg-449 for providing this info):

The sun.* packages are not part of the supported, public interface.

A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.

So, you should not have relied on sun.swing.AccessibleMethod class in the first place.

More info:


As a solution for your problem, you can do the following:

Upvotes: 10

Related Questions