[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...?

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);