Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » createCompilationUnit() -- new class is created in default package...?
createCompilationUnit() -- new class is created in default package...? [message #246044] Mon, 23 July 2007 16:17 Go to next message
Eclipse UserFriend
I am writing a code generator that targets projects which include a
directory:

/project/structures

this directory is designated as a source entry (using
JavaCore.newSourceEntry)... that all seems fine, but:

1. I get a target package fragment using
IJavaProject.findPackageFragment(new Path("/project/structures"));

2. then I create an ICompilationUnit myNewClass that includes the
declaration "packages structures;" within that package fragment,

3. and Eclipse puts the resulting source code in the correct file system
location /project/structures/myNewClass.java,

4. but... the compiler thinks myNewClass is in the default package.

How do I programatically designate the new class' package (at the level of
the JavaModel, not the file system?)

(thanks)
Re: createCompilationUnit() -- new class is created in default package...? [message #246049 is a reply to message #246044] Mon, 23 July 2007 17:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

Michael Megliola wrote:
> I am writing a code generator that targets projects which include a
> directory:
>
> /project/structures
>
> this directory is designated as a source entry (using
> JavaCore.newSourceEntry)... that all seems fine, but:
>
> 1. I get a target package fragment using
> IJavaProject.findPackageFragment(new Path("/project/structures"));
>
> 2. then I create an ICompilationUnit myNewClass that includes the
> declaration "packages structures;" within that package fragment,
>
> 3. and Eclipse puts the resulting source code in the correct file system
> location /project/structures/myNewClass.java,
>
> 4. but... the compiler thinks myNewClass is in the default package.
>
> How do I programatically designate the new class' package (at the level of
> the JavaModel, not the file system?)
>
> (thanks)
>
>
Are you using the AST to create the compilation unit? If so, you can
do something like the following:

PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
packageDeclaration.setName(ast.newName(pkgname));
compilationUnit.setPackage(packageDeclaration);
Re: createCompilationUnit() -- new class is created in default package...? [message #246054 is a reply to message #246049] Mon, 23 July 2007 20:53 Go to previous messageGo to next message
Eclipse UserFriend
no... I am copying source from an existing library, so I read source code
from an input stream (a jar file), then I use JavaCore to get an
IJavaProject, then from that an IPackageFragment (which represents the
target directory in the user's project), and from there an ICompilationUnit.

"Mike Yawn" <myawn@ebay.com> wrote in message
news:f8381d$dav$1@build.eclipse.org...
> Michael Megliola wrote:
>> I am writing a code generator that targets projects which include a
>> directory:
>>
>> /project/structures
>>
>> this directory is designated as a source entry (using
>> JavaCore.newSourceEntry)... that all seems fine, but:
>>
>> 1. I get a target package fragment using
>> IJavaProject.findPackageFragment(new Path("/project/structures"));
>>
>> 2. then I create an ICompilationUnit myNewClass that includes the
>> declaration "packages structures;" within that package fragment,
>>
>> 3. and Eclipse puts the resulting source code in the correct file system
>> location /project/structures/myNewClass.java,
>>
>> 4. but... the compiler thinks myNewClass is in the default package.
>>
>> How do I programatically designate the new class' package (at the level
>> of the JavaModel, not the file system?)
>>
>> (thanks)
>>
>>
> Are you using the AST to create the compilation unit? If so, you can do
> something like the following:
>
> PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
> packageDeclaration.setName(ast.newName(pkgname));
> compilationUnit.setPackage(packageDeclaration);
Re: createCompilationUnit() -- new class is created in default package...? [message #246059 is a reply to message #246044] Tue, 24 July 2007 08:54 Go to previous messageGo to next message
Eclipse UserFriend
Michael Megliola a écrit :
> this directory is designated as a source entry (using
> JavaCore.newSourceEntry)... that all seems fine, but:
>
> 1. I get a target package fragment using
> IJavaProject.findPackageFragment(new Path("/project/structures"));
>
> 2. then I create an ICompilationUnit myNewClass that includes the
> declaration "packages structures;" within that package fragment,
How do you create it?
Are you using this method
org.eclipse.jdt.core.IPackageFragment.createCompilationUnit( String,
String, boolean, IProgressMonitor) ?
If not, I believe this is the one you should use.
Let me know if it works.
--
Olivier
Re: createCompilationUnit() -- new class is created in default package...? [message #246090 is a reply to message #246044] Tue, 24 July 2007 16:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Michael Megliola" <michael.megliola@gmail.com> wrote in message
news:f832cb$ulj$1@build.eclipse.org...
>I am writing a code generator that targets projects which include a
>directory:
>
> /project/structures
>
> this directory is designated as a source entry (using
> JavaCore.newSourceEntry)... that all seems fine, but:
>
> 1. I get a target package fragment using
> IJavaProject.findPackageFragment(new Path("/project/structures"));

It sounds to me like your directory structure does not align with your
package structure.

If project/structures is a source directory, and you want something to go in
a package inside that, it would be in a subdirectory: for instance, package
pa would be /project/structures/pa, and the code would start with "package
pa;".

To put something in a package named "structures", you'd want /project to be
the source entry. (Which is generally a bad idea since it means you've got
source code at the root of your project.)
Re: createCompilationUnit() -- new class is created in default package...? [message #246143 is a reply to message #246090] Wed, 25 July 2007 09:42 Go to previous message
Eclipse UserFriend
I think that's what I did wrong --

will switch to: /project/src/structures and use "package structures;"

(otherwise I think I have something like /project/structures/structures,
which is awkward)



"Walter Harley" <wharley@bea.com> wrote in message
news:f85m22$7oe$1@build.eclipse.org...
> "Michael Megliola" <michael.megliola@gmail.com> wrote in message
> news:f832cb$ulj$1@build.eclipse.org...
>>I am writing a code generator that targets projects which include a
>>directory:
>>
>> /project/structures
>>
>> this directory is designated as a source entry (using
>> JavaCore.newSourceEntry)... that all seems fine, but:
>>
>> 1. I get a target package fragment using
>> IJavaProject.findPackageFragment(new Path("/project/structures"));
>
> It sounds to me like your directory structure does not align with your
> package structure.
>
> If project/structures is a source directory, and you want something to go
> in a package inside that, it would be in a subdirectory: for instance,
> package pa would be /project/structures/pa, and the code would start with
> "package pa;".
>
> To put something in a package named "structures", you'd want /project to
> be the source entry. (Which is generally a bad idea since it means you've
> got source code at the root of your project.)
>
Previous Topic:Split editor window in JDT plugin
Next Topic:Re: Request about Importing Java Compiler
Goto Forum:
  


Current Time: Thu Jun 19 18:45:25 EDT 2025

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

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

Back to the top