Bug 247205 - NameLookup#findPackageFragment throws IndexOOB; should return null
Summary: NameLookup#findPackageFragment throws IndexOOB; should return null
Status: RESOLVED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2008-09-12 19:54 EDT by sardion CLA
Modified: 2010-01-06 00:44 EST (History)
2 users (show)

See Also:


Attachments
Corresponding test (1.86 KB, patch)
2008-09-25 09:24 EDT, Jerome Lanneluc CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description sardion CLA 2008-09-12 19:54:00 EDT
If you have a project containing:

/foo/bar/baz/blah/x.jar
/foo/y/Y.java

and do:

javaProject.findPackageFragment(path)

where path is the IPath to Y.java

You get an IOOB from NameLookkup line 418.


It's clear from the code in NameLookup that one of two things should be changed:
either
1) line 414 should check not != 0 for matching segments but instead that rootPath is a *prefix* of path

or
2) line 418 should not call rootPath.toOSString() but should instead use just the matching segments

I think #2 is correct.

Thanks.
Comment 1 Olivier Thomann CLA 2008-09-15 09:09:25 EDT
*** Bug 202689 has been marked as a duplicate of this bug. ***
Comment 2 Jerome Lanneluc CLA 2008-09-18 12:30:58 EDT
Can you please provide more detailed steps and the Eclipse build id?
Comment 3 sardion CLA 2008-09-24 05:01:30 EDT
Ok. More detailed:


1. Create a java project.
2. Create a class in package foo.y called Y
 -- this will give you a file /foo/y/Y.java
3. Pick your favorite jar file and copy it to /foo/bar/baz/blah/JARNAME.jar
 -- the jar file should live in the same folder structure as Y.java
4. Add the jar file to the project build path.
5. Save this project for later.


Now, in your Eclipse plugin, write some code:

IFile file = THE_IFILE_FOR_Y_DOT_JAVA_IN_THAT_PROJECT;
IPath path = file.getLocation();
IJavaProject project = JavaCore.create(file.getProject());

project.findPackageFragment(path); // throws IOOB as described


I am 99% certain that the fix is to change line 418 of NameLookup.java to use only the matching segments and not the whole .toOSString().  The reason this has not been found sooner is that jars are not usually stored in that sort of location.  This happens with Eclipse 3.4.XXX for all XXX.
Comment 4 Jerome Lanneluc CLA 2008-09-25 09:23:35 EDT
(In reply to comment #3)
> 1. Create a java project.
> 2. Create a class in package foo.y called Y
>  -- this will give you a file /foo/y/Y.java
> 3. Pick your favorite jar file and copy it to /foo/bar/baz/blah/JARNAME.jar
>  -- the jar file should live in the same folder structure as Y.java
> 4. Add the jar file to the project build path.
> 5. Save this project for later.
> 
> 
> Now, in your Eclipse plugin, write some code:
> 
> IFile file = THE_IFILE_FOR_Y_DOT_JAVA_IN_THAT_PROJECT;
> IPath path = file.getLocation();
> IJavaProject project = JavaCore.create(file.getProject());
> 
> project.findPackageFragment(path); // throws IOOB as described
This still doesn't throw an IOOB for me but this returns null as expected (since the given path is the path to the Y.java file, it is not the path to the package fragment).
Comment 5 Jerome Lanneluc CLA 2008-09-25 09:24:05 EDT
Created attachment 113451 [details]
Corresponding test
Comment 6 Olivier Thomann CLA 2010-01-05 10:54:51 EST
Jay, please investigate. If it cannot be reproduced, please close as WORKSFORME.
Comment 7 Jay Arthanareeswaran CLA 2010-01-06 00:44:40 EST
Can't reproduce this on HEAD. Closing as WORKSFORME