1 21 22 package org.apache.derby.impl.services.bytecode; 23 24 import java.lang.reflect.*; 25 import org.apache.derby.iapi.services.classfile.VMOpcode; 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 import java.util.Hashtable ; 28 import org.apache.derby.iapi.services.loader.*; 29 import org.apache.derby.iapi.services.context.*; 30 31 36 class d_BCValidate 37 { 38 39 private static final String [] csPackages = { 40 "java", 41 "org.apache.derby.exe.", 42 "org.apache.derby.iapi.", 43 "org.apache.derby.jdbc.", 44 "org.apache.derby.iapi.", 45 "org.apache.derby.impl.", 46 "org.apache.derby.authentication.", 47 "org.apache.derby.catalog.", 48 "org.apache.derby.iapi.db.", 49 "org.apache.derby.iapi.types.", 50 "org.apache.derby.iapi.types.", 51 "org.apache.derby.catalog.types.", 52 }; 53 54 55 private static final Class [] NO_PARAMS = new Class [0]; 56 57 static void checkMethod(short opcode, Type dt, String methodName, String [] debugParameterTypes, Type rt) { 58 59 60 if (SanityManager.DEBUG) { 61 String reason = null; 62 try { 63 64 String declaringClass = dt.javaName(); 65 if (declaringClass.startsWith("org.apache.derby.exe.")) 66 return; 67 68 int p; 70 for (p = 0; p < csPackages.length; p++) { 71 if (declaringClass.startsWith(csPackages[p])) 72 break; 73 } 74 if (p == csPackages.length) 75 return; 76 77 Class [] params = NO_PARAMS; 78 79 Class declaring = loadClass(declaringClass); 80 81 if (debugParameterTypes != null) { 82 params = new Class [debugParameterTypes.length]; 83 for (int i = 0; i < debugParameterTypes.length; i++) { 84 params[i] = loadClass(debugParameterTypes[i]); 85 } 86 87 } 88 89 97 ClassLoader declareLoader = declaring.getClassLoader(); 98 ClassLoader myLoader = d_BCValidate.class.getClassLoader(); 99 100 boolean sameClassLoader = false; 101 if (declareLoader == myLoader) 102 sameClassLoader = true; 103 else if (declareLoader != null) 104 sameClassLoader = declareLoader.equals(myLoader); 105 106 String actualReturnType; 107 108 if (methodName.equals("<init>")) { 109 Constructor c; 110 111 if (sameClassLoader) 112 { 113 c = declaring.getDeclaredConstructor(params); 114 } 115 else 116 { 117 c = declaring.getConstructor(params); 118 119 if (!c.getDeclaringClass().equals(declaring)) 122 { 123 reason = "constructor " + c.toString() + " declared on " + c.getDeclaringClass() + " expected " + declaring; 124 } 125 } 126 127 actualReturnType = "void"; 128 } else { 129 Method m; 130 131 if (sameClassLoader) 132 { 133 m = declaring.getDeclaredMethod(methodName, params); 134 } 135 else 136 { 137 m = declaring.getMethod(methodName, params); 138 139 if (!m.getDeclaringClass().equals(declaring)) 142 { 143 reason = "method " + m.toString() + " declared on " + m.getDeclaringClass() + " expected " + declaring; 144 } 145 } 146 147 actualReturnType = m.getReturnType().getName(); 148 } 149 150 if (reason == null) 152 { 153 154 Class requestedReturnType = loadClass(rt.javaName()); 155 156 if (actualReturnType.equals(requestedReturnType.getName())) { 158 159 if (opcode != VMOpcode.INVOKEINTERFACE) 161 return; 162 163 if (declaring.isInterface()) 164 return; 165 166 reason = "declaring class is not an interface"; 167 168 } else { 169 reason = "return type is " + actualReturnType; 170 } 171 } 172 173 174 } catch (Exception e) { 175 reason = e.toString(); 176 e.printStackTrace(System.out); 177 } 178 179 String sig = dt.javaName() + " >> " + rt.javaName() + " " + methodName + "("; 180 if (debugParameterTypes != null) { 181 for (int i = 0; i < debugParameterTypes.length; i++) { 182 if (i != 0) 183 sig = sig + ", "; 184 sig = sig + debugParameterTypes[i]; 185 } 186 } 187 sig = sig + ")"; 188 189 String msg = "Invalid method " + sig + " because " + reason; 190 191 System.out.println(msg); 192 SanityManager.THROWASSERT(msg); 193 } 194 } 195 196 private static Hashtable primitives; 197 198 static { 199 if (SanityManager.DEBUG) { 200 primitives = new Hashtable (); 201 primitives.put("boolean", Boolean.TYPE); 202 primitives.put("byte", Byte.TYPE); 203 primitives.put("char", Character.TYPE); 204 primitives.put("double", Double.TYPE); 205 primitives.put("float", Float.TYPE); 206 primitives.put("int", Integer.TYPE); 207 primitives.put("long", Long.TYPE); 208 primitives.put("short", Short.TYPE); 209 primitives.put("void", Void.TYPE); 210 } 211 212 } 213 214 215 private static Class loadClass(String name) throws ClassNotFoundException { 216 217 if (SanityManager.DEBUG) { 218 219 Class c = (Class ) primitives.get(name); 220 if (c != null) 221 return c; 222 223 if (name.endsWith("[]")) { 224 Class baseClass = loadClass(name.substring(0, name.length() - 2)); 225 return Array.newInstance(baseClass, 0).getClass(); 226 } 227 228 return Class.forName(name); 229 } 230 231 return null; 232 } 233 } 234 | Popular Tags |