Bug 216817 - JavaCore.getEncoding() fails when there is no workspace.
Summary: JavaCore.getEncoding() fails 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 M5   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-28 15:44 EST by Marcelo Paternostro CLA
Modified: 2008-02-05 10:45 EST (History)
1 user (show)

See Also:


Attachments
Suggestion for the fix (915 bytes, patch)
2008-01-28 15:44 EST, Marcelo Paternostro CLA
no flags Details | Diff
Proposed patch (1.09 KB, patch)
2008-01-29 09:18 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 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...