简体   繁体   English

是否可以在所有情况下比较两个.java文件的方法和字段?

[英]Is it possible to compare two .java files methods and fields in all cases?

I am currently taking a project management class and the professor gave this assignment to compare two .java files methods and fields in all cases programmatically. 我目前正在上一个项目管理班,教授给了这个作业,以编程方式比较了所有情况下的两种.java文件方法和字段。 I don't think it's actually possible to do but maybe I am wrong! 我认为实际上是不可能的,但也许我错了!

The assignment spec is as following (its extremely ambiguous I know) 分配规范如下(我知道它非常模棱两可)

In this assignment, you are required to write a comparison tool for two 
versions of a Java source file.
Your program takes as input two .java files representing those two versions
and reports the following atomic changes:

1. AM: Add a new method
2. DM: Delete a method
3. CM: Change the body of a method (note: you need to handle the case where a method is
relocated within the body of its class)
4. AF: Add a field
5. DF: Delete a field
6. CFI: Change the definition of an instance field initializer (including (i) adding an initialization to a
field, (ii) deleting an initialization of a field, (iii) making changes to the initialized value of a field,
and (iv) making changes to a field modifier, e.g., private to public)

So that's what I am working with and my approach was to use reflection as it allows you to do everything but detect differences in the method body. 这就是我正在使用的方法,我的方法是使用反射,因为它可以执行所有操作,但可以检测方法主体中的差异。

I had considered the idea that you could create a parser but that seemed ridiculous, especially for a 3 credit undergrad class in project management. 我曾考虑过可以创建一个解析器的想法,但这似乎很荒谬,尤其是对于项目管理中3个学分的本科生类。 Tools like BeyondCompare don't list what methods or fields changed, just lines that are different so don't meet the requirements. 诸如BeyondCompare之类的工具不会列出更改的方法或字段,仅列出不同的行,因此不符合要求。

I turned in this assignment and pretty much the entire class failed it with the reason as "our code would not work for java files with external dependencies that are not given or with java files in different projects" - which is completely correct but also I'm thinking, impossible to do. 我上交了这个作业,几乎整个类都因为它的原因而失败了,原因是“我们的代码不适用于没有给出外部依存关系的Java文件或不同项目中的Java文件”-完全正确,但是我我在想,不可能做。

I am trying to nail down a concrete answer as to why this is not actually possible to do or learn something new about why this is possible so any insight would be great. 我试图确定一个具体的答案,说明为什么实际上不可能做到这一点,或者要学到一些新的知识来了解为什么这样做是可能的,所以任何见解都会很棒。

What you got wrong here is that you have started to examine the .class files (using reflection). 您在这里出错的是,您已经开始检查.class文件(使用反射)。 Some of the information listed above is not even available at that stage (generics, in-lined functions). 上面列出的某些信息在该阶段甚至不可用(泛型,内联函数)。 What you need to do is parsing the .java files as text. 您需要做的是将.java文件解析为文本。 That is the only way to actually solve the problem. 这是真正解决问题的唯一方法。 A very high-level solution could be writing a program that: 一个非常高级的解决方案可能是编写一个程序,该程序:

  • reads the files 读取文件
  • constructs a specific object for each .java file containing all the informations that needs to be compared (name of the functions, name of the instance variables, etc) 为每个.java文件构造一个特定的对象,其中包含需要比较的所有信息(函数名称,实例变量的名称等)
  • compares the constructed objects (example: addedFunctions = functionsFromA.removeAll(functionsFromB)) to provide the requested results 比较构造的对象(例如:addedFunctions = functionsFromA.removeAll(functionsFromB))以提供请求的结果

Note: if this is an assignment, you should not be using solutions provided by anybody else, you need to do it on your own. 注意:如果这是一项任务,则不应使用任何人提供的解决方案,而需要自己完成。 Likely you will not get a single point if you use a library written by somebody else. 如果使用其他人编写的库,则可能不会获得任何分数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM