Java/UML Compatibility

» Back to forum topics
  2 posts   Feedicon  
Replies: 1 - Last Post: February 04, 2011 22:42
by: ebresie
showing 1 - 2 of 2
Posted: January 01, 2011 20:49 by javydreamercsw
Is it possible to completely describe a UML class diagram using nothing but valid Java code? Is it possible to find make approximations that might be useful?
Java is a subset of UML; whilst a set of Java Classes can be represented in UML not all UML Class Diagrams have a direct Java equivalent. Java does not support multiple inheritance for example. There are other more subtle problems that prevent Java code from completely describing a class hierarchy, such as the words that appear above Association lines - these may well be used in comments found nearby to a variable declaration, but there is no automatic way to glean which word(s) should be used by reading the file.
In order to keep UML diagrams synchronous with Java code it is prudent that there be only one canonical source for all UML information, and it would be great if this could be the Java code itself. This page is about how this might be achieved?
Currently we have three alternatives, or some combination of them.
Posted: February 04, 2011 22:42 by ebresie
I would say it is possible, but that then adds a dependency on Java language verses the "Language Independence" that was mentioned previously.

Java is not a "subset of UML". Java is a language which can implement elements of a UML model with some aspects of it not being possible. In other words UML is a design blue print. Java Code is a concrete and steel implementation of the design blue print.

I think the java code could represent a straight UML however, I think we need to think of this from a model perspective. The model could be implemented in Java or C++ or Python etc as long as you have a good mapping between UML model elements to appropriate language specific keywords, APIs, variable types, interfaces, etc.

If there is not a good mapping between the two, then you probably have to either have a way of handling those cases (i.e. throw an exception for UnsupportedUMLElement or indicate "Not available in Language", etc)

We can take a lesson from Netbeans UI capabilities which has some "uneditable" regions of code that our surrounded by special comments. Only problem I sometimes have with this is that I sometimes want to update some of the code and this is not possible without knowing where to look (i.e. customize code, properties, etc).

I'm not sure that javadoc buys us anything but documentation directly.

We could as you suggested, define some annotations to reflect given elements (i.e. @umlClass, @umlMethod, @uml( type="Class" name="Foo"), etc). However we need to be cautious that we don't duplicate things (i.e. if the java class is defined then annotated again as a class)

I think annotations might be a little cleaner per say. I know it's not completely the same, but I like to think of annotations as macros that get expanded, so we could then "expand out" at compile time to reflect any necessary "linking" code which links elements to a given model element.
Replies: 1 - Last Post: February 04, 2011 22:42
by: ebresie