JDK-7021001 : Default implementation of Toolkit.loadSystemColors(int[]) and many others doesn't throw HE in hl env
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-02-21
  • Updated: 2012-03-22
  • Resolved: 2011-05-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b140Fixed
Related Reports
Relates :  
Relates :  
Description
Platform: all
JDK: 1.1+
Class. java.awt.Toolkit
Methods:
example - http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#loadSystemColors%28int[]%29
and the same with:
          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#setDynamicLayout%28boolean%29
          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#getMenuShortcutKeyMask%28%29
          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#getLockingKeyState%28int%29
          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#setLockingKeyState%28int,%20boolean%29
          


    ------------
    protected void loadSystemColors(int[] systemColors) throws HeadlessException
    Throws:
        HeadlessException - if GraphicsEnvironment.isHeadless() returns true
    ------------

In fact, default version of this method doesn't throw HE in HeadlessEnvironment

Please see, compile and execute with "-Djava.awt.headless=true" the following code sample:
HeadlessException won't be thrown

-----------------------------------------------------
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.dnd.*;
import java.awt.dnd.peer.DragSourceContextPeer;
import java.awt.font.TextAttribute;
import java.awt.im.InputMethodHighlight;
import java.awt.image.*;
import java.awt.peer.*;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

public class NoHE {

    public static void main(String[] args)  {
         new _Toolkit().loadSystemColors(new int[10]);
    }

    static class _Toolkit extends Toolkit {

        @Override
        public void loadSystemColors(int[] systemColors) throws HeadlessException {
            super.loadSystemColors(systemColors);
        }

        @Override
        protected DesktopPeer createDesktopPeer(Desktop target) throws HeadlessException {
            return null;
        }

        @Override
        protected ButtonPeer createButton(Button target) throws HeadlessException {
            return null;
        }

        @Override
        protected TextFieldPeer createTextField(TextField target) throws HeadlessException {
            return null;
        }

        @Override
        protected LabelPeer createLabel(Label target) throws HeadlessException {
            return null;
        }

        @Override
        protected ListPeer createList(List target) throws HeadlessException {
            return null;
        }

        @Override
        protected CheckboxPeer createCheckbox(Checkbox target) throws HeadlessException {
            return null;
        }

        @Override
        protected ScrollbarPeer createScrollbar(Scrollbar target) throws HeadlessException {
            return null;
        }

        @Override
        protected ScrollPanePeer createScrollPane(ScrollPane target) throws HeadlessException {
            return null;
        }

        @Override
        protected TextAreaPeer createTextArea(TextArea target) throws HeadlessException {
            return null;
        }

        @Override
        protected ChoicePeer createChoice(Choice target) throws HeadlessException {
            return null;
        }

        @Override
        protected FramePeer createFrame(Frame target) throws HeadlessException {
            return null;
        }

        @Override
        protected CanvasPeer createCanvas(Canvas target) {
            return null;
        }

        @Override
        protected PanelPeer createPanel(Panel target) {
            return null;
        }

        @Override
        protected WindowPeer createWindow(Window target) throws HeadlessException {
            return null;
        }

        @Override
        protected DialogPeer createDialog(Dialog target) throws HeadlessException {
            return null;
        }

        @Override
        protected MenuBarPeer createMenuBar(MenuBar target) throws HeadlessException {
            return null;
        }

        @Override
        protected MenuPeer createMenu(Menu target) throws HeadlessException {
            return null;
        }

        @Override
        protected PopupMenuPeer createPopupMenu(PopupMenu target) throws HeadlessException {
            return null;
        }

        @Override
        protected MenuItemPeer createMenuItem(MenuItem target) throws HeadlessException {
            return null;
        }

        @Override
        protected FileDialogPeer createFileDialog(FileDialog target) throws HeadlessException {
            return null;
        }

        @Override
        protected CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) throws HeadlessException {
            return null;
        }

        @Override
        protected FontPeer getFontPeer(String name, int style) {
            return null;
        }

        @Override
        public Dimension getScreenSize() throws HeadlessException {
            return null;
        }

        @Override
        public int getScreenResolution() throws HeadlessException {
            return 0;
        }

        @Override
        public ColorModel getColorModel() throws HeadlessException {
            return null;
        }

