Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to insert new file into project
How to insert new file into project [message #242673] Tue, 10 April 2007 21:07 Go to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

I'm not sure whether this is a JDT question, or a core question, but since
it's a Java file I'll start here.

I'm writing a wizard that will generate a Java source file. So I've got a
string representing the package name, and a string representing the class
name. Now I just want to create the file and insert it into the project.
This is supposed to be the easy part :-).

My head is about to explode from trying to navigate the object hierarchy
that may or may not be involved in this -- IJavaElement, IFile, IResource,
IContainer, IWorkspaceRoot, IJavaProject, IPackageFragement. I've probably
tried two dozen different permutations of things that look like they ought
to work, but in most cases I just get an exception thrown.

What do I actually need to call to do this?

- Take the package name, find it in my workspace. If it doesn't exist,
create it.
- Take the class name, create a.java source file for it in the package (and
if I can point to a template to be used to create the file content, even
better)

Thanks .
Mike
Re: How to insert new file into project [message #242702 is a reply to message #242673] Wed, 11 April 2007 09:03 Go to previous messageGo to next message
Eddie Man is currently offline Eddie ManFriend
Messages: 102
Registered: July 2009
Senior Member
Here is the simple code for creating a file in project.

IProject project;
....
// assume you want to create a file (src/mypackage/MyClass.java)
IFolder srcFolder = project.getFolder("src");
if (!srcFolder.exists())
srcFolder.create(true, true, null);
IFolder packageFolder = srcFolder.getFolder("mypackage");
if (!packageFolder.exists())
packageFolder.create(true, true, null);
IFile classFile = packageFolder.getFile("MyClass.java");
ByteArrayInputStream content = new ByteArrayInputStream("class content
here".getBytes());
classFile.create(content, true, null);

Mike Yawn wrote:
> I'm not sure whether this is a JDT question, or a core question, but since
> it's a Java file I'll start here.
>
> I'm writing a wizard that will generate a Java source file. So I've got a
> string representing the package name, and a string representing the class
> name. Now I just want to create the file and insert it into the project.
> This is supposed to be the easy part :-).
>
> My head is about to explode from trying to navigate the object hierarchy
> that may or may not be involved in this -- IJavaElement, IFile, IResource,
> IContainer, IWorkspaceRoot, IJavaProject, IPackageFragement. I've probably
> tried two dozen different permutations of things that look like they ought
> to work, but in most cases I just get an exception thrown.
>
> What do I actually need to call to do this?
>
> - Take the package name, find it in my workspace. If it doesn't exist,
> create it.
> - Take the class name, create a.java source file for it in the package (and
> if I can point to a template to be used to create the file content, even
> better)
>
> Thanks .
> Mike
>
>
Re: How to insert new file into project [message #242707 is a reply to message #242673] Wed, 11 April 2007 09:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: schmidts.iese.fraunhofer.de

Hi Mike, try this

final String FILE_NAME = "YourFileNameHere.java";
final String PACKAGE_NAME = "your.package.name.here";

final String PROJECT_NAME = "ProjectNameIfYouGotIt";


IPackageFragment actualPackage = null;
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

if(!PROJECT_NAME.equals("")) {
IProject project = root.getProject(PROJECT_NAME);
if(current.getDescription().hasNature(JavaCore.NATURE_ID)) {
IJavaProject javaProject = JavaCore.create(project);
IPackageFragments fragments = javaProject.getPackageFragments();
for(IPackageFragment frag : fragments) {
if(frag.getElementName().equals(PACKAGE_NAME) {
actualPackage = frag;
break;
}
}
}
} else {
IProject[] projects = root.getProjects();
for(IProject current : projects) {
if(current.getDescription().hasNature(JavaCore.NATURE_ID)) {
IJavaProject javaProject = JavaCore.create(current);
IPackageFragments fragments = javaProject.getPackageFragments();
for(IPackageFragment frag : fragments) {
if(frag.getElementName().equals(PACKAGE_NAME) {
actualPackage = frag;
break;
}
}
}
if(actualPackage != null)
break;
}
}

IFolder folder = (IFolder)actualPackage.getResource();
IFile file = folder.getFile(FILE_NAME);
if(file.exists) {
handleExistingFile();
}
final boolean FORCE = true;
file.create(getContent(), FORCE, new NullProgressMonitor);
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);





where getContent() looks like this:
private InputStream getContent() {
String content = getInitialContent();

content = "";

return new ByteArrayInputStream(content.getBytes());

}



Maybe there is an easier way to find the right IFolder (perhaps by using
project.getFolder(PACKAGE_NAME), but I'm not sure if this works with full
qualified package names).

However, I hope this gives you the right direction.

Good luck, Seb
Create package fragment? (was: How to insert new file into project) [message #242733 is a reply to message #242707] Wed, 11 April 2007 16:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

This almost gets me there :-)

The only problem is if I don't find a package fragment that matches the
package name for the source file I need to create. What's the proper way to
create a new package fragment?

Thanks,
Mike


"Sebastian Schmidt" <schmidts@iese.fraunhofer.de> wrote in message
news:evi9o6$6er$1@build.eclipse.org...
> Hi Mike, try this
>
> final String FILE_NAME = "YourFileNameHere.java";
> final String PACKAGE_NAME = "your.package.name.here";
>
> final String PROJECT_NAME = "ProjectNameIfYouGotIt";
>
>
> IPackageFragment actualPackage = null;
> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
>
> if(!PROJECT_NAME.equals("")) {
> IProject project = root.getProject(PROJECT_NAME);
> if(current.getDescription().hasNature(JavaCore.NATURE_ID)) {
> IJavaProject javaProject = JavaCore.create(project);
> IPackageFragments fragments = javaProject.getPackageFragments();
> for(IPackageFragment frag : fragments) {
> if(frag.getElementName().equals(PACKAGE_NAME) {
> actualPackage = frag;
> break;
> }
> }
> }
> } else {
> IProject[] projects = root.getProjects();
> for(IProject current : projects) {
> if(current.getDescription().hasNature(JavaCore.NATURE_ID)) {
> IJavaProject javaProject = JavaCore.create(current);
> IPackageFragments fragments =
> javaProject.getPackageFragments();
> for(IPackageFragment frag : fragments) {
> if(frag.getElementName().equals(PACKAGE_NAME) {
> actualPackage = frag;
> break;
> }
> }
> }
> if(actualPackage != null)
> break;
> }
> }
>
> IFolder folder = (IFolder)actualPackage.getResource();
> IFile file = folder.getFile(FILE_NAME);
> if(file.exists) {
> handleExistingFile();
> }
> final boolean FORCE = true;
> file.create(getContent(), FORCE, new NullProgressMonitor);
> ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
>
>
>
>
>
> where getContent() looks like this:
> private InputStream getContent() {
> String content = getInitialContent();
>
> content = "";
>
> return new ByteArrayInputStream(content.getBytes());
>
> }
>
>
>
> Maybe there is an easier way to find the right IFolder (perhaps by using
> project.getFolder(PACKAGE_NAME), but I'm not sure if this works with full
> qualified package names).
>
> However, I hope this gives you the right direction.
>
> Good luck, Seb
>
>
Re: Create package fragment? (was: How to insert new file into project) [message #242766 is a reply to message #242733] Thu, 12 April 2007 08:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: schmidts.iese.fraunhofer.de

Hi Mike

you have to get or create the IFolder with the IProject, that is the source
root of this project.

IFolder folder = project.getFolder("src");
if(!folder.exists)
folder.create(..);

and when you have the resource, i.e. the IFolder object, you just need the
IJavaProject to call

IPackageFragmentRoot fragRoot = javaProject.getPackageFragmentRoot(folder);
IPackageFragment fragment = fragRoot.createPackageFragment(PACKAGE_NAME,
FORCE, new NullProgressMonitor);

et voil
Re: Create package fragment? (was: How to insert new file into project) [message #242776 is a reply to message #242766] Thu, 12 April 2007 15:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

Thanks for your help on this ... still trying to work out a last detail or
two.

I don't want to be dependent on their being a folder called 'src' -- in my
example project, for example, there isn't one, and packages are rooted
directly under the project, rather than under a src folder.

I'm not sure if there is a reliable way to get the 'root' folder -- either
the project folder, or a 'src' or 'source' or whatever the user may have
called it.

I have an IResource (IFile) of another Java file that is in the project, but
which isn't in the same package as the element I'm creating. I thought I
might be able to walk up the tree (IResource.getParent()) until I found an
element where instanceof IFolder was true, and then create a package
fragment there. But it turns out that instanceof IFolder is true for all of
the package pieces, also. So I had code in com.mycompany.example, and
wanted to create a new package com.mycompany.codegen, but what I ended up
with instead was com.mycompany.example.com.mycompany.codegen.

So is there a way to walk up the tree (or some other process) that gets me
to the 'parent of com' (without explicitly testing for that, since I don't
want to break if the code is in a different top-level domain, or even
(horrors) the default package.

Mike


"Sebastian Schmidt" <schmidts@iese.fraunhofer.de> wrote in message
news:evks1s$vtu$1@build.eclipse.org...
> Hi Mike
>
> you have to get or create the IFolder with the IProject, that is the
> source root of this project.
>
> IFolder folder = project.getFolder("src");
> if(!folder.exists)
> folder.create(..);
>
> and when you have the resource, i.e. the IFolder object, you just need the
> IJavaProject to call
>
> IPackageFragmentRoot fragRoot =
> javaProject.getPackageFragmentRoot(folder);
> IPackageFragment fragment = fragRoot.createPackageFragment(PACKAGE_NAME,
> FORCE, new NullProgressMonitor);
>
> et voil
Re: Create package fragment? (was: How to insert new file into project) [message #242806 is a reply to message #242776] Thu, 12 April 2007 22:55 Go to previous message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

I think this is resolved now -- I'm using
project.getAllPackageFragmentRoots, and then creating a package fragment
under the first root for which isArchive() and isExternal() both return
false.

If the user has more than one qualifying root, I'm not sure there would be a
reliable way to pick the 'right' one.

Mike


"Mike Yawn" <myawn@ebay.com> wrote in message
news:evli8n$id6$1@build.eclipse.org...
> Thanks for your help on this ... still trying to work out a last detail or
> two.
>
> I don't want to be dependent on their being a folder called 'src' -- in my
> example project, for example, there isn't one, and packages are rooted
> directly under the project, rather than under a src folder.
>
> I'm not sure if there is a reliable way to get the 'root' folder -- either
> the project folder, or a 'src' or 'source' or whatever the user may have
> called it.
>
> I have an IResource (IFile) of another Java file that is in the project,
> but which isn't in the same package as the element I'm creating. I
> thought I might be able to walk up the tree (IResource.getParent()) until
> I found an element where instanceof IFolder was true, and then create a
> package fragment there. But it turns out that instanceof IFolder is true
> for all of the package pieces, also. So I had code in
> com.mycompany.example, and wanted to create a new package
> com.mycompany.codegen, but what I ended up with instead was
> com.mycompany.example.com.mycompany.codegen.
>
> So is there a way to walk up the tree (or some other process) that gets me
> to the 'parent of com' (without explicitly testing for that, since I
> don't want to break if the code is in a different top-level domain, or
> even (horrors) the default package.
>
> Mike
>
>
> "Sebastian Schmidt" <schmidts@iese.fraunhofer.de> wrote in message
> news:evks1s$vtu$1@build.eclipse.org...
>> Hi Mike
>>
>> you have to get or create the IFolder with the IProject, that is the
>> source root of this project.
>>
>> IFolder folder = project.getFolder("src");
>> if(!folder.exists)
>> folder.create(..);
>>
>> and when you have the resource, i.e. the IFolder object, you just need
>> the IJavaProject to call
>>
>> IPackageFragmentRoot fragRoot =
>> javaProject.getPackageFragmentRoot(folder);
>> IPackageFragment fragment = fragRoot.createPackageFragment(PACKAGE_NAME,
>> FORCE, new NullProgressMonitor);
>>
>> et voil
Previous Topic:classpathContainerPage edit
Next Topic:Batch Compiler Fail On Warnings
Goto Forum:
  


Current Time: Fri Dec 27 04:19:35 GMT 2024

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

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

Back to the top