如何从加载的Java代码创建UML图? [英] How to create UML diagram from loaded Java code?

查看:173
本文介绍了如何从加载的Java代码创建UML图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的节目有疑问。我的程序应该是能够从Java代码创建UML图,但我不知道,如何设计方法,它将检索(加载)Java关键字,对象等。注意:我不能使用自动程序来创建UML图。这是我的论文。

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.

我的想法是使用Java关键字创建枚举类,可以在UML图中看到并使用此枚举检查所有加载的代码。但是有几个问题,我无法解决,特别是空间。下一个问题如下:

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:

例如我有代码:

[space][space]public[space]class[space][space][space]SomeClass[space]{
[empty line]
private int something;
public BufferedReader br;
private ArrayList<File> al;
}

此代码的评论:


  • [space]代表代码中的经典差距。

  • 我已故意突出显示差距,因为它在语法上是正确的。

  • 我不知道如何将所有Java类加载为BufferedReader,ArrayList等或其他对象。

感谢您的回复。我想有更好的方法可以解决这个问题。

Thank you for any reply. I suppose there is a better way to solve this problem.

推荐答案

添加前两个答案和你的评论,特别是re。 '纯Java代码'。

Adding to the two previous answers and your comments, particularly re. 'pure java code'.

假设我理解你的问题,你要做的第一件事就是将java源代码(即文本文件)转换成一些数据结构。从那里,您可以为数据结构生成UML图。

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:


  • 解析文本(由@ user714965建议)

  • 使用反射(由@Joop Eggen建议)

手写解析器不是一件轻而易举的事。您对创建枚举类等的评论表明您正在思考的问题。但是,在极少数情况下,手动编码的解析器只是推荐的解决方案。有一整套理论和实践致力于解析算法和技术。我真的不确定你是否想参与你的项目。

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.

大多数人会使用解析器生成器(例如 antlr )从语法定义生成解析器。鉴于java的流行,对于antlr,至少有一个现有的java语法。我不太确定'纯Java代码'约束意味着什么。 Antlr生成一个纯Java代码解析器,这样就可以了。如果你的意思是你需要从头开始编写所有代码,那么使用解析器生成器就会出来。但这似乎是一个非常奇怪的约束...

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...

无论如何。你的另一个选择是使用反射。这实际上使用了JVM中的解析器,并为您提供查询和访问的API访问权限。导航代码本身。 java.lang.reflect(显然)也是纯java - 所以你调用它的代码也是如此。

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.

Eclipse / Netbeans API将为你提供另一种可能的路径。实际上它们只是另一个解析器,它提供了一组代表你的java代码的数据结构。

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.

我不确定这是否有帮助。也许你可以更多地解释'纯Java代码'约束。

I'm not sure if that helps. Perhaps you could explain the 'pure java code' constraint a bit more.

这篇关于如何从加载的Java代码创建UML图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