1 package org.python.core; 3 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.Modifier ; 6 7 8 public class PyReflectedField extends PyObject { 9 public Field field; 10 11 public PyReflectedField() {} 12 13 public PyReflectedField(Field field) { 14 this.field = field; 15 } 16 17 public PyObject _doget(PyObject self) { 18 Object iself = null; 19 if (!Modifier.isStatic(field.getModifiers())) { 20 if (self == null) 21 return this; 22 iself = Py.tojava(self, field.getDeclaringClass()); 23 } 24 Object value; 25 26 try { 27 value = field.get(iself); 28 } catch (IllegalAccessException exc) { 29 throw Py.JavaError(exc); 30 } 31 32 return Py.java2py(value); 33 } 34 35 public boolean _doset(PyObject self, PyObject value) { 36 Object iself = null; 37 if (!Modifier.isStatic(field.getModifiers())) { 38 if (self == null) { 39 throw Py.AttributeError("set instance variable as static: "+ 40 field.toString()); 41 } 42 iself = Py.tojava(self, field.getDeclaringClass()); 43 } 44 Object fvalue = Py.tojava(value, field.getType()); 45 46 try { 47 field.set(iself, fvalue); 48 } catch (IllegalAccessException exc) { 49 throw Py.JavaError(exc); 50 } 51 return true; 52 } 53 54 public String toString() { 55 return "<reflected field "+field.toString()+" "+Py.idstr(this)+">"; 56 } 57 } 58 | Popular Tags |