Bug 142299 [JFace] Class Dialog cannot be loaded in non-UI thread
The static initializer of org.eclipse.jface.dialogs.Dialog cannot be executed in a non-UI thread. The problematic call-chain is: JFaceResources.getImageRegistry() -> Display.getCurrent(). In a non-UI thread this returns null, causing an NPE in the static initializer. As the result, this class fails to load. I stumbled over this because I wanted to perform aspect weaving (using the new hooks in the OSGI) on a subclass of Dialog. For that purpose I had to force loading these classes from infrastructure code, perhaps even before the UI was up and running. Another (hypothetical) scenario where this will cause problems would be a configurable factory, which could be programmed like this: Class myDialogClass; void init() { myDialogClass = Class.forName(readDialogClassName()); } Dialog createDialog() { return (Dialog)myDialogClass.newInstance(); } Also in this case method init() would crash if invoked within a non-UI thread. IMHO, a static initializer should never make any assumptions about the context it is being executed in. The code in question seems to exist for backward compatibility only. (Typo in javadoc: @see Display#getSystemIcon(int ID), should read: getSystemImage, the same typo in ImageRegistry.get()). In fact ImageRegistry.get(String) re-maps the deprecated keys from Dialog to the new ones in SWT, so I believe the code in the static initializer has not even any effect, the entries entered into the image registry will never be retrieved by that name. Correct me if I'm wrong.
bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/Dialog.java
* @since 2.0
	 * @deprecated Use <code>getButton(IDialogConstants.CANCEL_ID)</code>
	 *             instead. This method will be removed soon.
	 */
	protected Button getCancelButton() {
		return getButton(IDialogConstants.CANCEL_ID);
	}
	/**
	 * Returns the dialog area control.
	 * <p>
	 * Clients may call this framework method, but should not override it.
	 * </p>
	 * 
	 * @return the dialog area, or <code>null</code> if the dialog area has
	 *         not been created yet
	 */
	protected Control getDialogArea() {
		return dialogArea;
	}
	/**
	 * Returns the standard dialog image with the given key. Note that these
	 * images are managed by the dialog framework, and must not be disposed by
	 * another party.
	 * 
	 * @param key
	 *            one of the <code>Dialog.DLG_IMG_* </code> constants
	 * @return the standard dialog image
	 */
	public static Image getImage(String key) {
		return JFaceResources.getImageRegistry().get(key);