A New Class Visualization Module for NetBeans

Tags:
Subheadline: 
Graph interdependencies inside one Java class
Location: 
Somewhere in Antarctica

Last week I wrote a little module that uses the NetBeans Visual Library and the Javac Tree API to create a graph of the contents of a Java file, showing dependencies between class members. It's now available on the NetBeans daily build update center - if you're running a daily build of NetBeans, just go to Tools | Plugins and download Graphical Class Viewer.

My question is, would anybody actually find this useful? It was mostly an experiment to get more fluent with a couple of APIs I need to know well - but it seems like it could be handy if you're going to edit a class you didn't write and want to see where the action is - or if you've got a bunch of encapsulated fields and want to quickly see if anything is not using the getters and setters.

What it does:
  • Shows a graph (see screenshot below)
  • If you hover the mouse over, say, a method, then the path to things that method calls/uses will be shown in red, and things that call that method are shown in blue
  • You can click the arrow button on the widget for any class member and see its source code

Feel free to try it out and let me know if you think it's something worth putting more work into.

Also, if anyone knows of any speedy and good hub-and-spoke or other layout algorithms for the case where you have n nodes each of which can have up to n-1 connections to other nodes, feel free to let me know - what it does now is less than optimal. At some point I may think through the geometry to do it nicely, but I'm not sure when.

4
Average: 4 (1 vote)
AttachmentSize
gcv1.jpg72.65 KB

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Rui Vale replied on Thu, 2008/02/28 - 6:12am

Hi!

I found it useful but I'm not using a daily build of NetBeans. I'm using NetBeans 6.0.1. Can I use your plugin in this version? If yes, how can I install it?

 

Thanks in advance!

 

RV 

Sachin Agrawal replied on Thu, 2008/02/28 - 7:07am

I have the same query as Rui has

and I also want to know if there is support available in Netbeans JavaSource API to parse the java expressions like "((2==2) && (1==1))" and give us back the complete ExpressionTree in form of either BinaryTree or UnaryTree (as defined in CompilerTree API), or if there is support available to how to use the javacAPI directly to parse the above string and get the ExpressionTree, or any place in Netbeans we are using to parse the expression.

 

Thanks in Advance!

Sachin

 

 

 

 

 

 

Milos Kleint replied on Thu, 2008/02/28 - 7:52am

hey Tim,

if you manage to come up with a cool layouting algorithm, let me know. I got the same layouting constraints when showing maven project's transitive dependencies in a graph.

 

Milos 

Tim Boudreau replied on Thu, 2008/02/28 - 10:55am in response to: ag_sachin

Sachin:  Yes, you can do that.  Basically you would either (in NetBeans) call JavaSource.forFileObject(someFile), and then run a Task <CompilationController> against it.  You get the TypeDecl's (top level classes in the file) from the CompilationController, and then to each one (unless JDK 1.0 multi-classes-per-file there is only one) you pass your own TreeVisitor, which overrides visitExpressionTree() and whatever else you need (typical is to set a flag when you're in an outer element you know you're interested in, and then override other methods to do more in depth stuff only when you know you are in something you're interested in).

 If you know the location of what you want to parse, you can probably start from whatever is there, backtrack until you find the enclosing block and then just work with that. 

Tim Boudreau replied on Thu, 2008/02/28 - 11:33am in response to: ruivale

Rui:  If you want to access the Alpha update center from a release build such as 6.0 or 6.0.1, you need to add that update center's URL to Tools | Plugins - go to the settings tab, click Add and use the URL

http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/updates.xml.gz

-Tim 

		       			
  				

Tim Boudreau replied on Thu, 2008/02/28 - 6:54pm in response to: ag_sachin

Sachin: You inspired me to release this AST browser tool which I've had sitting around for almost a year. It will probably help you get a sense of exactly what the internals of javac's ASTs look like.

Geertjan Wielenga replied on Fri, 2008/02/29 - 3:02am

I made something similar, but not nearly as good, sometime ago: 

http://blogs.sun.com/geertjan/entry/source_file_visualizers 

Why don't you try to make yours part of the official distribution? 

Sachin Agrawal replied on Fri, 2008/02/29 - 6:05am in response to: tim

Thanks Tim for the response, but the fact is, i am able to get the tree from existing javaSource of a method and all the constructs of a conditional expression, but my problem is in doing the reverse of that..

