Bug 216817

Summary: JavaCore.getEncoding() fails when there is no workspace.
Product: [Eclipse Project] JDT Reporter: Marcelo Paternostro <marcelop>
Component: CoreAssignee: Frederic Fusier <frederic_fusier>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Olivier_Thomann
Version: 3.4   
Target Milestone: 3.4 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Suggestion for the fix
none
Proposed patch none

Description Marcelo Paternostro CLA 2008-01-28 15:44:52 EST
Created attachment 88053 [details]
Suggestion for the fix

Pls see bugzilla https://bugs.eclipse.org/bugs/show_bug.cgi?id=208807 as this new problem is related to that one.

The changes made to fix that bugzilla avoid this line in the JavaCore.getEncoding() method to throw an exception:

  ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();

The problem now is that the very last line, 

  return ResourcesPlugin.getEncoding();

fails with a NPE, because ResourcePlugin.getPlugin() returns null when there is no workspace.

I am attaching a patch with a fix that solves the problems on the scenario I am working now.  As you can see, I am returning the system's file encoding, which is what is done by ResourcesPlugin.getEncoding().
Comment 1 Frederic Fusier CLA 2008-01-29 09:18:55 EST
Created attachment 88126 [details]
Proposed patch

The proposed fix takes advantage of the fact that ResourcePlugin.getWorkspace() throws an IllegalStateException when there's no workspace. Then, bug 60687 should also be definitely fixed as well...

No test case added as JDT/Core tests always have a workspace.

To validate the fix, I run following simple class as a JUnit Test (no plug-in):

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.tests.junit.extension.TestCase;

public class TestEncoding extends TestCase {

public TestEncoding(String name) {
	super(name);
}

public void test01() {
	assertEquals("Cp1252", JavaCore.getEncoding());
}
}
Comment 2 Frederic Fusier CLA 2008-01-29 09:21:16 EST
Released for 3.4M5 in HEAD stream.
Comment 3 Maxime Daniel CLA 2008-02-05 07:48:52 EST
Verified for 34M5 using I20080204-0010 build.

However, I would suggest that a test be added to our test projects.
Comment 4 Frederic Fusier CLA 2008-02-05 10:45:42 EST
(In reply to comment #3)
> Verified for 34M5 using I20080204-0010 build.
> 
> However, I would suggest that a test be added to our test projects.
> 
Verifying that is definitely not possible as all Eclipse JUnit tests are run as 'JUnit Plug-in tests' and so ResourcePlugin.getWorkspace() will never return a null value or throw a CoreException (even for our compiler tests...).

As this condition is necessary to reproduce the bug, we cannot add any regression test to verify this new correct behavior...