Bug 300136 - classpathentry OPTIONAL attribute not honored for var entries
Summary: classpathentry OPTIONAL attribute not honored for var entries
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M6   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-20 00:37 EST by Jared Burns CLA
Modified: 2010-03-08 10:25 EST (History)
4 users (show)

See Also:
srikanth_sankaran: review+


Attachments
Proposed Patch (5.77 KB, patch)
2010-01-26 23:07 EST, Jay Arthanareeswaran CLA
jarthana: review?
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jared Burns CLA 2010-01-20 00:37:39 EST
The OPTIONAL attribute for classpath entries doesn't appear to work for variable classpath entries.

For example, we'd like to use the following entry in our classpath file:
	<classpathentry kind="var" path="ECLIPSE_INSTALL/plugins/org.apache.ant_1.7.0.v200803061910/lib/ant.jar">
		<attributes>
			<attribute name="optional" value="true"/>
		</attributes>
	</classpathentry>

Basically, we want our projects to use this classpath entry if the ECLIPSE_INSTALL variable is defined. But if it's not, then we still want the project to compile.

We'd like the OPTIONAL attribute on variable entries to work the same way it does for source entries. For example:
	<classpathentry kind="src" path="bar">
		<attributes>
			<attribute name="optional" value="true"/>
		</attributes>
	</classpathentry>

If the folder "bar" doesn't exist, the project will still compile.
Comment 1 Srikanth Sankaran CLA 2010-01-20 00:51:58 EST
Jay, can you please take a look.
Comment 2 Jared Burns CLA 2010-01-20 01:03:40 EST
I debugged into this a bit and it looks like it's happening because the optional attribute is only checked if the classpath entry's error code is the generic IJavaModelStatusConstants.INVALID_CLASSPATH. If it's a more specific code like IJavaModeLStatusConstants.CP_VARIABLE_PATH_UNBOUND, the "optional" check isn't done.

From ClasspathValidation#validate():

  for (int i = 0; i < rawClasspath.length; i++) {
    status = ClasspathEntry.validateClasspathEntry(this.project, rawClasspath[i], false/*src attach*/, true /*recurse in container*/);
    if (!status.isOK()) {
      if (status.getCode() == IJavaModelStatusConstants.INVALID_CLASSPATH && ((ClasspathEntry) rawClasspath[i]).isOptional())
        continue; // ignore this entry
      this.project.createClasspathProblemMarker(status);	
      }
    }
    status = ClasspathEntry.validateClasspath(this.project, rawClasspath, outputLocation);
    if (!status.isOK()) 
      this.project.createClasspathProblemMarker(status);
  }

I believe the fourth line should be checking for additional status codes. Probably INVALID_CLASSPATH, CP_CONTAINER_PATH_UNBOUND, or CP_VARIABLE_PATH_UNBOUND.
Comment 3 Jay Arthanareeswaran CLA 2010-01-20 06:38:33 EST
(In reply to comment #2)
> I debugged into this a bit and it looks like it's happening because the
> optional attribute is only checked if the classpath entry's error code is the
> generic IJavaModelStatusConstants.INVALID_CLASSPATH. If it's a more specific
> code like IJavaModeLStatusConstants.CP_VARIABLE_PATH_UNBOUND, the "optional"
> check isn't done.
> 

Yes, that seems to be the code responsible for this behavior. Only that that particular code has been moved to ClasspathEntry#validateLibraryEntry in HEAD. I will look further into it and see if we can accommodate such a change for libraries.
Comment 4 Jay Arthanareeswaran CLA 2010-01-26 23:07:47 EST
Created attachment 157369 [details]
Proposed Patch

Added 3 other error conditions to be exempted for optional classpath entries. Test cases added (ClasspathTests#testBug300136 and testBug300136a)
Comment 5 Srikanth Sankaran CLA 2010-01-29 00:23:27 EST
(In reply to comment #4)
> Created an attachment (id=157369) [details]
> Proposed Patch
> 
> Added 3 other error conditions to be exempted for optional classpath entries.
> Test cases added (ClasspathTests#testBug300136 and testBug300136a)

(1) Jay, can you see if similar code is needed in org.eclipse.jdt.internal.core.ClasspathEntry.validateClasspathEntry(IJavaProject, IClasspathEntry, IClasspathContainer, boolean, boolean)

(search for 171136)

(2) Do you think it is reasonable to simply say

if (((ClasspathEntry) entry).isOptional())
	return JavaModelStatus.VERIFIED_OK;
Comment 6 Jay Arthanareeswaran CLA 2010-01-29 03:52:00 EST
(In reply to comment #5)
> 
> (1) Jay, can you see if similar code is needed in
> org.eclipse.jdt.internal.core.ClasspathEntry.validateClasspathEntry(IJavaProject,
> IClasspathEntry, IClasspathContainer, boolean, boolean)
> 
> (search for 171136)

The other place uses ClasspathEntry#validateLibraryEntry, which is specific for CPE_LIBRARY. So, I am not sure if we need to worry about the other error conditions there.


> (2) Do you think it is reasonable to simply say
 
I realized that we had to handle multiple error conditions. But I see there is at least one error&#12288;condition that I wasn't sure about ignoring - NAME_COLLISION. We can probably change the check to exclude that Status. I agree that would be programmatically more efficient. But from maintenance aspect, the current approach looks more appropriate.
Comment 7 Srikanth Sankaran CLA 2010-01-29 04:26:26 EST
Ok, thanks for the explanation.
Comment 8 Jay Arthanareeswaran CLA 2010-02-01 06:18:36 EST
Released in HEAD for 3.6M6
Comment 9 Satyam Kandula CLA 2010-03-08 10:19:27 EST
Verified for 3.6M6 using build I20100305-101
Comment 10 Jay Arthanareeswaran CLA 2010-03-08 10:25:16 EST
Verified.