Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Retrieving IPackageFragmentRoot from a JAR
Retrieving IPackageFragmentRoot from a JAR [message #254045] Wed, 18 June 2008 12:49 Go to next message
Paul Glezen is currently offline Paul GlezenFriend
Messages: 60
Registered: July 2009
Member
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 #254049 is a reply to message #254045] Wed, 18 June 2008 13:05 Go to previous messageGo to next message
Jerome Lanneluc is currently offline Jerome LannelucFriend
Messages: 572
Registered: July 2009
Senior Member
Paul Glezen wrote:
> 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
>
Is the jar on the classpath of file.getProject() or is it on the classpath of another project?
If it is on the classpath of another project, you should use JavaCore.create(IFile,IJavaProject).

Jerome
Re: Retrieving IPackageFragmentRoot from a JAR [message #254056 is a reply to message #254049] Wed, 18 June 2008 14:46 Go to previous messageGo to next message
Paul Glezen is currently offline Paul GlezenFriend
Messages: 60
Registered: July 2009
Member
Thanks for the tip, Jerome. Unfortunately, that method was added in
Eclipse 3.3. I'm on a project using RSA 7.0 which is based on Eclipse
3.2.

To answer your question, the JAR is located in a simple project. It is
in the classpath of the Java file that references it.

Is there a way achieve the effect of the API you referenced before
Eclipse 3.3?

Thanks,
- Paul

Jerome Lanneluc wrote:
> Paul Glezen wrote:
>>
>> 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
>>
> Is the jar on the classpath of file.getProject() or is it on the
> classpath of another project?
> If it is on the classpath of another project, you should use
> JavaCore.create(IFile,IJavaProject).
>
> Jerome
Re: Retrieving IPackageFragmentRoot from a JAR [message #254075 is a reply to message #254056] Wed, 18 June 2008 16:39 Go to previous messageGo to next message
Jerome Lanneluc is currently offline Jerome LannelucFriend
Messages: 572
Registered: July 2009
Senior Member
Paul Glezen wrote:
> Thanks for the tip, Jerome. Unfortunately, that method was added in
> Eclipse 3.3. I'm on a project using RSA 7.0 which is based on Eclipse 3.2.
>
> To answer your question, the JAR is located in a simple project. It is
> in the classpath of the Java file that references it.
>
> Is there a way achieve the effect of the API you referenced before
> Eclipse 3.3?
>
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;
Re: Retrieving IPackageFragmentRoot from a JAR [message #254078 is a reply to message #254075] Wed, 18 June 2008 17:33 Go to previous message
Paul Glezen is currently offline Paul GlezenFriend
Messages: 60
Registered: July 2009
Member
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;
Previous Topic:Bread Crumbs implementation location
Next Topic:Constructor: Assign parameter to a new field
Goto Forum:
  


Current Time: Sun Sep 01 09:44:33 GMT 2024

Powered by FUDForum. Page generated in 0.03082 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top