Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm on the way developing a Java application where user can provide a class diagram and get the corresponding Java code.

I don't know how can I let the user interactively draw a class diagram in Java. I am currently getting the required parameters like attributes, functions directly from the user, and then I render a class diagram for him. I show the class diagram on a jdialog.But when it comes to multiple class diagrams, it screws me. Is there a better way to do this?

This is an example of a class diagram, I need to generate this from a Java program, given the values and relationship.

enter image description here

share|improve this question
1  
You're asking for quite a lot, and some of your questions are off topic here. –  Yannis Rizos Nov 21 '11 at 16:23
    
@FrustratedWithFormsDesigner no! I'm doing as a small project, later on I'd like to add other features like from the sequence diagram, activity diagram etc, you can hope for future questions :D –  COD3BOY Nov 21 '11 at 16:24
1  
You might have a look at Dia for some ideas. dia-installer.de –  Robert Harvey Nov 21 '11 at 16:24
    
Also Dia2Code sourceforge.net/projects/dia2code –  Robert Harvey Nov 21 '11 at 16:25
2  
@Sanjay: Did you look at ArgoUML? I believe it's open source and you can see how it has been done and get some ideas. But here's a question WHY do you want to do this? What's the intent/purpose? There are a ton of tools out there that could do this for you? Maybe writing a plug-in/interface module for those tools would be more efficient IMHO, than reinventing the wheel from scratch –  PhD Nov 21 '11 at 17:58

2 Answers 2

up vote 3 down vote accepted

First point: this is fairly non-trivial. Second point: the fact that the Java environment makes the Java compiler directly available will help a lot in implementing this. I believe you should be able to collect most (all?) the information you need by walking the AST with the compiler tree API. At least from the looks of things, the part you'll care the most about will be the ClassTree interface. So, the basic idea would be to create your tree visitor, walk the tree, and collect information about the ClassTree objects you find.

Once you've collected the information, it becomes mostly a matter of drawing a nicely formatted result. If you have some funds available, I've heard good things about yFiles for Java (I should also mention that the same company's yWorks UML Doclet is supposed to do nearly what you're talking about, but from JavaDac comments rather than the source itself). There are, as you'd expect, lots of alternatives to that as well. I don't know enough about most (any?) of them to comment further on them though (I did use GraphViz once, but so long ago that I don't remember much, and what little I might remember is probably obsolete anyway).

share|improve this answer
    
+1 for a good link and concise description. –  Emmad Kareem Nov 21 '11 at 19:00

I have some experience with code generation, significantly less with diagramming. I have a few suggestions:

I notice you use human readable text. I suggest googling for Inflector.java and choosing an appropriate implementation. This will also help with variable names for unlabelled relationships between classes (pluralization gives you List<Foo> foos and stuff like that).

I use UML diagrams, but I use an xml serialized form. The UML XMI stuff from OMG has been helpful to me. It's much easier to import/export the underlying information for UML diagrams than to lay them out yourself. This might not be applicable since you seem to want to do the diagramming portion yourself. Much has been written on laying out diagrams. I suggest you do some reading if you want to generate diagrams from source without resulting in a jumbled mess.

I use both antlr and StringTemplate. Starting with a good parser/grammar to read source is nice. StringTemplate is pretty nice for generating source code. I also use it at work for email templates. If you don't want to read source, suck in a jar or class file and go to town. I have a lot of code built around BCEL. You might want to look at ASM if you don't like BCEL.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.