Retrieving IPackageFragmentRoot from a JAR [message #254045] |
Wed, 18 June 2008 08:49  |
Eclipse User |
|
|
|
Hi Folks,
I'm having trouble constructing a valid IType reference to a class file
located in a JAR. I'm using the SearchEngine of Eclipse 3.2 (sorry I'm
stuck with this version) which hands me results via my implementation of
TypeNameRequestor. In some cases, the path attribute is of the form
"<path to JAR>|<classname>.jar"
I extract the <path to JAR> part and resolve it to an IFile reference I
called "file" in the snippet below. This JAR file is located in a
Simple Eclipse project without a Java nature. I "opened" the project
out of desperation in line 2, but that didn't seem to have an effect.
In the debugger, line 3 returns null. The Javadoc says this happens
with the archive is not of an appropriate format. But the JDT
SearchEngine had no trouble finding the class file inside it. The
"packageName" and "typeName" variables below were populated by the
SearchEngine.
Ultimately, I'm simply trying to find a list of methods of a type
returned to me via the SearchEngine. I'd appreciate any pointers.
1 if (file.exists()) {
2 file.getProject().open(null);
3 IPackageFragmentRoot jarRoot =
JavaCore.createJarPackageFragmentRootFrom(file);
4 IPackageFragment packageFragment =
jarRoot.getPackageFragment(packageName);
5 IClassFile classFile =
packageFragment.getClassFile(typeName + ".class");
6 if (classFile.getType().isStructureKnown()) {
7 result = classFile.getType();
8 } else {
9 System.out.println("JDT could not determine the structure of "
+ classFile.getElementName());
10 }
- Paul Glezen
|
|
|
|
|
|
Re: Retrieving IPackageFragmentRoot from a JAR [message #254078 is a reply to message #254075] |
Wed, 18 June 2008 13:33  |
Eclipse User |
|
|
|
This works! After meditating on your first reply, I figured that I need
to somehow supply a Java project that does have the JAR in its
classpath. Fortunately, I will know this project in advance. So I was
able to use the IJavaProject in a manner similar to your suggestion below.
Thanks a lot. This was really driving me nuts.
-Paul
Jerome Lanneluc wrote:
>>
> If you know the referring project, the following should do the trick:
> IProject referringProject = ...;
> IJavaProject javaProject = JavaCore.create(referringProject);
> return javaProject.getPackageFragmentRoot(file);
>
> If you don't know the referring project, then you would need to walk
> all Java projects in the workspace:
>
> IJavaProject[] projects =
> JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).ge tJavaProjects();
>
> for (int i = 0; i < projects.length; i++) {
> IPackageFragmentRoot root = projects[i].getPackageFragmentRoot(file);
> if (root.exists())
> return root;
> }
> return null;
|
|
|
Powered by
FUDForum. Page generated in 0.03024 seconds