Skip to main content
summaryrefslogtreecommitdiffstats
blob: cfe091f5dbd60a075e982ab3dc05d3a2db4dba6b (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tests.junit;

import java.util.zip.*;
import java.util.*;
import java.lang.reflect.*;
import java.io.*;
import java.net.*;

/**
 * Create SWT Junit stubs based on a swt jar used as a reference
 * Command line tool - run without parameters to get help information
 */
public class junitGen {
	String destFolder = null;
	int counter = 0;
	
	/**
	 * @param jarfile the url of a jar file
	 * @return all public classes in the jar
	 */
	public Vector getClasses(String jarfile) {
		Vector vector = new Vector();
		try {
			ZipFile zipFile = new ZipFile(jarfile);
			File file = new File(jarfile);
			URL url = file.toURL();
			ClassLoader loader = new URLClassLoader(new URL[] { url });

			Enumeration entries = zipFile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry)entries.nextElement();
				String name = entry.getName();
				if (name.endsWith(".class")) {
					name = name.substring(0, name.length() - ".class".length());
					name = name.replace('/', '.');
					Class clazz = Class.forName(name, false, loader);
					if (filterClass(clazz)) vector.add(clazz);
				}
			}
		} catch (Exception e) {
			System.out.println("Exception " + e);
			return null;
		}
		return vector;
	}
	/**
	 * Create a Junit java test file
	 * @param clazz class to create the test for
	 */
	public void createFileTest(Class clazz) {
		try {
			String name = getClassName(clazz);
			name = destFolder + "/" + name + ".java";
			FileWriter writer = new FileWriter(name);
			createSwtTest(writer, clazz);
			writer.close();
		} catch (Exception e) {
			System.out.println("Exception " + e);
		}
		
	}

	void createSwtTest(FileWriter writer, Class clazz) throws IOException {
		SwtTest test = new SwtTest(writer, clazz);
		test.writeCopyRights();
		test.writePackage();
		test.newLine();
		test.writeImports();
		test.newLine();
		test.writeClassComments();
		test.writeClassDeclaration();
		test.newLine();
		test.writeConstructor();		
		test.newLine();
		test.writeMain();
		test.newLine();
		test.writeSetUp();
		test.newLine();
		test.writeTearDown();
		test.newLine();
		test.writeMethods();
		test.newLine();
		test.writeSuite();
		test.newLine();
		test.writeMethodNames();
		test.newLine();
		test.writeRunTest();
		test.writeClassClosure();
	}
	
	public void setDestinationFolder(String dest) {
		destFolder = dest;	
	}

	public void incrementCounter() {
		counter++;
	}
	
	public int getCounter() {
		return counter;
	}
	
	public static void createTests(String jarfile, String destFolder) {
		junitGen gen = new junitGen();
		Vector classes = gen.getClasses(jarfile);
		gen.setDestinationFolder(destFolder);
		if (classes == null) {
			System.out.println("No classes found.");
			return;
		}
		Enumeration entries = classes.elements();
		while (entries.hasMoreElements()) {
			gen.createFileTest((Class)entries.nextElement());
		}
		System.out.println("Test stubs written: " + gen.getCounter());
	}

	public void diff(Vector oldClasses, Vector newClasses) {
		SwtDiff diff = new SwtDiff(oldClasses, newClasses);
		diff.compute();		
	}
	
	public static void diffTests(String jarfileOld, String jarfileNew) {
		junitGen gen = new junitGen();
		Vector oldClasses = gen.getClasses(jarfileOld);
		if (oldClasses == null) {
			System.out.println("No classes found in old jar");
			return;
		}
		Vector newClasses = gen.getClasses(jarfileNew);
		if (newClasses == null) {
			System.out.println("No classes found in new jar");
			return;
		}
		gen.diff(oldClasses, newClasses);	
	}

	public static void main(String[] args) {
		if (args == null || args.length < 3) {
			System.out.println("Usage: java junitGen flag arg1 arg2 ...");
			System.out.println(" where flag is one of:");
			System.out.println("create: create the junit test stubs");
			System.out.println("       arg1: source jar e.g. d:/swt.jar");
			System.out.println("       arg2: destination e.g. d:/tests");
			System.out.println("diff:   diff 2 jars showing which tests must be removed,");
			System.out.println("        and added");
			System.out.println("       arg1: old jar e.g. d:/old/swt.jar");
			System.out.println("       arg2: new jar e.g. d:/new/swt.jar");	
			return;
		}
		if (args[0].equals("create")) createTests(args[1], args[2]);
		else if (args[0].equals("diff")) diffTests(args[1], args[2]);
	}
	
	boolean filterClass(Class clazz) {
		/* JUnit tests for public classes only */
		int modifiers = clazz.getModifiers();
		if (!Modifier.isPublic(modifiers)) return false;

		/* JUnit tests for classes which are not part of an internal package */
		String packageName = clazz.getPackage().getName();
		if (packageName.indexOf("internal") != -1) return false;

		/* JUnit tests for classes with public constructors or public methods */
		Constructor[] constructors = getFilteredConstructors(clazz);
		Method[] methods = getFilteredMethods(clazz);
		if (constructors.length == 0 && methods.length == 0) return false;
		return true;
	}

	String getClassName(Class clazz) {
		String name = clazz.getName();
		name = "Test_" + name.replace('.', '_');
		return name;
	}

	String getMethodName(Constructor constructor) {
		String name = "test_Constructor";
		Class[] parameters = constructor.getParameterTypes();
		for (int i = 0; i < parameters.length; i++) {
			name += getTypeSignature(parameters[i]);
		}
		return name;
	}

	String getMethodName(Method method) {
		String name = "test_" + method.getName();
		Class[] parameters = method.getParameterTypes();
		for (int i = 0; i < parameters.length; i++) {
			name += getTypeSignature(parameters[i]);
		}
		return name;
	}
		
	String getTypeSignature(Class clazz) {
		if (clazz == Integer.TYPE) return "I";
		if (clazz == Boolean.TYPE) return "Z";
		if (clazz == Long.TYPE) return "J";
		if (clazz == Short.TYPE) return "S";
		if (clazz == Character.TYPE) return "C";
		if (clazz == Byte.TYPE) return "B";
		if (clazz == Float.TYPE) return "F";
		if (clazz == Double.TYPE) return "D";
		if (clazz.isArray()) {
			Class componentType = clazz.getComponentType();
			return "$" + getTypeSignature(componentType);
		}
		return "L" + clazz.getName().replace('.', '_');
	}

	Constructor[] getFilteredConstructors(Class clazz) {
		Constructor[] constructors = clazz.getConstructors();
		Arrays.sort(constructors, new Comparator() {
			public int compare(Object a, Object b) {
				return (getMethodName((Constructor)a).compareTo(getMethodName((Constructor)b)));					
			}
		});
		int count = 0;
		for (int i = 0; i < constructors.length; i++) {
			Constructor constructor = constructors[i];
			int modifiers = constructor.getModifiers();
			if (Modifier.isPublic(modifiers)) {
				constructors[count++] = constructors[i];
			}
		}
		Constructor[] result = new Constructor[count];
		System.arraycopy(constructors, 0, result, 0, count);
		return result;
	}
		
	Method[] getFilteredMethods(Class clazz) {
		Method[] methods = clazz.getDeclaredMethods();
		Arrays.sort(methods, new Comparator() {
			public int compare(Object a, Object b) {
				return (getMethodName((Method)a).compareTo(getMethodName((Method)b)));					
			}
		});
		int count = 0;
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			int modifiers = method.getModifiers();
			if (Modifier.isPublic(modifiers)) {
				methods[count++] = methods[i];
			}
		}
		Method[] result = new Method[count];
		System.arraycopy(methods, 0, result, 0, count);
		return result;		
	}


	public class SwtTest {
		FileWriter writer;
		Class clazz;
		String lineSep = "\r\n";
		
		SwtTest(FileWriter writer, Class clazz) {
			this.writer = writer;
			this.clazz = clazz;
		}
		
		void newLine() throws IOException {
			writer.write(lineSep);
		}
		
		void writePackage() throws IOException {
			writer.write("package org.eclipse.swt.tests.junit;");
			writer.write(lineSep);
			writer.write(lineSep);
		}
		
		void writeCopyRights() throws IOException {
			writer.write(
				"/*******************************************************************************" + lineSep +
 				" * Copyright (c) 2000, 2004 IBM Corporation and others." + lineSep +
 				" * All rights reserved. This program and the accompanying materials" + lineSep +
 				" * are made available under the terms of the Eclipse Public License v1.0" + lineSep +
 				" * which accompanies this distribution, and is available at" + lineSep +
 				" * http://www.eclipse.org/legal/epl-v10.html" + lineSep +
 				" * " + lineSep +
 				" * Contributors:" + lineSep +
 				" *     IBM Corporation - initial API and implementation" + lineSep +
				" *******************************************************************************/");
			writer.write(lineSep);
		}
		
		void writeImports() throws IOException {
			writer.write(
				"import junit.framework.*;" + lineSep +
				"import junit.textui.*;");
			writer.write(lineSep);
		}
		
		void writeClassComments() throws IOException {
			writer.write(
				"/**" + lineSep +
				" * Automated Test Suite for class " + clazz.getName() + lineSep +
				" *" + lineSep +
				" * @see " + clazz.getName() + lineSep +
				" */"
			);
			writer.write(lineSep);
		}

		boolean hasSwtJunitSuperClass(Class clazz) {
			Class superClass = clazz.getSuperclass();
			if (superClass == null) return false;
			String packageName = superClass.getPackage().getName();
			if (!packageName.startsWith("org.eclipse.swt")) return false;
			if (!filterClass(superClass)) return false;
			return true;						
		}
		
		String getSwtJunitSuperClassName(Class clazz) {
			String defaultClass = "SwtTestCase";
			Class superClass = clazz.getSuperclass();
			if (superClass == null) return defaultClass;
			String packageName = superClass.getPackage().getName();
			if (!packageName.startsWith("org.eclipse.swt")) return defaultClass;
			if (!filterClass(superClass)) return defaultClass;
			return getClassName(superClass);
		}
		
		void writeClassDeclaration() throws IOException {
			String name = getClassName(clazz);
			String superClassName = getSwtJunitSuperClassName(clazz);
			writer.write(
				"public class " + name + " extends " + superClassName +" {"
			);
			writer.write(lineSep);
		}
		
		void writeConstructor() throws IOException {
			String name = getClassName(clazz);
			writer.write(
				"public " + name + "(String name) {" + lineSep +
				"	super(name);" + lineSep +
				"}"
			);
			writer.write(lineSep);
		}		
		
		void writeMain() throws IOException {
			writer.write(
				"public static void main(String[] args) {" + lineSep +
				"	TestRunner.run(suite());" + lineSep +
				"}"
			);
			writer.write(lineSep);
		}

		void writeSetUp() throws IOException {
			writer.write(
				"protected void setUp() {"
			);
			writer.write(lineSep);
			if (hasSwtJunitSuperClass(clazz)) {
				writer.write("	super.setUp();");
				writer.write(lineSep);
			}
			writer.write("}");
			writer.write(lineSep);
		}

		void writeTearDown() throws IOException {
			writer.write(
				"protected void tearDown() {"
			);
			writer.write(lineSep);
			if (hasSwtJunitSuperClass(clazz)) {
				writer.write("	super.tearDown();");
				writer.write(lineSep);
			}
			writer.write("}");
			writer.write(lineSep);
		}
		
		void writeMethods() throws IOException {
			Constructor[] constructors = getFilteredConstructors(clazz);
			for (int i = 0; i < constructors.length; i++) {
				writeMethod(constructors[i]);
				if (i < constructors.length - 1) writer.write(lineSep);
			}
			
			Method[] methods = getFilteredMethods(clazz);
			if (constructors.length > 0 && methods.length > 0) writer.write(lineSep);
			for (int i = 0; i < methods.length; i++) {
				writeMethod(methods[i]);
				if (i < methods.length - 1) writer.write(lineSep);
			}
		}
		
		void writeMethod(Constructor constructor) throws IOException {
			String name = getMethodName(constructor);
			writer.write(
				"public void " + name + "() {" + lineSep +
				"	warnUnimpl(\"Test " + name + " not written\");" + lineSep +
				"}"
			);
			writer.write(lineSep);
			incrementCounter();
		}
		
		void writeMethod(Method method) throws IOException {
			String name = getMethodName(method);
			writer.write(
				"public void " + name + "() {" + lineSep +
				"	warnUnimpl(\"Test " + name + " not written\");" + lineSep +
				"}"
			);
			writer.write(lineSep);
			incrementCounter();
		}
		
		void writeSuite() throws IOException {
			String name = getClassName(clazz);
			writer.write(
				"public static Test suite() {" + lineSep +
				"	TestSuite suite = new TestSuite();" + lineSep +	
				"	java.util.Vector methodNames = methodNames();" + lineSep +
				"	java.util.Enumeration e = methodNames.elements();" + lineSep +
				"	while (e.hasMoreElements()) {" + lineSep +
				"		suite.addTest(new " + name + "((String)e.nextElement()));" + lineSep +
				"	}" + lineSep +
				"	return suite;" + lineSep +
				"}"
			);
			writer.write(lineSep);
		}

		void writeMethodNames() throws IOException {
			writer.write(
				"public static java.util.Vector methodNames() {" + lineSep +
				"	java.util.Vector methodNames = new java.util.Vector();"
			);
			writer.write(lineSep);
			Constructor[] constructors = getFilteredConstructors(clazz);
			for (int i = 0; i < constructors.length; i++) {
				String name = getMethodName(constructors[i]);
				writer.write(
					"	methodNames.addElement(\"" + name + "\");"
				);
				writer.write(lineSep);
			}
			Method[] methods = getFilteredMethods(clazz);
			for (int i = 0; i < methods.length; i++) {
				String name = getMethodName(methods[i]);
				writer.write(
					"	methodNames.addElement(\"" + name + "\");"
				);
				writer.write(lineSep);
			}
			if (hasSwtJunitSuperClass(clazz)) {
				String superClassName = getSwtJunitSuperClassName(clazz);
				writer.write("	methodNames.addAll(" + superClassName + ".methodNames()); // add superclass method names");
				writer.write(lineSep);
			}
			writer.write(
				"	return methodNames;" + lineSep +
				"}"
			);
			writer.write(lineSep);
		}
		
		void writeRunTest() throws IOException {
			writer.write(
				"protected void runTest() throws Throwable {"
			);
			writer.write(lineSep);
			Constructor[] constructors = getFilteredConstructors(clazz);
			Method[] methods = getFilteredMethods(clazz);
			for (int i = 0; i < constructors.length + methods.length; i++) {
				if (i == 0) writer.write("	if"); else writer.write("	else if");
				String name = i < constructors.length ? getMethodName(constructors[i]) : getMethodName(methods[i - constructors.length]);
				writer.write(
					" (getName().equals(\"" + name + "\")) " + name + "();"
				);
				writer.write(lineSep);
			}
			if (hasSwtJunitSuperClass(clazz)) {
				if (constructors.length + methods.length > 0) {
					writer.write("	else ");
				} else {
					writer.write("	");
				}
				writer.write("super.runTest();");
				writer.write(lineSep);
			}			
			writer.write("}");
			writer.write(lineSep);		
		}
		
		void writeClassClosure() throws IOException {
			writer.write("}");
			writer.write(lineSep);
		}
	}
	
	public class DiffClass {
		/* the newClass has been added */
		public static final int NEW = 1;
		/* the oldClass has been removed */
		public static final int REMOVED = 2;
		/* the removedMethods from oldClass have been removed (if any)
		 * the newMethods from newClass have been added
		 */
		public static final int MODIFIED = 3;
		
		int type;
		Class oldClass;
		Class newClass;
		Method[] removedMethods;
		Method[] newMethods;
		Constructor[] removedConstructors;
		Constructor[] newConstructors;

		public DiffClass(int type, 
			Class oldClass, Class newClass, 
			Method[] removedMethods, Method[] newMethods,
			Constructor[] removedConstructors, Constructor[] newConstructors) {
			this.type = type;
			this.oldClass = oldClass;
			this.newClass = newClass;
			this.removedMethods = removedMethods;
			this.newMethods = newMethods;
			this.removedConstructors = removedConstructors;
			this.newConstructors = newConstructors;
		}
		
		public String toString() {
			switch (type) {
				case NEW: {
					return "NEW: " + getClassName(newClass);
				}
				case REMOVED: {
					return "REMOVED: " + getClassName(oldClass);
				}
				case MODIFIED: {
					String string = "MODIFIED: " + getClassName(oldClass);
					if (removedConstructors != null) {
						string += "\r\n\t" + removedConstructors.length + " constructor(s) removed";
						for (int i = 0; i < removedConstructors.length; i++) {
							string += "\r\n\t" + getMethodName(removedConstructors[i]);
						}
					}

					if (newConstructors != null) {
						string += "\r\n\t" + newConstructors.length + " constructor(s) added";
						for (int i = 0; i < newConstructors.length; i++) {
							string += "\r\n\t" + getMethodName(newConstructors[i]);
						}
					}
					if (removedMethods != null) {
						string += "\r\n\t" + removedMethods.length + " method(s) removed";
						for (int i = 0; i < removedMethods.length; i++) {
							string += "\r\n\t" + getMethodName(removedMethods[i]);
						}
					}

					if (newMethods != null) {
						string += "\r\n\t" + newMethods.length + " method(s) added";
						for (int i = 0; i < newMethods.length; i++) {
							string += "\r\n\t" + getMethodName(newMethods[i]);
						}
					}
					return string;
				}
				default: {
					return "invalid DiffClass "+type;
				}
			}
		}
	}
	
	public class SwtDiff {
		Class[] oldClasses, newClasses;
		/* removed[i] true means oldClasses[i] has been removed */
		boolean[] removed;
		/* added[i] true means newClasses[i] has been added */
		boolean[] added;
		/* old2New[i] == j >= 0 means oldClasses[i] corresponds to newClasses[j] */
		int[] old2New;
		int[] new2Old;
		Vector diffs = new Vector();
		
		public SwtDiff(Vector oldClasses, Vector newClasses) {
			Enumeration oldClassesEnum = oldClasses.elements();
			this.oldClasses = new Class[oldClasses.size()];
			int cnt = 0;
			while (oldClassesEnum.hasMoreElements()) {
				this.oldClasses[cnt++] = (Class)oldClassesEnum.nextElement();
			}

			Enumeration newClassesEnum = newClasses.elements();
			this.newClasses = new Class[newClasses.size()];
			cnt = 0;
			while (newClassesEnum.hasMoreElements()) {
				this.newClasses[cnt++] = (Class)newClassesEnum.nextElement();
			}
			old2New = new int[this.oldClasses.length];
			new2Old = new int[this.newClasses.length];
		}
		
		public void compute() {
			compareClasses();
			createClassDiff();
			outputClassDiff();		
		}

		void compareClasses() {
			for (int i = 0; i < oldClasses.length; i++) {
				old2New[i] = -1;
				for (int j = 0; j < newClasses.length; j++) {
					if (oldClasses[i].getName().equals(newClasses[j].getName())) {
						old2New[i] = j;
					}
				}
			}
			
			for (int i = 0; i < newClasses.length; i++) {
				new2Old[i] = -1;
				for (int j = 0; j < oldClasses.length; j++) {
					if (newClasses[i].getName().equals(oldClasses[j].getName())) {
						new2Old[i] = j;
					}
				}
			}
		}
		
		void createClassDiff() {
			for (int i = 0; i < old2New.length; i++) {
				if (old2New[i] < 0) {
					/* Found a class which has been removed */
					DiffClass diff = new DiffClass(
						DiffClass.REMOVED,
						oldClasses[i],
						null,
						null,
						null,
						null,
						null);
					diffs.add(diff);
				} else {
					/* Found a class which is either identical or which contains
					 * modified methods
					 */
					Class oldClass = oldClasses[i];
					Class newClass = newClasses[old2New[i]];
					Method[] removedMethods = getRemovedMethods(oldClass, newClass);
					Method[] addedMethods = getAddedMethods(oldClass, newClass);
					Constructor[] removedConstructors = getRemovedConstructors(oldClass, newClass);
					Constructor[] addedConstructors = getAddedConstructors(oldClass, newClass);
					if (removedMethods != null || addedMethods != null || 
						removedConstructors != null || addedConstructors != null) {
						DiffClass diff = new DiffClass(
					 		DiffClass.MODIFIED,
					 		oldClass,
					 		newClass,
					 		removedMethods,
					 		addedMethods,
					 		removedConstructors,
					 		addedConstructors
					 	);
					 	diffs.add(diff);
					 }
				}
			}
			for (int i = 0; i < new2Old.length; i++) {
				if (new2Old[i] < 0) {
					/* Found a class which has been added */
					DiffClass diff = new DiffClass(
						DiffClass.NEW,
						null,
						newClasses[i],
						null,
						null,
						null,
						null
					);
					diffs.add(diff);
				}
			}
		}

		Method[] getRemovedMethods(Class oldClass, Class newClass) {
			Method[] oldMethods = getFilteredMethods(oldClass);
			Method[] newMethods = getFilteredMethods(newClass);
			
			int count = 0;
			for (int i = 0; i < oldMethods.length; i++) {
				String oldName = getMethodName(oldMethods[i]);
				boolean removed = true;
				for (int j = 0; j < newMethods.length; j++) {
					String newName = getMethodName(newMethods[j]);
					if (oldName.equals(newName)) {
						removed = false;
						break;
					}
				}
				if (removed) oldMethods[count++] = oldMethods[i];
			}
			if (count == 0) return null;
			Method[] result = new Method[count];
			System.arraycopy(oldMethods, 0, result, 0, count);
			return result;
		}
		
		Method[] getAddedMethods(Class oldClass, Class newClass) {
			return getRemovedMethods(newClass, oldClass);
		}
		
		Constructor[] getRemovedConstructors(Class oldClass, Class newClass) {
			Constructor[] oldConstructors = getFilteredConstructors(oldClass);
			Constructor[] newConstructors = getFilteredConstructors(newClass);
			
			int count = 0;
			for (int i = 0; i < oldConstructors.length; i++) {
				String oldName = getMethodName(oldConstructors[i]);
				boolean removed = true;
				for (int j = 0; j < newConstructors.length; j++) {
					String newName = getMethodName(newConstructors[j]);
					if (oldName.equals(newName)) {
						removed = false;
						break;
					}
				}
				if (removed) oldConstructors[count++] = oldConstructors[i];
			}
			if (count == 0) return null;
			Constructor[] result = new Constructor[count];
			System.arraycopy(oldConstructors, 0, result, 0, count);
			return result;
		}
		
		Constructor[] getAddedConstructors(Class oldClass, Class newClass) {
			return getRemovedConstructors(newClass, oldClass);
		}
		
		void outputClassDiff() {
			Enumeration elements = diffs.elements();
			int changes = 0;
			while (elements.hasMoreElements()) {
				DiffClass diff = (DiffClass)elements.nextElement();
				System.out.println("["+changes+"] "+ diff);
				changes++;
			}
		}
	}
}

Back to the top