Simple JDT example of code generation - java

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 classes 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.

Related

Plugins to add features to java

I'm working on a project that requires to add some features to an existing java application using Netbeans. After searching the net, i founded that I should use "plugins". I didn't hear of plugins before.
I have a java application that I should download it from the net, then lets say I have to add a button, when clicked, calls a function that is written in some class in the application. In other words I want to make a new class that is able to access the classes and functions that are written in this application.
What I understood from searching the net is that:
-In the downloaded project's folder there is a folder named plugins.
-This folder contains zip files that contains classes and other stuff.
-I should make a plugin and add it to this folder in order to add a specific feature to the application.
That's what I know, if there is something wrong in what I said, I'll be thankful to correct to me.
Now, my question is that I want a link or website that can teach me how to create and add a plugin to an existing java application. Thank you :)
That entirely depends on the java application you're talking about. Not every application has a Plugins functionality. And not every Java Application uses the same Plugin API. The best bet is to go to the site of the App you're talking about and see if they offer a documentation about their Plugin API.

Java: Simple Validation API Validation Strategy Change

This is in reference to the question posted at org.netbeans.validation.api.ui.ValidationGroup.add() - How to specify ValidationStrategy?
I am trying to do something similar. Please guide me on what needs to be changed and where. I went through the given links but couldn't find the relevant info.
P.S.: Asking in a separate question because I can't comment on the original one due to lack of reputation points.
Regards
Uday
I found out what was required.
Check out the code from their repository (https://svn.kenai.com/svn/simplevalidation~src) and open ValidationAPI as a project in Netbeans (or whatever IDE you're using).
To change the default validation strategy you'll need to make the changes in ValidationGroup and SwingValidationGroup classes under org.netbeans.validation.api.ui and org.netbeans.validation.api.ui.swing respectively.
In these classes find ValidationStrategy.DEFAULT and replace it with the strategy you want (INPUT_VERIFIER, ON_FOCUS_LOSS, ON_CHANGE_OR_ACTION).
Build the project to generate a new jar and incorporate that jar in your own project.
That's it.

C++ or Java Library for automated code editing?

I am writing/planning to write a program that takes in a java file (or multiple java files), and edits and adds functions/classes/variables and then outputs a new java file (or multiple files).
Is there a C++ or Java library that
Can recognize and output names of classes/functions within a text file
Can recognize and output the names of the input arguments for said classes/functions
Can allow me to insert code at specific lines or within specific functions
Can search for a given variable name/value
Maintains original file formatting
I would prefer not having to manually code something to do the above, so any help would be appreciated.
Thank you in advance!
EDIT: I currently use Eclipse, and am unsure of how to proceed. So to further explain my question:
In eclipse, if I write a program that opens another .java file, How would I go about 'asking' eclipse to output, say, all the class names of the .java file I just opened?
Also I will explain the 'purpose' of this project to further clarify. I want to write a program that can take in any java file, and turn it into a class that can implemented remotely via RMI. To do this I will need to add an execute() function, have the file implement Task and Serializable and add a few variables, etc... Based on my knowledge, doing this in Eclipse would require manual editing of the program, but I would like to completely automate this process.
Thank you, again.
Much of what you need can be found in a modern IDE; and some very good IDEs are open source (eclipse and Intellij IDEA Community Edition for Java). You might look there to see if there are modules that suite your needs.
Looks like you are talking of a tool like eclipse. You might not be looking for a full fledged IDE, but the requirements that you have mentioned are fulfilled by any basic IDE.
If you wish to make one of your own, you can do that using eclipse rich client platform.
All that you would need from Java is the reflection API.

Control NetBeans Programmatically using a NetBeans Module

I know I should probably be using Eclipse but whatever...
Usualy at computer science contests I go to, we are given some sample data sets for the problems, such as "prob01.in, PizzaProblem.text, ect.". I am writing a NetBeans Module to make a project and then fill the project with java files of the input files, in the specified location.
So, on to my question. Is there any way to "control" NetBeans, and use it to make and open projects and files?
I did a breif google search and did not find anything useful.
Thanks,
-EpicDavi
http://wiki.netbeans.org/OpenProjectsProgramaticallyInNetBeansIDE#Tutorial_.7C_Open_Projects_Programatically_in_NetBeans
This page only tells you how to open a project programmatically in a Netbeans module, but it should set you on the right path. Unfortunately, I'm not well versed in NetBeans so I don't have much other insight to offer.

Can I include code from one file to the next, similar to Includes in html

Ok, Please be gentle and kind. I am taking an Android Programming class in college and its our teachers first semester so he is learning as we are... Nothing against him, he's a good teacher, just doesn't know the answer.
I don't know how to put this in Android/java language so here goes...
Is there a way to use something similar to Includes, in web design, in Android. I am looking to add similar java code from one activity to the next, can i make a file that if i change this one file it will change in all the files its included it.
I know it can be done with the XML files but i couldn't find anything to show that it can be done in the actually coding... but then again i don't know the correct terminology. Please any help would be great. And thank you for taking the time to read and answer me. I apologize if this was somewhere else, I just couldn't find it.. Thanks again
Put anything you wish to "include" in Java classes, then import them. http://leepoint.net/notes-java/language/10basics/import.html
Android Programming class! I would've loved that in University...
Regardless, if you're using Eclipse for your IDE, it should pickup any changes from your java imports automatically. If you choose to refactor other java classes within your project, a rename for example (with "Update All References"), will update all references to that class across your project.
You could also use Maven (dependency management) to pull the latest third-party (or other) .jars at build time, if that's applicable to what your needs are.
:)

Categories

Resources