The Java Program: Method_Main.java
1 import java.lang.reflect.Method;
2 import java.lang.reflect.Modifier;
3 import java.util.Vector;
4
5 class Method_Main {
6
7 Method_Main(Class c) {
8 Vector superMethods = new Vector();
9 Method[] methods = c.getDeclaredMethods();
10
11 // Retrieve the declared methods of c's superclasses.
12 c = c.getSuperclass();
13 while (c != null) {
14 Method[] ms = c.getDeclaredMethods();
15 for (int i=0; i<ms.length; i++) {
16 superMethods.addElement(ms[i]);
17 }
18 c = c.getSuperclass();
19 }
20
21 // Compare each method in c with methods in all superclasses.
22 for (int i=0; i<methods.length; i++) {
23 for (int j=0; j<superMethods.size(); j++) {
24 Method m2 = (Method)superMethods.elementAt(j);
25
26 if (sameNameAndSignature(methods[i], m2)) {
27 System.out.println(methods[i]);
28 System.out.println(" overrides "
29 + m2.getDeclaringClass().getName());
30 break;
31 }
32 }
33 }
34 }
35
36 boolean sameNameAndSignature (Method m1, Method m2) {
37 // Is either private?
38 if (Modifier.isPrivate(m1.getModifiers())
39 || Modifier.isPrivate(m2.getModifiers())) {
40 return false;
41 }
42
43 // Are the names the same?
44 if (!m1.getName().equals(m2.getName())) {
45 return false;
46 }
47
48 // Get the parameter type lists.
49 Class[] p1 = m1.getParameterTypes();
50 Class[] p2 = m2.getParameterTypes();
51
52 // Are the parameter list sizes the same?
53 if (p1.length != p2.length) {
54 return false;
55 }
56
57 // Are the types the same?
58 for (int i=0; i<p1.length; i++) {
59 if (p1[i] != p2[i]) {
60 return false;
61 }
62 }
63 return true;
64 }
65
66 public static void main (String[] args) {
67 if (args.length != 1) {
68 System.err.println("Usage: java Method_Main <classname>");
69 } else {
70 try {
71 new Method_Main(Class.forName(args[0]));
72 } catch (ClassNotFoundException e) {
73 e.printStackTrace();
74 }
75 }
76 }
77 }