I have a String in form of a (3==2 & (4==3)) (with complexity of nth place) as keyed in by user, now i want to convert it into the BinaryTree using the TreeMaker instance make.Binary(operator,LeftExpression,rigghtExpression), so that i am able to insert the resultant tree into the code block and add it to the source.

I am able to create a new method using maker.Method(params..) using method text as it is, but not the Conditional Expression, as there is no method directly present which takes string as input and returns a Conditional Expression in form of BinaryTree as depicted above.

If you give me an hint, about something like is already been done somewehere else or in netbeans itself, it would be of great help.

Cheers,

Sachin.

 

 

Michael Bar-sinai replied on Fri, 2008/02/29 - 6:20am

Useful? Useful??!? This would have been a lifesaver on a project I'm working on, but we use eclipse. You might want to look at relo, an MIT project that goes along those lines. I used it for a while, but as it makes the build very slow I had to turn it off.

http://relo.csail.mit.edu/

As for automatic layout, check out JUNG at http://jung.sourceforge.net/index.html.

Michael

 

Jan Lahoda replied on Fri, 2008/02/29 - 4:06pm in response to: ag_sachin

Sachin: org.netbeans.api.java.source.TreeUtilities.parseExpression might be what you are looking for.

Sachin Agrawal replied on Tue, 2008/03/04 - 4:42am in response to: jlahoda

Thankyou very much Jan' 

Yes i was looking for that only, i don't know how but i missed that one from the API-documentation.

 

 

Jan Moons replied on Thu, 2008/03/06 - 9:06am

Thanks very much for this module Tim. It is extremely interesting for me as I am trying to get to grips with the visual library and need all the examples I can get. Some of the examples on the visual library site lead to dead links. Now my question: is the source available anywhere so I can inspect it? I am not very familiar with mercurial, but if you could lead me in to the right location I would be very grateful.

Thanks again

Tim Boudreau replied on Thu, 2008/03/06 - 3:20pm

To get the sources:

hg pull http://hg.netbeans.org/main//contrib

cd graphicclassview

Or you can browse the sources here.

Jan Moons replied on Fri, 2008/03/07 - 9:28am

 

great Tim, thank you!

 

Now on trying to get these sources to work: I did a "hg clone http://hg.netbeans.org/main " and a "hg clone http://hg.netbeans.org/main//contrib " so I now have a folder main which contains many modules and also the contrib folder. The contrib folder contains many modules among which the graphicclassview. However, when I try to open ANY module (under main or under main/contrib) in netbeans I get

java.io.IOException: netbeans.org-type module not in a complete netbeans.org source root: NbModuleProject[MasterFileObject@6e0061[C:/NetbeansSourceTree/main/contrib/graphicclassview]]

 

In the project name field in the "open project" dialog. What do I have to do to get this to work?

I already tried to issue ant in the main folder which started to build a bunch of things but eventually resulted in an OutOfMemoryError... Is building the sources necessary? What do I have to do next?

 

thanks again!

Jan Moons replied on Mon, 2008/03/10 - 4:09pm

Anyone?

Tim Boudreau replied on Mon, 2008/03/10 - 7:15pm in response to: JanMoons

NetBeans modules in the NetBeans source tree expect to build into $NB_SRC/nbbuild/netbeans - contrib/ should be set up as a subdir of $NB_SRC.  Since contrib/ is now a separate hg repository, I only gave instructions for how to get contrib (figuring you might just want a look at the source code and contrib/ is a much smaller download).  To do a full build, first get the full NetBeans sources:

hg pull http://hg.netbeans.org/main// 

and then move your checkout of contrib/ underneath the root of the checkout.  Then run Ant against the master build script (you will need to set the environment variable ANT_OPTS=-Xmx512M for the build to complete).

Alternately, you could probably fiddle with the project metadata a bit to convince NetBeans that it's not a netbeans.org source tree module - simplest would be to create a new module project somewhere else on your disk (not under the nb source root), copy the sources and the dependencies section of nbproject/project.xml into the new module's project.xml and that should do it.

 Sorry for the confusion - I was trying to save you a huge checkout :-/ 

Jan Moons replied on Tue, 2008/03/11 - 5:17am in response to: tim

Thanks a lot again Tim, you save me a great deal of time. I did try to do the build but got the outofmemory error - so  ANT_OPTS=-Xmx512M should do the trick! I'll try right away

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.