-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparison.java
More file actions
276 lines (253 loc) · 9.08 KB
/
Copy pathComparison.java
File metadata and controls
276 lines (253 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package diff;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.Stack;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Comparison {
public static void main(String[] args) throws ClassNotFoundException, MalformedURLException, FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("Enter full path to newest file:");
String newFile = input.next();
System.out.println("Enter full path to old file:");
String oldFile = input.next();
String javaDir = "C:\\Program Files (x86)\\Java\\";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int firstCompilationResult = compiler.run(System.in, System.out, System.err, newFile);
if(firstCompilationResult == 0){
System.out.println("First Compilation is successful");
}else{
System.out.println("Compilation Failed");
}
int secondCompilationResult = compiler.run(null, null, null, oldFile);
if(secondCompilationResult == 0){
System.out.println("Second Compilation is successful");
}else{
System.out.println("Compilation Failed");
}
if (firstCompilationResult == 0 && secondCompilationResult == 0){
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File fileNew = new File(newFile);
File fileOld = new File(oldFile);
URL[] firstURL = {fileNew.getParentFile().toURL()};
URL[] secondURL = {fileOld.getParentFile().toURL()};
URLClassLoader ucl = new URLClassLoader(firstURL);
//Class newClass = classLoader.loadClass(newFile.replace(".java", ".class"));
//Class oldClass = classLoader.loadClass(oldFile.replace(".java", ".class"));
Class newClass = ucl.loadClass(fileNew.getName().replace(".java", ""));
ucl = new URLClassLoader(secondURL);
Class oldClass = ucl.loadClass(fileOld.getName().replace(".java", ""));
System.out.println("AM:\n" + AddMethod(newClass, oldClass));
System.out.println("DM:\n" + DeleteMethod(newClass, oldClass));
System.out.println("CM:\n" + ChangeMethod(newClass, oldClass, new File(newFile), new File(oldFile)));
System.out.println("AF:\n" + AddField(newClass, oldClass));
System.out.println("DF:\n" + DeleteField(newClass, oldClass));
System.out.println("CFI:\n" + ChangeFieldInit(newClass, oldClass));
}
}
// Part 1
public static String AddMethod(Class newClass, Class oldClass){
Method[] newMethods = newClass.getDeclaredMethods();
Method[] oldMethods = oldClass.getDeclaredMethods();
String resultString = "";
for (Method methodNew : newMethods){
boolean found = false;
for(Method methodOld : oldMethods){
if(isSameMethod(methodNew, methodOld))
found = true;
}
// ignores erroneous methods from inner classes.
if(!found && !methodNew.getName().contains("access$")){
resultString += "Added Method " + methodNew.getName() + "\n";
}
}
return resultString;
}
// Part 2
public static String DeleteMethod(Class newClass, Class oldClass){
Method[] newMethods = newClass.getDeclaredMethods();
Method[] oldMethods = oldClass.getDeclaredMethods();
String resultString = "";
for (Method methodOld : oldMethods){
boolean found = false;
for(Method methodNew : newMethods){
if(isSameMethod(methodOld, methodNew))
found = true;
}
// ignores erroneous methods from inner classes.
if(!found && !methodOld.getName().contains("access$")){
resultString += "Deleted Method " + methodOld.getName() + "\n";
}
}
return resultString;
}
// Part 3
public static String ChangeMethod(Class newClass, Class oldClass, File newFile, File oldFile) throws FileNotFoundException{
Method[] newMethods = newClass.getDeclaredMethods();
Method[] oldMethods = oldClass.getDeclaredMethods();
String resultString = "";
// clean out methods from internal private classes
List<Method> newMeth = new ArrayList<Method>();
List<Method> oldMeth = new ArrayList<Method>();
for(Method method : newMethods)
if(!method.getName().contains("access$"))
newMeth.add(method);
for(Method method : oldMethods)
if(!method.getName().contains("access$"))
oldMeth.add(method);
for (Method methodNew : newMeth){
boolean changed = false;
for(Method methodOld : oldMeth){
// if same method signature
if(isSameMethod(methodNew, methodOld))
// if same method signature but different body
if(!isSameMethodContents(methodNew, methodOld, newFile, oldFile))
changed = true;
}
if(changed){
resultString += "Changed Method " + methodNew.getName() + "\n";
}
}
return resultString;
}
// Part 4
public static String AddField(Class newClass, Class oldClass){
Field[] newFields = newClass.getDeclaredFields();
Field[] oldFields = oldClass.getDeclaredFields();
String resultString = "";
for (Field fieldNew : newFields){
boolean found = false;
for (Field fieldOld: oldFields){
if (fieldOld.getName().equals(fieldNew.getName())){
found = true;
}
}
if (!found){
resultString += "Added field " + fieldNew.getName() + "\n";
}
}
return resultString;
}
// Part 5
public static String DeleteField(Class newClass, Class oldClass){
Field[] newFields = newClass.getDeclaredFields();
Field[] oldFields = oldClass.getDeclaredFields();
String resultString = "";
for (Field fieldOld : oldFields){
boolean found = false;
for (Field fieldNew: newFields){
if (fieldNew.getName().equals(fieldOld.getName())){
found = true;
}
}
if (!found){
resultString += "Deleted field " + fieldOld.getName() + "\n";
}
}
return resultString;
}
// Part 6
public static String ChangeFieldInit(Class newClass, Class oldClass){
Field[] newFields = newClass.getDeclaredFields();
Field[] oldFields = oldClass.getDeclaredFields();
try{
Constructor<?> ctor = newClass.getConstructor();
Object newObject = ctor.newInstance();
ctor = oldClass.getConstructor();
Object oldObject = ctor.newInstance();
String resultString = "";
List<Field> oldList = Arrays.asList(oldFields);
for (Field field : newFields){
if (oldList.stream().filter(o -> o.getName().equals(field.getName())).findFirst().isPresent()){
Field oldField = oldList.stream().filter(o -> o.getName().equals(field.getName())).findFirst().get();
if (oldField.getModifiers() != field.getModifiers()){
resultString += "Changed field accessability " + field.getName() + "\n";
}
if (!oldField.isAccessible()){
oldField.setAccessible(true);
}
if (!field.isAccessible()){
field.setAccessible(true);
}
if (oldField.get(oldObject) == null && field.get(newObject) != null){
resultString += "Added field initializer " + field.getName() + "\n";
}
else if (oldField.get(oldObject) != null && field.get(newObject) == null){
resultString += "Deleted field initializer " + field.getName() + "\n";
}
else if (!oldField.get(oldObject).equals(field.get(newObject))){
resultString += "Changed field initializer " + field.getName() + "\n";
}
}
}
return resultString;
}
catch(Exception e){
e.printStackTrace();
}
return "";
}
// Helper function to check method equality
private static boolean isSameMethod(Method m1, Method m2){
if(m1.getName().equals(m2.getName()) && m1.getModifiers() == m2.getModifiers()
&& Arrays.equals(m1.getParameterTypes(), m2.getParameterTypes()) && m1.getReturnType().equals(m2.getReturnType()))
return true;
return false;
}
private static boolean isSameMethodContents(Method mNew, Method mOld, File newFile, File oldFile) throws FileNotFoundException{
Scanner newScan = new Scanner(newFile);
Scanner oldScan = new Scanner(oldFile);
StringBuffer sbNew = new StringBuffer();
StringBuffer sbOld = new StringBuffer();
// skip to method body
newScan.useDelimiter(mNew.getName());
oldScan.useDelimiter(mOld.getName());
newScan.next();
oldScan.next();
newScan.useDelimiter("\\{");
oldScan.useDelimiter("\\{");
Stack<String> bracketsNew = new Stack<String>();
Stack<String> bracketsOld = new Stack<String>();
newScan.useDelimiter("\\}");
oldScan.useDelimiter("\\}");
do {
String sectionNew = newScan.next(); //skips to next "}"
sbNew.append(sectionNew + "}");
if(sectionNew.contains("{")){
for(int i = 0; i < sectionNew.length(); i++){
if(sectionNew.charAt(i) == '{')
bracketsNew.push("{");
}
}
bracketsNew.pop();
} while(!bracketsNew.isEmpty());
do {
String sectionOld = oldScan.next(); //skips to next "}"
sbOld.append(sectionOld + "}");
if(sectionOld.contains("{")){
for(int i = 0; i < sectionOld.length(); i++){
if(sectionOld.charAt(i) == '{')
bracketsOld.push("{");
}
}
bracketsOld.pop();
} while(!bracketsOld.isEmpty());
newScan.close();
oldScan.close();
// return true if method bodies are the same.
if(sbNew.toString().equals(sbOld.toString()))
return true;
return false;
}
}