I'm creating an eclipse plug-in. I have a string with java code, and I want to save this code to a project. I used the IFile to create my file. Is there another way that is better for saving java code?
Ido
I'm creating an eclipse plug-in. I have a string with java code, and I want to save this code to a project. I used the IFile to create my file. Is there another way that is better for saving java code?
Ido
I am thinking about this one. Using IFile and then allowing the JDT to discover it will work just fine, it definitely has the advantage of simplicity. That is what the org.eclipse.core.resources API is designed to allow. If you want to know more take a look at JavaCore which is in the org.eclipse.jdt.core bundle. What you might be looking for would be after creating your IFile to invoke JavaCore.create( file ) where file is your newly created IFile instance.
I followed the example from the book Contributing to Eclipse.
Download the sample code from here http://www.informit.com/store/product.aspx?isbn=0321205758
It is in a class called TestProject
.
Basically it creates a org.eclipse.jdt.core.IJavaProject
from the IProject
. And creates a org.eclipse.jdt.core.ICompilationUnit
from a org.eclipse.jdt.core.IPackageFragment
IPackageFragment pack = sourceFolder.createPackageFragment(packageName, false, null);
StringBuffer buf = new StringBuffer();
buf.append("package " + pack.getElementName() + ";\n");
buf.append("\n");
buf.append(source);
ICompilationUnit cu = pack.createCompilationUnit(cuName, buf.toString(), false, null);
By the way this is an excellent book and shows how to unit test part of you plugin that you don't think are testable.
You could write a nature and builder that saves the text as a file with the ".java" extension, and the JDT will pick it up as long as build automatically is turned on.