[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.tools.jdt] Re: createCompilationUnit() -- new class is created in default package...?
|
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@xxxxxxxx> wrote in message
news:f8381d$dav$1@xxxxxxxxxxxxxxxxxxxx
> 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);