Reputation: 359
I have question about my program. My program should be to able create UML diagram from Java code, but I don't know, how design method, which will retrieves (load) Java keywords, objects etc. Notice: I can not use automated programs to create UML diagram. It is my thesis.
My idea was create enum class with Java keywords, that are seen in UML diagram and check all loaded code with this enum. But there are several problems, which I am not able to solve, especially spaces. Next problem is following:
For example I have code:
[space][space]public[space]class[space][space][space]SomeClass[space]{
[empty line]
private int something;
public BufferedReader br;
private ArrayList<File> al;
}
Comments for this code:
Thank you for any reply. I suppose there is a better way to solve this problem.
Upvotes: 1
Views: 2162
Reputation: 14067
Simply choose a parser generator like JavaCC (your task 1). Use a finished Java grammar (or build an own one). Generate Lexer (2) and write your own Parser stuff into the grammar (3). Save the tree from your parser and/or modify it first to get an XML/XMI representation. For 5) you should really choose an existing tool as writing an own one can be a complete additional thesis...
Upvotes: 0
Reputation: 9952
Adding to the two previous answers and your comments, particularly re. 'pure java code'.
Assuming I understand your question right, the first thing you have to do is transform the java source code (i.e. text files) into some data structures. From there you can generate UML diagrams for the data structures.
Assuming so that's a pretty common pattern. There are generally 2 approaches for converting text to data structures:
Hand-writing a parser is not a trivial affair. Your comment about creating an enum class etc. suggests that's what you're thinking. However a hand-coded parser would only be the recommended solution in a very few cases. There's a whole body of theory and practice dedicated to parsing algorithms and techniques. I'm really not sure you want to get in to that for your project.
Most people would use a parser-generator (e.g. antlr) to generate the parser from a grammar definition. Given the popularity of java, there's at least one existing java grammar for antlr. I'm not quite sure what the 'pure java code' constraint means. Antlr generates a pure java code parser so that would be ok. If you mean you need to write all the code from scratch then using a parser generator would be out. But that seems a very strange constraint...
Anyway. Your other option is using reflection. That in effect uses the parser in the JVM and gives you API access to query & navigate the code itself. java.lang.reflect is also (obviously) pure java - so your code calling it would be too.
The Eclipse/Netbeans API will provide you another possible route. In effect they are just another 'parser' that provides a set of data structures representing your java code.
I'd strongly recommend one of those three approaches instead of writing your own parser by hand.
I'm not sure if that helps. Perhaps you could explain the 'pure java code' constraint a bit more.
Upvotes: 1
Reputation: 109547
If you can use compiled code, that would be nice. In java you can load a class without initialisation, and inspect its structure with java.lang.reflect. Of course parameter names will be missing for instance.
For more details there are alternative class parsing libraries like ASM.
Upvotes: 1
Reputation: 39632
You should think about using a library which parses Java code for you. Certainly I couldn't tell you one, try google for one.
Here is the approach which I would follow. First read this article about the Eclipse Abstract Syntax Tree. Using the Eclipse AST would imply you are designing your tool as an eclipse plug-in. If you don't want to do this the article would give you some hints of how to parse a source tree nevertheless.
Upvotes: 1