1 package org.python.core; 3 4 import java.lang.reflect.Constructor ; 5 import java.lang.reflect.Modifier ; 6 import java.lang.reflect.InvocationTargetException ; 7 import java.lang.InstantiationException ; 8 9 10 public class PyReflectedConstructor extends PyReflectedFunction 11 { 12 13 public PyReflectedConstructor(String name) { 14 super(name); 15 __name__ = name; 16 argslist = new ReflectedArgs[1]; 17 nargs = 0; 18 } 19 20 public PyReflectedConstructor(Constructor c) { 21 this(c.getDeclaringClass().getName()); 22 addConstructor(c); 23 } 24 25 private ReflectedArgs makeArgs(Constructor m) { 26 return new ReflectedArgs(m, m.getParameterTypes(), 27 m.getDeclaringClass(), true); 28 } 29 30 public void addConstructor(Constructor m) { 31 int mods = m.getModifiers(); 32 if (!Modifier.isPublic(mods) && !JavaAccessibility.accessIsMutable()) 34 return; 35 addArgs(makeArgs(m)); 36 } 37 38 PyObject make(PyObject[] args,String [] keywords) { 40 ReflectedArgs[] argsl = argslist; 41 42 ReflectedCallData callData = new ReflectedCallData(); 43 Object method=null; 44 boolean consumes_keywords = false; 45 int n = nargs; 46 int nkeywords = keywords.length; 47 PyObject[] allArgs = null; 48 49 if (n > 0) { if (argsl[0].matches(null, args, keywords, callData)) { 52 method = argsl[0].data; 53 consumes_keywords = argsl[0].flags == ReflectedArgs.PyArgsKeywordsCall; 54 } else { 55 allArgs = args; 56 int i = 1; 57 if (nkeywords > 0) { 58 args = new PyObject[allArgs.length-nkeywords]; 59 System.arraycopy(allArgs, 0, args, 0, args.length); 60 i = 0; 61 } 62 for (; i < n; i++) { 63 ReflectedArgs rargs = argsl[i]; 64 if (rargs 65 .matches(null, args, Py.NoKeywords, callData)) { 66 method = rargs.data; break; } 67 } 68 } 69 } 70 71 if (method == null) { 73 throwError(callData.errArg, args.length, true ,false); 74 } 75 76 PyObject obj = null; 78 Constructor ctor = (Constructor )method; 79 try { 80 obj = (PyObject)ctor.newInstance(callData.getArgsArray()); 81 } 82 catch (Throwable t) { 83 throw Py.JavaError(t); 84 } 85 86 if (!consumes_keywords) { 87 int offset = args.length; 88 for (int i=0; i<nkeywords; i++) { 89 obj.__setattr__(keywords[i], allArgs[i+offset]); 90 } 91 } 92 93 return obj; 94 } 95 96 public PyObject __call__(PyObject self, PyObject[] args, 97 String [] keywords) 98 { 99 ReflectedArgs[] argsl = argslist; 100 101 if (self == null || !(self instanceof PyInstance)) { 102 throw Py.TypeError("invalid self argument to constructor"); 103 } 104 105 PyInstance iself = (PyInstance)self; 106 Class javaClass = iself.instclass.proxyClass; 107 boolean proxyConstructor=false; 110 Class declaringClass = argsl[0].declaringClass; 111 112 if (PyProxy.class.isAssignableFrom(declaringClass)) { 114 } 119 else { 120 if (!(iself instanceof PyJavaInstance)) { 121 if (declaringClass.isAssignableFrom(javaClass)) { 123 proxyConstructor = true; 124 } else { 125 throw Py.TypeError("invalid self argument"); 126 } 127 128 PyJavaClass jc = PyJavaClass.lookup(javaClass); jc.initConstructors(); 130 return jc.__init__.__call__(iself, args, keywords); 131 } 132 } 133 134 135 if (declaringClass.isAssignableFrom(javaClass)) { 136 proxyConstructor = true; 137 } else { 138 throw Py.TypeError("self invalid - must implement: "+ 139 declaringClass.getName()); 140 } 141 142 if (iself.javaProxy != null) { 143 Class sup = iself.instclass.proxyClass; 144 if (PyProxy.class.isAssignableFrom(sup)) 145 sup = sup.getSuperclass(); 146 throw Py.TypeError("instance already instantiated for "+ 147 sup.getName()); 148 } 149 150 ReflectedCallData callData = new ReflectedCallData(); 151 Object method=null; 152 153 int nkeywords = keywords.length; 155 PyObject[] allArgs = args; 156 if (nkeywords > 0) { 157 args = new PyObject[allArgs.length-nkeywords]; 158 System.arraycopy(allArgs, 0, args, 0, args.length); 159 } 160 161 int n = nargs; 163 for (int i=0; i<n; i++) { 164 ReflectedArgs rargs = argsl[i]; 165 if (rargs.matches(null, args, Py.NoKeywords, callData)) { 166 method = rargs.data; 167 break; 168 } 169 } 170 171 if (method == null) { 173 throwError(callData.errArg, args.length, self != null, false); 174 } 175 176 Object jself = null; 178 ThreadState ts = Py.getThreadState(); 179 try { 180 ts.pushInitializingProxy(iself); 181 Constructor ctor = (Constructor )method; 182 try { 183 jself = ctor.newInstance(callData.getArgsArray()); 184 } 185 catch (InvocationTargetException e) { 186 if (e.getTargetException() instanceof InstantiationException ){ 187 Class sup = iself.instclass.proxyClass.getSuperclass(); 188 String msg = "Constructor failed for Java superclass"; 189 if (sup != null) 190 msg += " " + sup.getName(); 191 throw Py.TypeError(msg); 192 } 193 else throw Py.JavaError(e); 194 } 195 catch (Throwable t) { 196 throw Py.JavaError(t); 197 } 198 } 199 finally { 200 ts.popInitializingProxy(); 201 } 202 203 iself.javaProxy = jself; 204 205 int offset = args.length; 207 for (int i=0; i<nkeywords; i++) { 208 iself.__setattr__(keywords[i], allArgs[i+offset]); 209 } 210 return Py.None; 211 } 212 213 public PyObject __call__(PyObject[] args, String [] keywords) { 214 if (args.length < 1) { 215 throw Py.TypeError("constructor requires self argument"); 216 } 217 PyObject[] newArgs = new PyObject[args.length-1]; 218 System.arraycopy(args, 1, newArgs, 0, newArgs.length); 219 220 return __call__(args[0], newArgs, keywords); 221 } 222 223 public String toString() { 224 return "<java constructor "+__name__+" "+Py.idstr(this)+">"; 226 } 227 } 228 | Popular Tags |