是否可以在所有情况下比较两个 .java 文件的方法和字段?
Is it possible to compare two .java files methods and fields in all cases?
我目前正在学习项目管理 class,教授布置的作业是在所有情况下以编程方式比较两个 .java 文件的方法和字段。 我认为实际上不可能做到但也许我错了!
赋值规范如下(我知道它非常模糊)
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)
这就是我正在使用的方法,我的方法是使用反射,因为它允许您做任何事情,但检测方法主体中的差异。
我曾考虑过创建解析器的想法,但这似乎很荒谬,尤其是对于一个 3 学分的项目管理本科生 class。 BeyondCompare 之类的工具不会列出更改了哪些方法或字段,只是列出不同的行,因此不符合要求。
我交了这个作业,几乎整个 class 都失败了,原因是 "our code would not work for java files with external dependencies that are not given or with java files in different projects" - 这是完全正确的,但我也在想,不可能做到。
我正在尝试确定一个具体的答案,说明为什么这实际上是不可能的,或者学习一些关于为什么这可能的新知识,所以任何见解都会很棒。
你在这里错了,你已经开始检查 .class 文件(使用反射)。 上面列出的一些信息甚至不可用在那个阶段(泛型,内联函数)。您需要做的是将 .java 文件解析为文本。只有这样才能真正解决问题。一个非常高级的解决方案可能是编写一个程序:
- 读取文件
- 为每个 .java 文件构造一个特定对象,其中包含所有需要比较的信息(函数名称、实例变量名称等)
- 比较构造的对象(示例:addedFunctions = functionsFromA.removeAll(functionsFromB))以提供请求的结果
注意:如果这是一个作业,你不应该使用其他人提供的解决方案,你需要自己做。如果您使用其他人编写的库,您很可能得不到一分。
我目前正在学习项目管理 class,教授布置的作业是在所有情况下以编程方式比较两个 .java 文件的方法和字段。 我认为实际上不可能做到但也许我错了!
赋值规范如下(我知道它非常模糊)
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)
这就是我正在使用的方法,我的方法是使用反射,因为它允许您做任何事情,但检测方法主体中的差异。
我曾考虑过创建解析器的想法,但这似乎很荒谬,尤其是对于一个 3 学分的项目管理本科生 class。 BeyondCompare 之类的工具不会列出更改了哪些方法或字段,只是列出不同的行,因此不符合要求。
我交了这个作业,几乎整个 class 都失败了,原因是 "our code would not work for java files with external dependencies that are not given or with java files in different projects" - 这是完全正确的,但我也在想,不可能做到。
我正在尝试确定一个具体的答案,说明为什么这实际上是不可能的,或者学习一些关于为什么这可能的新知识,所以任何见解都会很棒。
你在这里错了,你已经开始检查 .class 文件(使用反射)。 上面列出的一些信息甚至不可用在那个阶段(泛型,内联函数)。您需要做的是将 .java 文件解析为文本。只有这样才能真正解决问题。一个非常高级的解决方案可能是编写一个程序:
- 读取文件
- 为每个 .java 文件构造一个特定对象,其中包含所有需要比较的信息(函数名称、实例变量名称等)
- 比较构造的对象(示例:addedFunctions = functionsFromA.removeAll(functionsFromB))以提供请求的结果
注意:如果这是一个作业,你不应该使用其他人提供的解决方案,你需要自己做。如果您使用其他人编写的库,您很可能得不到一分。