java left logo
java middle logo
java right logo

Home arrow Other API Tips
Main Menu
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog

Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews

Registered Users: 4115
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
How can I programmatically add a method to a java source? E-mail
User Rating: / 2

You need to get a Resource for your java file. If you only do this on the open file in the editor, you can do it like this:

JMIUtils utils = JMIUtils.get(baseDoc);
Resource resource = utils.getResource();

For where to get the document object baseDoc, see getting the document in the current editor.

If you're starting from a DataObject, do this:

Resource resource = JavaModel.getResource dob.getPrimaryFile () );

A Resource is a model representation of a .java or .class file - it is a thing that can contain 0 or more java classes and has imports.

Now you need to get a JavaClass for the java class you want to edit. Call resource.getClassifiers(), and iterate the collection you get back (unless JDK 1.0 style sources, there will be only one JavaClass, named the same as the file).

Now you've got your JavaClass. That's the thing you want to add the method to. There is a method getFeatures() that will return a List of "features" (methods, fields, initializers, whatnot).

Simply adding your method object to the returned list will add it to the class (be careful with this list object - if you remove objects from it, you will remove the corresponding code from the source file - it is live).

But where do you get that method object? In a bit of stunning non-obviousness, the object that represents the repository package (a repository package corresponds to a classpath element - it serves as a container/database for all metadata for a given classpath element) is the thing that can create prototype method objects (and other things) which you can add to classes in that classpath element package. So,

JavaModelPackage jpck = (JavaModelPackageresource.refImmediatePackage();

gets you the object you need - or actually, it gives you a way to get strikingly ambiguously named objects called things like MethodClass, ArrayClass, and other such, which you then use as factories. So, call

jpck.getMethod().createMethod() - use the version of this method which takes arguments for body, javadoc, arguments, etc., or just get a Method object back and call setBodyText() on it.

All of this needs to be done while holding a write lock on the model, so in your entry point you'll want something like:

        boolean rollback = true;
        try {
            // get the resource corresponding to a given file
            Resource res = JavaModel.getResource(fileObject);
            // get the first class from the resource
            JavaClass cls = (JavaClassres.getClassifiers().get(0);
            // create a new method
            JavaModelPackage jmp = (JavaModelPackagecls.refImmediatePackage();
            Method method = jmp.getMethod().createMethod(...);
            // add the method to the class
            rollback = false;
        finally {

You'll probably have to add a whole heap of dependencies to your module to access all the classes you'll need to - probably refactoring module has most of them and you could copy from project.xml there, or just use project properties, on the libraries tab, click add library and type the name of the library you need.

Source: NetBeans FAQ

 Related Tips

< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.

Name (required)

E-Mail (required)

Your email will not be displayed on the site - only to our administrator

Comment Enable HTML code : Yes No

java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008
Java is a trademark of Sun Microsystems, Inc.