Bug 208436 - bad code practice in JavaProject.hasJavaNature
Summary: bad code practice in JavaProject.hasJavaNature
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4 M4   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-01 11:10 EDT by Rodrigo Peretti CLA
Modified: 2008-01-03 12:48 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rodrigo Peretti CLA 2007-11-01 11:10:56 EDT
Eclipse 3.2

I'm trying to debug a ResourceException somewhere in my code so I've added a exception breakpoint for ResourceException. The problem is that I get tons of hits in JavaProject.hasJavaNature because it is implemented the following way:

public static boolean hasJavaNature(IProject project) { 
	try {
		return project.hasNature(JavaCore.NATURE_ID);
	} catch (CoreException e) {
		if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(project.getName()))
			return true;
		// project does not exist or is not open
	}
	return false;
}

Instead of checking if the project.exists() or project.isOpen() it relies on an exception being thrown... Which makes it painful to everyone else trying to debug a ResourceException.
Comment 1 Rodrigo Peretti CLA 2007-11-01 11:26:56 EDT
The same happens in JavaProject.exists():

	public boolean exists() {
		try {
			return this.project.hasNature(JavaCore.NATURE_ID);
		} catch (CoreException e) {
			// project does not exist or is not open
		}
		return false;
	}	
Comment 2 Jerome Lanneluc CLA 2007-11-02 06:40:25 EDT
There is no guaranty that a CoreException will not be thrown even if we check that the project exists() and isOpen(). So we still need to catch the CoreException. 
Also catching an exception is much faster than testing exists() and isOpen().

If you don't want to see the exception thrown in this code, you can still configure the debugger to not stop on this code (see the Filtering section of the breakpoint properties).

Will not change this.
Comment 3 Rodrigo Peretti CLA 2007-11-02 09:00:26 EDT
Not sure if I agree with it being much faster than checking for open or exists. In this case I get lots of exceptions which I believe are not cheap to handle.
Comment 4 Frederic Fusier CLA 2007-12-11 07:29:48 EST
Verified for 3.4M4
Comment 5 Philipe Mulet CLA 2007-12-14 06:29:35 EST
Exception handling approach is only faster if the exception never gets thrown (i.e. avoiding an eager check 99.99% of the time). Rodrigo seems to imply that this is not what is happening...
Comment 6 Jerome Lanneluc CLA 2007-12-14 06:53:24 EST
Rodrigo, what is your use case? Do you have many projects closed in your workspace? If so how many?

Looking at the code of Project.isOpen(), it looks like very expensive (many messages are sent in this code). I still believe that catching the exception is faster. I will need to write a performance test to verify this.
Comment 7 Rodrigo Peretti CLA 2008-01-03 12:48:55 EST
Hey guys, I can't find the workspace anymore. It was happening when I was importing an existing workspace with many Java projects. If I find it I can reopen this bug.