createCompilationUnit() -- new class is created in default package...? [message #246044] |
Mon, 23 July 2007 16:17  |
Eclipse User |
|
|
|
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 #246054 is a reply to message #246049] |
Mon, 23 July 2007 20:53   |
Eclipse User |
|
|
|
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 #246143 is a reply to message #246090] |
Wed, 25 July 2007 09:42  |
Eclipse User |
|
|
|
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.)
>
|
|
|
Powered by
FUDForum. Page generated in 0.05067 seconds