So what are the basic things I need to know about to do something useful?

Note: This FAQ item applies only to NetBeans 4.x and 5.x. The Java parsing infrastructure in NetBeans 6 has been replaced with a new API.

Working with JavaModel can sometimes be a bit like being a rat in a maze - so many tunnels, you know the cheese must be down one of them.

Essentially, you're working with an abstract syntax tree for a java source, wrapped up in objects each of which implements an enormous number of interfaces; each interface describes some single facet of the element in question; many the interfaces have methods that will return a java.util.List of other similar objects. Figuring out what things you can, or are likely to, get back in one of these lists can be a challenge - generics would help a bit, but we can't yet use generics in NetBeans' APIs. System.out.println and Alt-Shift-O are likely to be your best friends here.

It involves a certain amount of swimming in interface soup. For example, a Method object implements the following interfaces: CallableFeature, BehavioralFeature, TypedElement, AnnotableElement, Feature, GenericElement, ClassMember, NamedElement and Element. Since these interfaces are machine generated from a model of the Java language, the javadoc often returns such gems as "The value of the references reference".

It ain't pretty. It is planned that this whole thing be replaced with a much simpler API based more directly on working with the abstract syntax tree generated by the parser (the Tree API / JSR 199 ), in the not too distant future.

It is, however, all logical, and for the most part, the names make it clear what things do (a notable exception is "MultipartId" - this refers to an object which can be a reference to a class, comprised of a package identifier, and a class identifier). So it is possible to write code that will work with this API reasonably quickly - eventually your sense of smell develops and you can tell which tunnel the cheese is down.

Unpretty but practical advice for working with JavaModel: If you are going to do serious work with it, you will probably want to get a source checkout so you can open these classes and traverse the maze of interfaces - you will need to at some point. Code completion is actually a pretty good resource for figuring out what you can do on a given object; there's probably no avoiding System.out.printlning the contents of lists you get back from an object (you'll need to copy the list first to get a meaningful result) to figure out what the types of the objects you can get from a given call are. Don't be too sanguine that your casts are always perfectly safe - for example, a ClassDefinition is usually a JavaClass, but on a broken source file it may be UnresolvedClass. It all makes sense - it's just a little byzantine.

There are a few projects in the NetBeans sources that are relatively small, do work with java sources, and can provide examples of doing basic things with it:

  • refactoring
  • java/hints
  • contrib/codeInfo
  • java/navigation
  • contrib/importcruncher

In a pinch, think of some function the NetBeans IDE already offers that, to do what it does, must touch the same objects you need to find, and search the java module's sources for likely names for the actions or classes involved. Then use Find Usages liberally to sniff out the specifics.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo