Bug 208807 - JavaCore.getDefaultOptions() throws an exception when there is no workspace
Summary: JavaCore.getDefaultOptions() throws an exception when there is no workspace
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4 M4   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-05 15:14 EST by Marcelo Paternostro CLA
Modified: 2007-12-11 10:17 EST (History)
3 users (show)

See Also:


Attachments
Proposed patch (1.34 KB, patch)
2007-11-06 05:59 EST, Frederic Fusier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marcelo Paternostro CLA 2007-11-05 15:14:35 EST
Since AST supports stand-alone environments, it seems puzzling that JavaCore.getDefaultOptions() throws an exception when there is no workspace.

The actual culprit, as indicated by the stack trace below, is ResourcesPlugin.getWorkspace().  It is interesting to notice that the null check on JavaCore.getEncoding() doesn't do much (so a possible way of avoiding the exception is to change it to a try/catch).

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
	at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:326)
	at org.eclipse.jdt.core.JavaCore.getEncoding(JavaCore.java:2893)
	at org.eclipse.jdt.internal.core.JavaModelManager.getDefaultOptions(JavaModelManager.java:1685)
	at org.eclipse.jdt.core.JavaCore.getDefaultOptions(JavaCore.java:2880)
...
Comment 1 Ed Merks CLA 2007-11-05 15:28:09 EST
Given that ResourcesPlugin.getWorkspace is like this:

	public static IWorkspace getWorkspace() {
		if (workspace == null)
			throw new IllegalStateException(Messages.resources_workspaceClosed);
		return workspace;
	}


It seems impossible for the null guard in this code to ever have the intended consequence of returning the default encoding instead of the workspace charset.

	public static String getEncoding() {
		// Verify that workspace is not shutting down (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=60687)
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		if (workspace != null) {
			try {
				return workspace.getRoot().getDefaultCharset();
			} catch (CoreException e) {
				// fails silently and return plugin global encoding if core exception occurs
			}
		}
		return ResourcesPlugin.getEncoding();
	}
Comment 2 Frederic Fusier CLA 2007-11-06 05:59:32 EST
Created attachment 82185 [details]
Proposed patch
Comment 3 Frederic Fusier CLA 2007-11-06 06:11:58 EST
Released for 3.4M4 in HEAD stream.
Comment 4 Marcelo Paternostro CLA 2007-11-06 20:34:55 EST
Thanks!
Comment 5 Eric Jodet CLA 2007-12-11 10:17:57 EST
Verified for 3.4 M4 using build I20071210-1800