        @Override
        public String[] getFontList() {
            return new String[0];
        }

        @Override
        public FontMetrics getFontMetrics(Font font) {
            return null;
        }

        @Override
        public void sync() {

        }

        @Override
        public Image getImage(String filename) {
            return null;
        }

        @Override
        public Image getImage(URL url) {
            return null;
        }

        @Override
        public Image createImage(String filename) {
            return null;
        }

        @Override
        public Image createImage(URL url) {
            return null;
        }

        @Override
        public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
            return false;
        }

        @Override
        public int checkImage(Image image, int width, int height, ImageObserver observer) {
            return 0;
        }

        @Override
        public Image createImage(ImageProducer producer) {
            return null;
        }

        @Override
        public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
            return null;
        }

        @Override
        public PrintJob getPrintJob(Frame frame, String jobtitle, Properties props) {
            return null;
        }

        @Override
        public void beep() {

        }

        @Override
        public Clipboard getSystemClipboard() throws HeadlessException {
            return null;
        }

        @Override
        protected EventQueue getSystemEventQueueImpl() {
            return null;
        }

        @Override
        public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
            return null;
        }

        @Override
        public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
            return false;
        }

        @Override
        public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType modalExclusionType) {
            return false;
        }

        @Override
        public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight) throws HeadlessException {
            return null;
        }
    }
}

-----------------------------------------------------
The following methods are still not fixed in JDK7 b140

          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#getLockingKeyState%28int%29
          http://download.java.net/jdk7/docs/api/java/awt/Toolkit.html#setLockingKeyState%28int,%20boolean%29

The following tests still fail

api/java_awt/Toolkit/indexTGF.html#HeadlessExceptions[setLockingKeyState_defaultMethodImpl]
api/java_awt/Toolkit/indexTGF.html#HeadlessExceptions[getLockingKeyState_defaultMethodImpl]

Comments
SUGGESTED FIX diff --git a/src/share/classes/java/awt/Toolkit.java b/src/share/classes/java/awt/Toolkit.java --- a/src/share/classes/java/awt/Toolkit.java +++ b/src/share/classes/java/awt/Toolkit.java @@ -466,6 +466,9 @@ */ protected void loadSystemColors(int[] systemColors) throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } } /** @@ -500,6 +503,9 @@ */ public void setDynamicLayout(boolean dynamic) throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } } /** @@ -523,6 +529,10 @@ */ protected boolean isDynamicLayoutSet() throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + if (this != Toolkit.getDefaultToolkit()) { return Toolkit.getDefaultToolkit().isDynamicLayoutSet(); } else { @@ -601,6 +611,10 @@ */ public Insets getScreenInsets(GraphicsConfiguration gc) throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + if (this != Toolkit.getDefaultToolkit()) { return Toolkit.getDefaultToolkit().getScreenInsets(gc); } else { @@ -1342,6 +1356,10 @@ * @since 1.4 */ public Clipboard getSystemSelection() throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + if (this != Toolkit.getDefaultToolkit()) { return Toolkit.getDefaultToolkit().getSystemSelection(); } else { @@ -1371,6 +1389,10 @@ * @since JDK1.1 */ public int getMenuShortcutKeyMask() throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + return Event.CTRL_MASK; } @@ -1499,6 +1521,10 @@ */ public Dimension getBestCursorSize(int preferredWidth, int preferredHeight) throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + // Override to implement custom cursor support. if (this != Toolkit.getDefaultToolkit()) { return Toolkit.getDefaultToolkit(). @@ -1526,6 +1552,10 @@ * @since 1.2 */ public int getMaximumCursorColors() throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + // Override to implement custom cursor support. if (this != Toolkit.getDefaultToolkit()) { return Toolkit.getDefaultToolkit().getMaximumCursorColors(); @@ -2572,6 +2602,10 @@ * @since 1.7 */ public boolean areExtraMouseButtonsEnabled() throws HeadlessException { + if (GraphicsEnvironment.isHeadless()){ + throw new HeadlessException(); + } + return Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled(); } }
08-04-2011

EVALUATION Indeed we do not throw the exception.
25-02-2011