01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.object.util;
06:
07: import java.lang.reflect.Method;
08: import java.lang.reflect.Modifier;
09: import java.util.ArrayList;
10: import java.util.HashSet;
11: import java.util.Iterator;
12: import java.util.List;
13: import java.util.Set;
14:
15: public class OverrideCheck {
16:
17: public static void check(Class parent, Class subClass) {
18: Set super Methods = methodsFor(parent);
19: Set subMethods = methodsFor(subClass);
20:
21: List missing = new ArrayList();
22:
23: for (Iterator i = super Methods.iterator(); i.hasNext();) {
24: String method = (String) i.next();
25:
26: if (!subMethods.contains(method)) {
27: // This class should be overriding all methods on the super class
28: missing.add(method);
29: }
30: }
31:
32: if (!missing.isEmpty()) {
33: throw new RuntimeException("Missing overrides:\n" + missing);
34: }
35: }
36:
37: private static Set methodsFor(Class c) {
38: Method[] methods = c.getDeclaredMethods();
39:
40: Set set = new HashSet();
41: for (int i = 0; i < methods.length; i++) {
42: Method m = methods[i];
43:
44: int access = m.getModifiers();
45:
46: if (Modifier.isAbstract(access)
47: || Modifier.isStatic(access)
48: || Modifier.isPrivate(access)) {
49: continue;
50: }
51:
52: StringBuffer sig = new StringBuffer();
53: sig.append(m.getName()).append('(');
54:
55: Class[] parameterTypes = m.getParameterTypes();
56: for (int j = 0; j < parameterTypes.length; j++) {
57: sig.append(parameterTypes[j].getName());
58: if (j < (parameterTypes.length - 1)) {
59: sig.append(',');
60: }
61: }
62: sig.append(')');
63:
64: set.add(sig.toString());
65: }
66: return set;
67: }
68:
69: }
|