KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > bytecode > d_BCValidate


1 /*
2
3    Derby - Class org.apache.derby.impl.services.bytecode.d_BCValidate
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

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 JavaDoc;
28 import org.apache.derby.iapi.services.loader.*;
29 import org.apache.derby.iapi.services.context.*;
30
31 /**
32  * Validate BC calls.
33  *
34  * @author jamie
35  */

36 class d_BCValidate
37 {
38
39     private static final String JavaDoc[] 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 JavaDoc[] NO_PARAMS = new Class JavaDoc[0];
56
57     static void checkMethod(short opcode, Type dt, String JavaDoc methodName, String JavaDoc[] debugParameterTypes, Type rt) {
58
59
60         if (SanityManager.DEBUG) {
61             String JavaDoc reason = null;
62             try {
63
64                 String JavaDoc declaringClass = dt.javaName();
65                 if (declaringClass.startsWith("org.apache.derby.exe."))
66                     return;
67
68                 // only validate against Cloudscape engine or Java classes. Not user defined classes
69
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 JavaDoc[] params = NO_PARAMS;
78
79                 Class JavaDoc declaring = loadClass(declaringClass);
80
81                 if (debugParameterTypes != null) {
82                     params = new Class JavaDoc[debugParameterTypes.length];
83                     for (int i = 0; i < debugParameterTypes.length; i++) {
84                         params[i] = loadClass(debugParameterTypes[i]);
85                     }
86
87                 }
88                 
89                 // If the class is not in the same class loader then it
90
// it must be a non-Derby class. In that case any method etc.
91
// being accessed must be public, so don't use the getDeclared
92
// methods. Default SecurityManager behaviour is to grant access to public members
93
// and members from classes loaded by the same class loader. Thus
94
// we try to fall into these categories to avoid having to grant
95
// permissions to derby jars for the function tests.
96

97                 ClassLoader JavaDoc declareLoader = declaring.getClassLoader();
98                 ClassLoader JavaDoc 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 JavaDoc 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                         // check this construct is declared by this
120
// class, has to be, right? But no harm checking.
121
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                         // check this method is declared by this
140
// class? But no harm checking.
141
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                 // do we already have a problem?
151
if (reason == null)
152                 {
153
154                     Class JavaDoc requestedReturnType = loadClass(rt.javaName());
155     
156                     // check the return type
157
if (actualReturnType.equals(requestedReturnType.getName())) {
158     
159                         // check the inteface match
160
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 JavaDoc e) {
175                 reason = e.toString();
176                 e.printStackTrace(System.out);
177             }
178
179             String JavaDoc 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 JavaDoc msg = "Invalid method " + sig + " because " + reason;
190
191             System.out.println(msg);
192             SanityManager.THROWASSERT(msg);
193         }
194     }
195
196     private static Hashtable JavaDoc primitives;
197
198     static {
199         if (SanityManager.DEBUG) {
200             primitives = new Hashtable JavaDoc();
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 JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
216
217         if (SanityManager.DEBUG) {
218
219             Class JavaDoc c = (Class JavaDoc) primitives.get(name);
220             if (c != null)
221                 return c;
222
223             if (name.endsWith("[]")) {
224                 Class JavaDoc 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