Simple JDT example of code generation
I'm trying to write an Eclipse plugin. It will have dependencies on the RCP for basic stuff and JDT for Java code generation. It consists of a Wizard / WizardPage which asks for a package and a few class names. When the user hits "Finish", I want the wizard to create clas开发者_开发问答ses corresponding to the entered values into the current Java project.
I have the basic UI working. What I cannot figure out is how to generate the corresponding class code. Eclipse documentation is a bit hard to follow. From what I can glean I need to first obtain an IJavaProject from the workspace, from that an IPackageFragment and from that I can call createCompilationUnit() to make a class.
What I can't do is find any working examples. I assume that somehow I can walk from the workspace root to where I need to be and create the unit.
Can anyone offer some pointers to simple working code?
You need to extend NewElementWizardPage and provide the functionality. See this documentation for more details
I assume that somehow I can walk from the workspace root to where I need to be and create the unit.
Yes. Take a look at IProject, IFolder, and IFile in the org.eclipse.core.resources package.
Can anyone offer some pointers to simple working code?
Eclipse itself creates a new Java class with a limited skeleton when you ask it to, using the New Java Class Wizard. That source code would be helpful to you for what you want to do. I couldn't find it with a cursory search of Eclipse and the Eclipse help. I'm guessing it's somewhere in an org.eclipse.pde package.
You might also look at Creation Wizards, which is the extension point org.eclipse.ui.newWizards.
精彩评论