KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > reflect > ReflectGeneratedClass


1 /*
2
3    Derby - Class org.apache.derby.impl.services.reflect.ReflectGeneratedClass
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.reflect;
23
24 import org.apache.derby.iapi.services.loader.GeneratedMethod;
25 import org.apache.derby.iapi.services.loader.GeneratedByteCode;
26 import org.apache.derby.iapi.services.loader.ClassFactory;
27
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.iapi.reference.SQLState;
30
31 import org.apache.derby.iapi.services.context.Context;
32
33 import java.lang.reflect.Method JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 public final class ReflectGeneratedClass extends LoadedGeneratedClass {
37
38     private final Hashtable JavaDoc methodCache;
39     private static final GeneratedMethod[] directs;
40
41
42     private final Class JavaDoc factoryClass;
43     private GCInstanceFactory factory;
44
45     static {
46         directs = new GeneratedMethod[10];
47         for (int i = 0; i < directs.length; i++) {
48             directs[i] = new DirectCall(i);
49         }
50     }
51
52     public ReflectGeneratedClass(ClassFactory cf, Class JavaDoc jvmClass, Class JavaDoc factoryClass) {
53         super(cf, jvmClass);
54         methodCache = new Hashtable JavaDoc();
55         this.factoryClass = factoryClass;
56     }
57
58     public Object JavaDoc newInstance(Context context) throws StandardException {
59         if (factoryClass == null) {
60             return super.newInstance(context);
61         }
62
63         if (factory == null) {
64
65             Throwable JavaDoc t;
66             try {
67                 factory = (GCInstanceFactory) factoryClass.newInstance();
68                 t = null;
69             } catch (InstantiationException JavaDoc ie) {
70                 t = ie;
71             } catch (IllegalAccessException JavaDoc iae) {
72                 t = iae;
73             } catch (LinkageError JavaDoc le) {
74                 t = le;
75             }
76
77             if (t != null)
78                 throw StandardException.newException(SQLState.GENERATED_CLASS_INSTANCE_ERROR, t, getName());
79         }
80
81         GeneratedByteCode ni = factory.getNewInstance();
82         ni.initFromContext(context);
83         ni.setGC(this);
84         ni.postConstructor();
85         return ni;
86
87     }
88
89     public GeneratedMethod getMethod(String JavaDoc simpleName)
90         throws StandardException {
91
92         GeneratedMethod rm = (GeneratedMethod) methodCache.get(simpleName);
93         if (rm != null)
94             return rm;
95
96         // Only look for methods that take no arguments
97
try {
98             if ((simpleName.length() == 2) && simpleName.startsWith("e")) {
99
100                 int id = ((int) simpleName.charAt(1)) - '0';
101
102                 rm = directs[id];
103
104
105             }
106             else
107             {
108                 Method m = getJVMClass().getMethod(simpleName, (Class JavaDoc []) null);
109                 
110                 rm = new ReflectMethod(m);
111             }
112             methodCache.put(simpleName, rm);
113             return rm;
114
115         } catch (NoSuchMethodException JavaDoc nsme) {
116             throw StandardException.newException(SQLState.GENERATED_CLASS_NO_SUCH_METHOD,
117                 nsme, getName(), simpleName);
118         }
119     }
120 }
121
122 class DirectCall implements GeneratedMethod {
123
124     private final int which;
125
126     DirectCall(int which) {
127
128         this.which = which;
129     }
130
131     public Object JavaDoc invoke(Object JavaDoc ref)
132         throws StandardException {
133
134         try {
135
136             GeneratedByteCode gref = (GeneratedByteCode) ref;
137             switch (which) {
138             case 0:
139                 return gref.e0();
140             case 1:
141                 return gref.e1();
142             case 2:
143                 return gref.e2();
144             case 3:
145                 return gref.e3();
146             case 4:
147                 return gref.e4();
148             case 5:
149                 return gref.e5();
150             case 6:
151                 return gref.e6();
152             case 7:
153                 return gref.e7();
154             case 8:
155                 return gref.e8();
156             case 9:
157                 return gref.e9();
158             }
159             return null;
160         } catch (StandardException se) {
161             throw se;
162         }
163         catch (Throwable JavaDoc t) {
164             throw StandardException.unexpectedUserException(t);
165         }
166     }
167 }
168
Popular Tags