8203224: java.awt.desktop.*Event classes could not be instantiated if Desktop feature is not supported

Reviewed-by: prr, kaddepalli
This commit is contained in:
Sergey Bylokhov 2018-06-06 15:37:10 -07:00
parent 1791b54bf2
commit fc1016ce18
15 changed files with 288 additions and 51 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,9 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application is asked to open its about window.
@ -34,12 +37,19 @@ package java.awt.desktop;
* @since 9
*/
public final class AboutEvent extends AppEvent {
private static final long serialVersionUID = -5987180734802756477L;
/**
* Constructs an {@code AboutEvent}
* Constructs an {@code AboutEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public AboutEvent() {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,11 +26,13 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.util.EventObject;
/**
* AppEvents are sent to listeners and handlers installed on the
* {@link java.awt.Desktop}.
* {@link java.awt.Desktop} instance of the current desktop context.
*
* @since 9
*/
@ -38,8 +40,17 @@ public class AppEvent extends EventObject {
private static final long serialVersionUID = -5958503993556009432L;
/**
* Constructs an {@code AppEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
AppEvent() {
super(Desktop.getDesktop());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,9 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application has become the foreground app, and when it is
@ -36,12 +39,19 @@ package java.awt.desktop;
* @since 9
*/
public final class AppForegroundEvent extends AppEvent {
private static final long serialVersionUID = -5513582555740533911L;
/**
* Constructs an {@code AppForegroundEvent}
* Constructs an {@code AppForegroundEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public AppForegroundEvent() {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,9 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application has been hidden or shown.
@ -35,12 +38,19 @@ package java.awt.desktop;
* @since 9
*/
public final class AppHiddenEvent extends AppEvent {
private static final long serialVersionUID = 2637465279476429224L;
/**
* Constructs an {@code AppHiddenEvent}
* Constructs an {@code AppHiddenEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public AppHiddenEvent() {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,9 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application is asked to re-open itself.
@ -34,12 +37,19 @@ package java.awt.desktop;
* @since 9
*/
public final class AppReopenedEvent extends AppEvent {
private static final long serialVersionUID = 1503238361530407990L;
/**
* Constructs an {@code AppReopenedEvent}
* Constructs an {@code AppReopenedEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public AppReopenedEvent() {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,30 +25,41 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Auxiliary event containing a list of files.
*
* @since 9
*/
public class FilesEvent extends AppEvent {
private static final long serialVersionUID = 5271763715462312871L;
final List<File> files;
/**
* Constructs a {@code FilesEvent}
* @param files files
* Constructs a {@code FilesEvent}.
*
* @param files the list of files
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
FilesEvent(final List<File> files) {
this.files = files;
}
/**
* Gets the list of files
* Gets the list of files.
*
* @return the list of files
*/
public List<File> getFiles() {
@ -56,5 +67,4 @@ public class FilesEvent extends AppEvent {
? null
: new ArrayList<>(files);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,10 +25,12 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.io.File;
import java.util.List;
/**
* Event sent when the app is asked to open a list of files.
*
@ -37,13 +39,21 @@ import java.util.List;
* @since 9
*/
public final class OpenFilesEvent extends FilesEvent {
private static final long serialVersionUID = -3982871005867718956L;
final String searchTerm;
/**
* Constructs an {@code OpenFilesEvent}
* @param files files
* @param searchTerm searchTerm
* Constructs an {@code OpenFilesEvent}.
*
* @param files the list of files
* @param searchTerm the search term
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public OpenFilesEvent(final List<File> files, final String searchTerm) {
super(files);
@ -57,7 +67,7 @@ public final class OpenFilesEvent extends FilesEvent {
* term that was used to find the files. This is for example the case
* on Mac OS X, when the files were opened using the Spotlight search
* menu or a Finder search window.
*
* <p>
* This is useful for highlighting the search term in the documents when
* they are opened.
*
@ -66,5 +76,4 @@ public final class OpenFilesEvent extends FilesEvent {
public String getSearchTerm() {
return searchTerm;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,11 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.net.URI;
/**
* Event sent when the app is asked to open a {@code URI}.
*
@ -36,12 +38,20 @@ import java.net.URI;
* @since 9
*/
public final class OpenURIEvent extends AppEvent {
private static final long serialVersionUID = 221209100935933476L;
final URI uri;
/**
* Constructs an {@code OpenURIEvent}
* @param uri {@code URI}
* Constructs an {@code OpenURIEvent}.
*
* @param uri the {@code URI} the app was asked to open
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public OpenURIEvent(final URI uri) {
this.uri = uri;
@ -54,5 +64,4 @@ public final class OpenURIEvent extends AppEvent {
public URI getURI() {
return uri;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,9 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application is asked to open its preferences window.
@ -34,12 +37,19 @@ package java.awt.desktop;
* @since 9
*/
public final class PreferencesEvent extends AppEvent {
private static final long serialVersionUID = -6398607097086476160L;
/**
* Constructs a {@code PreferencesEvent}
* Constructs a {@code PreferencesEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public PreferencesEvent() {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,13 +23,14 @@
* questions.
*/
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.io.File;
import java.util.List;
/**
* Event sent when the app is asked to print a list of files.
*
@ -37,14 +38,21 @@ import java.util.List;
* @since 9
*/
public final class PrintFilesEvent extends FilesEvent {
private static final long serialVersionUID = -5752560876153618618L;
/**
* Constructs a {@code PrintFilesEvent}
* @param files files
* Constructs a {@code PrintFilesEvent}.
*
* @param files the list of files
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public PrintFilesEvent(final List<File> files) {
super(files);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,10 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the application is asked to quit.
*
@ -37,7 +41,14 @@ public final class QuitEvent extends AppEvent {
private static final long serialVersionUID = -256100795532403146L;
/**
* Constructs a {@code QuitEvent}
* Constructs a {@code QuitEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public QuitEvent() {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,13 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the displays attached to the system enter and exit power save
* sleep.
@ -38,7 +43,14 @@ public final class ScreenSleepEvent extends AppEvent {
private static final long serialVersionUID = 7521606180376544150L;
/**
* Constructs a ScreenSleepEvent
* Constructs a {@code ScreenSleepEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public ScreenSleepEvent() {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,10 @@
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the system enters and exits power save sleep.
*
@ -38,7 +42,14 @@ public final class SystemSleepEvent extends AppEvent {
private static final long serialVersionUID = 11372269824930549L;
/**
* Constructs a SystemSleepEvent
* Constructs a {@code SystemSleepEvent}.
*
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public SystemSleepEvent() {
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,13 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.desktop;
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
/**
* Event sent when the user session has been changed.
*
@ -66,9 +71,15 @@ public final class UserSessionEvent extends AppEvent {
}
/**
* Constructs a {@code UserSessionEvent}
* Constructs a {@code UserSessionEvent}.
*
* @param reason of session change
* @param reason the reason of the user session change
* @throws HeadlessException if {@link GraphicsEnvironment#isHeadless()}
* returns {@code true}
* @throws UnsupportedOperationException if Desktop API is not supported on
* the current platform
* @see Desktop#isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public UserSessionEvent(Reason reason) {
this.reason = reason;

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.desktop.AboutEvent;
import java.awt.desktop.AppForegroundEvent;
import java.awt.desktop.AppHiddenEvent;
import java.awt.desktop.AppReopenedEvent;
import java.awt.desktop.OpenFilesEvent;
import java.awt.desktop.OpenURIEvent;
import java.awt.desktop.PreferencesEvent;
import java.awt.desktop.PrintFilesEvent;
import java.awt.desktop.QuitEvent;
import java.awt.desktop.ScreenSleepEvent;
import java.awt.desktop.SystemSleepEvent;
import java.awt.desktop.UserSessionEvent;
import java.awt.desktop.UserSessionEvent.Reason;
import java.io.File;
import java.util.Collections;
import java.util.List;
/**
* @test
* @bug 8203224
* @summary tests that the correct exceptions are thrown by the events classes
* in {code java.awt.desktop} package
* @run main/othervm DesktopEventsExceptions
* @run main/othervm -Djava.awt.headless=true DesktopEventsExceptions
*/
public final class DesktopEventsExceptions {
public static void main(final String[] args) {
// Each element of the list will creates one object to test
final List<Runnable> constructors = List.of(
AboutEvent::new,
AppForegroundEvent::new,
AppHiddenEvent::new,
AppReopenedEvent::new,
QuitEvent::new,
ScreenSleepEvent::new,
SystemSleepEvent::new,
PreferencesEvent::new,
() -> new PrintFilesEvent(Collections.emptyList()),
() -> new UserSessionEvent(Reason.UNSPECIFIED),
() -> new OpenFilesEvent(Collections.emptyList(), ""),
() -> new OpenURIEvent(new File("").toURI())
);
for (final Runnable test : constructors) {
try {
test.run();
checkHeadless(true);
checkSupported(true);
} catch (HeadlessException ex) {
checkHeadless(false);
} catch (UnsupportedOperationException ex) {
checkSupported(false);
}
}
}
private static void checkSupported(final boolean isSupported) {
if (isSupported != Desktop.isDesktopSupported()) {
throw new RuntimeException();
}
}
private static void checkHeadless(final boolean isHeadless) {
if (isHeadless == GraphicsEnvironment.isHeadless()) {
throw new RuntimeException();
}
}
}