001: /*
002:
003: Derby - Class org.apache.derby.impl.services.reflect.ReflectGeneratedClass
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.reflect;
023:
024: import org.apache.derby.iapi.services.loader.GeneratedMethod;
025: import org.apache.derby.iapi.services.loader.GeneratedByteCode;
026: import org.apache.derby.iapi.services.loader.ClassFactory;
027:
028: import org.apache.derby.iapi.error.StandardException;
029: import org.apache.derby.iapi.reference.SQLState;
030:
031: import org.apache.derby.iapi.services.context.Context;
032:
033: import java.lang.reflect.Method;
034: import java.util.Hashtable;
035:
036: public final class ReflectGeneratedClass extends LoadedGeneratedClass {
037:
038: private final Hashtable methodCache;
039: private static final GeneratedMethod[] directs;
040:
041: private final Class factoryClass;
042: private GCInstanceFactory factory;
043:
044: static {
045: directs = new GeneratedMethod[10];
046: for (int i = 0; i < directs.length; i++) {
047: directs[i] = new DirectCall(i);
048: }
049: }
050:
051: public ReflectGeneratedClass(ClassFactory cf, Class jvmClass,
052: Class factoryClass) {
053: super (cf, jvmClass);
054: methodCache = new Hashtable();
055: this .factoryClass = factoryClass;
056: }
057:
058: public Object newInstance(Context context) throws StandardException {
059: if (factoryClass == null) {
060: return super .newInstance(context);
061: }
062:
063: if (factory == null) {
064:
065: Throwable t;
066: try {
067: factory = (GCInstanceFactory) factoryClass
068: .newInstance();
069: t = null;
070: } catch (InstantiationException ie) {
071: t = ie;
072: } catch (IllegalAccessException iae) {
073: t = iae;
074: } catch (LinkageError le) {
075: t = le;
076: }
077:
078: if (t != null)
079: throw StandardException.newException(
080: SQLState.GENERATED_CLASS_INSTANCE_ERROR, t,
081: getName());
082: }
083:
084: GeneratedByteCode ni = factory.getNewInstance();
085: ni.initFromContext(context);
086: ni.setGC(this );
087: ni.postConstructor();
088: return ni;
089:
090: }
091:
092: public GeneratedMethod getMethod(String simpleName)
093: throws StandardException {
094:
095: GeneratedMethod rm = (GeneratedMethod) methodCache
096: .get(simpleName);
097: if (rm != null)
098: return rm;
099:
100: // Only look for methods that take no arguments
101: try {
102: if ((simpleName.length() == 2)
103: && simpleName.startsWith("e")) {
104:
105: int id = ((int) simpleName.charAt(1)) - '0';
106:
107: rm = directs[id];
108:
109: } else {
110: Method m = getJVMClass().getMethod(simpleName,
111: (Class[]) null);
112:
113: rm = new ReflectMethod(m);
114: }
115: methodCache.put(simpleName, rm);
116: return rm;
117:
118: } catch (NoSuchMethodException nsme) {
119: throw StandardException.newException(
120: SQLState.GENERATED_CLASS_NO_SUCH_METHOD, nsme,
121: getName(), simpleName);
122: }
123: }
124: }
125:
126: class DirectCall implements GeneratedMethod {
127:
128: private final int which;
129:
130: DirectCall(int which) {
131:
132: this .which = which;
133: }
134:
135: public Object invoke(Object ref) throws StandardException {
136:
137: try {
138:
139: GeneratedByteCode gref = (GeneratedByteCode) ref;
140: switch (which) {
141: case 0:
142: return gref.e0();
143: case 1:
144: return gref.e1();
145: case 2:
146: return gref.e2();
147: case 3:
148: return gref.e3();
149: case 4:
150: return gref.e4();
151: case 5:
152: return gref.e5();
153: case 6:
154: return gref.e6();
155: case 7:
156: return gref.e7();
157: case 8:
158: return gref.e8();
159: case 9:
160: return gref.e9();
161: }
162: return null;
163: } catch (StandardException se) {
164: throw se;
165: } catch (Throwable t) {
166: throw StandardException.unexpectedUserException(t);
167: }
168: }
169: }
|