2

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;
}
napulitano
  • 61
  • 7
  • Not sure why this class was removed in JDK 8. Still, if you read [the source code](http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/50c67678b0d1/src/share/classes/sun/swing/AccessibleMethod.java) it's just a wrapper for reflection methods. You can copy/paste this code in your project in a custom class and replace `sun.swing.AccessibleMethod` by usage of this custom class. – Luiggi Mendoza Oct 15 '14 at 15:03
  • Tanks for your answer @LuiggiMendoza, yes, it is a good idea to write my own wrapper class but I hope someone know why this class has been removed, it's very weird. – napulitano Oct 15 '14 at 15:19
  • 10
    Using sun.* classes is bad practice [Why Developers Should Not Write Programs That Call 'sun' Packages](http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html) and http://stackoverflow.com/q/1834826/2670892 – greg-449 Oct 15 '14 at 15:21

2 Answers2

10

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:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
5

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...

assylias
  • 321,522
  • 82
  • 660
  • 783
Holger
  • 285,553
  • 42
  • 434
  • 765