--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.services.bytecode.d_BCValidate\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.services.bytecode;\r
+\r
+import java.lang.reflect.*;\r
+import org.apache.derby.iapi.services.classfile.VMOpcode;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import java.util.Hashtable;\r
+import org.apache.derby.iapi.services.loader.*;\r
+import org.apache.derby.iapi.services.context.*;\r
+\r
+/**\r
+ * Validate BC calls.\r
+ *\r
+ */\r
+class d_BCValidate\r
+{\r
+\r
+ private static final String[] csPackages = {\r
+ "java",\r
+ "org.apache.derby.exe.",\r
+ "org.apache.derby.iapi.",\r
+ "org.apache.derby.jdbc.",\r
+ "org.apache.derby.iapi.",\r
+ "org.apache.derby.impl.",\r
+ "org.apache.derby.authentication.",\r
+ "org.apache.derby.catalog.",\r
+ "org.apache.derby.iapi.db.",\r
+ "org.apache.derby.iapi.types.",\r
+ "org.apache.derby.iapi.types.",\r
+ "org.apache.derby.catalog.types.",\r
+ };\r
+\r
+\r
+ private static final Class[] NO_PARAMS = new Class[0];\r
+\r
+ static void checkMethod(short opcode, Type dt, String methodName, String[] debugParameterTypes, Type rt) {\r
+\r
+\r
+ if (SanityManager.DEBUG) {\r
+ String reason = null;\r
+ try {\r
+\r
+ String declaringClass = dt.javaName();\r
+ if (declaringClass.startsWith("org.apache.derby.exe."))\r
+ return;\r
+\r
+ // only validate against Derby engine or Java classes. Not user defined classes\r
+ int p;\r
+ for (p = 0; p < csPackages.length; p++) {\r
+ if (declaringClass.startsWith(csPackages[p]))\r
+ break;\r
+ }\r
+ if (p == csPackages.length)\r
+ return;\r
+\r
+ Class[] params = NO_PARAMS;\r
+\r
+ Class declaring = loadClass(declaringClass);\r
+\r
+ if (debugParameterTypes != null) {\r
+ params = new Class[debugParameterTypes.length];\r
+ for (int i = 0; i < debugParameterTypes.length; i++) {\r
+ params[i] = loadClass(debugParameterTypes[i]);\r
+ }\r
+\r
+ }\r
+ \r
+ // If the class is not in the same class loader then it\r
+ // it must be a non-Derby class. In that case any method etc.\r
+ // being accessed must be public, so don't use the getDeclared\r
+ // methods. Default SecurityManager behaviour is to grant access to public members\r
+ // and members from classes loaded by the same class loader. Thus\r
+ // we try to fall into these categories to avoid having to grant\r
+ // permissions to derby jars for the function tests.\r
+ \r
+ ClassLoader declareLoader = declaring.getClassLoader();\r
+ ClassLoader myLoader = d_BCValidate.class.getClassLoader();\r
+ \r
+ boolean sameClassLoader = false;\r
+ if (declareLoader == myLoader)\r
+ sameClassLoader = true;\r
+ else if (declareLoader != null)\r
+ sameClassLoader = declareLoader.equals(myLoader);\r
+ \r
+ String actualReturnType;\r
+\r
+ if (methodName.equals("<init>")) {\r
+ Constructor c;\r
+ \r
+ if (sameClassLoader)\r
+ {\r
+ c = declaring.getDeclaredConstructor(params);\r
+ }\r
+ else\r
+ {\r
+ c = declaring.getConstructor(params);\r
+ \r
+ // check this construct is declared by this\r
+ // class, has to be, right? But no harm checking.\r
+ if (!c.getDeclaringClass().equals(declaring))\r
+ {\r
+ reason = "constructor " + c.toString() + " declared on " + c.getDeclaringClass() + " expected " + declaring;\r
+ }\r
+ }\r
+ \r
+ actualReturnType = "void";\r
+ } else {\r
+ Method m;\r
+ \r
+ if (sameClassLoader)\r
+ {\r
+ m = declaring.getDeclaredMethod(methodName, params);\r
+ }\r
+ else\r
+ {\r
+ m = declaring.getMethod(methodName, params);\r
+ \r
+ // check this method is declared by this\r
+ // class? But no harm checking.\r
+ if (!m.getDeclaringClass().equals(declaring))\r
+ {\r
+ reason = "method " + m.toString() + " declared on " + m.getDeclaringClass() + " expected " + declaring;\r
+ }\r
+ }\r
+ \r
+ actualReturnType = m.getReturnType().getName();\r
+ }\r
+ \r
+ // do we already have a problem?\r
+ if (reason == null)\r
+ {\r
+\r
+ Class requestedReturnType = loadClass(rt.javaName());\r
+ \r
+ // check the return type\r
+ if (actualReturnType.equals(requestedReturnType.getName())) {\r
+ \r
+ // check the inteface match\r
+ if (opcode != VMOpcode.INVOKEINTERFACE)\r
+ return;\r
+ \r
+ if (declaring.isInterface())\r
+ return;\r
+ \r
+ reason = "declaring class is not an interface";\r
+ \r
+ } else {\r
+ reason = "return type is " + actualReturnType;\r
+ }\r
+ }\r
+\r
+\r
+ } catch (Exception e) {\r
+ reason = e.toString();\r
+ e.printStackTrace(System.out);\r
+ }\r
+\r
+ String sig = dt.javaName() + " >> " + rt.javaName() + " " + methodName + "(";\r
+ if (debugParameterTypes != null) {\r
+ for (int i = 0; i < debugParameterTypes.length; i++) {\r
+ if (i != 0)\r
+ sig = sig + ", ";\r
+ sig = sig + debugParameterTypes[i];\r
+ }\r
+ }\r
+ sig = sig + ")";\r
+\r
+ String msg = "Invalid method " + sig + " because " + reason;\r
+\r
+ System.out.println(msg);\r
+ SanityManager.THROWASSERT(msg);\r
+ }\r
+ }\r
+\r
+ private static Hashtable primitives;\r
+\r
+ static {\r
+ if (SanityManager.DEBUG) {\r
+ primitives = new Hashtable();\r
+ primitives.put("boolean", Boolean.TYPE);\r
+ primitives.put("byte", Byte.TYPE);\r
+ primitives.put("char", Character.TYPE);\r
+ primitives.put("double", Double.TYPE);\r
+ primitives.put("float", Float.TYPE);\r
+ primitives.put("int", Integer.TYPE);\r
+ primitives.put("long", Long.TYPE);\r
+ primitives.put("short", Short.TYPE);\r
+ primitives.put("void", Void.TYPE);\r
+ }\r
+\r
+ }\r
+ \r
+\r
+ private static Class loadClass(String name) throws ClassNotFoundException {\r
+\r
+ if (SanityManager.DEBUG) {\r
+\r
+ Class c = (Class) primitives.get(name);\r
+ if (c != null)\r
+ return c;\r
+\r
+ if (name.endsWith("[]")) {\r
+ Class baseClass = loadClass(name.substring(0, name.length() - 2));\r
+ return Array.newInstance(baseClass, 0).getClass();\r
+ }\r
+ \r
+ return Class.forName(name);\r
+ //Added by Jeff Huang\r
+ //TODO: FIXIT\r
+ }\r
+\r
+ return null;\r
+ }\r
+}\r