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 = (JavaModelPackage) resource.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 = (JavaClass) res.getClassifiers().get(0);
            // create a new method
            JavaModelPackage jmp = (JavaModelPackage) cls.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

Parent Category: Other API Tips