1 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 ; 34 import java.util.Hashtable ; 35 36 public final class ReflectGeneratedClass extends LoadedGeneratedClass { 37 38 private final Hashtable methodCache; 39 private static final GeneratedMethod[] directs; 40 41 42 private final Class 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 jvmClass, Class factoryClass) { 53 super(cf, jvmClass); 54 methodCache = new Hashtable (); 55 this.factoryClass = factoryClass; 56 } 57 58 public Object newInstance(Context context) throws StandardException { 59 if (factoryClass == null) { 60 return super.newInstance(context); 61 } 62 63 if (factory == null) { 64 65 Throwable t; 66 try { 67 factory = (GCInstanceFactory) factoryClass.newInstance(); 68 t = null; 69 } catch (InstantiationException ie) { 70 t = ie; 71 } catch (IllegalAccessException iae) { 72 t = iae; 73 } catch (LinkageError 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 simpleName) 90 throws StandardException { 91 92 GeneratedMethod rm = (GeneratedMethod) methodCache.get(simpleName); 93 if (rm != null) 94 return rm; 95 96 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 []) null); 109 110 rm = new ReflectMethod(m); 111 } 112 methodCache.put(simpleName, rm); 113 return rm; 114 115 } catch (NoSuchMethodException 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 invoke(Object 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 t) { 164 throw StandardException.unexpectedUserException(t); 165 } 166 } 167 } 168 | Popular Tags |