📄 pyjavaclass.java
字号:
return; if (o instanceof PyBeanProperty) { PyBeanProperty oldProp = (PyBeanProperty)o; if (prop.myType == oldProp.myType) { // If this adds nothing over old property, do nothing if ((prop.getMethod == null || oldProp.getMethod != null) && (prop.setMethod == null || oldProp.setMethod != null)) { set = false; } // Add old get/set methods to current prop // Handles issues with private classes if (oldProp.getMethod != null) { prop.getMethod = oldProp.getMethod; } if (oldProp.setMethod != null) { prop.setMethod = oldProp.setMethod; } } } // This is now handled in setFields which gets called after // setBeanProperties// else {// // Keep static fields around...// PyReflectedField field = (PyReflectedField)o;// if (Modifier.isStatic(field.field.getModifiers())) {// prop.field = field.field;// } else {// // If the field is not static (and thus subsumable)// // don't overwrite// return;// }// } } if (set) __dict__.__setitem__(name, prop); } /* Adds a bean event to this class */ void addEvent(String name, Class eventClass, Method addMethod, Method[] meths) { String eventName = eventClass.getName(); for (int i=0; i<meths.length; i++) { PyBeanEventProperty prop; prop = new PyBeanEventProperty(name, eventClass, addMethod, meths[i]); __dict__.__setitem__(prop.__name__, prop); } PyBeanEvent event = new PyBeanEvent(name, eventClass, addMethod); __dict__.__setitem__(event.__name__, event); } /* A reimplementation of java.beans.Introspector.decapitalize. This is needed due to bugs in Netscape Navigator */ private static String decapitalize(String s) { //return java.beans.Introspector.decapitalize(s); if (s.length() == 0) return s; char c0 = s.charAt(0); if (Character.isUpperCase(c0)) { if (s.length() > 1 && Character.isUpperCase(s.charAt(1))) return s; char[] cs = s.toCharArray(); cs[0] = Character.toLowerCase(c0); return new String(cs); } else { return s; } } // This method is a workaround for Netscape's stupid security bug! private void setBeanInfoCustom(Class c, Method[] meths) { //try { int i; int n = meths.length; for (i=0; i<n; i++) { Method method = meths[i]; if (ignoreMethod(method)) continue; if (method.getDeclaringClass() != c || Modifier.isStatic(method.getModifiers())) { continue; } String name = method.getName(); Method getter = null; Method setter = null; Class[] args = method.getParameterTypes(); Class ret = method.getReturnType(); Class myType=null; String pname=""; if (name.startsWith("get")) { if (args.length != 0) continue; getter = method; pname = decapitalize(name.substring(3)); myType = ret; //System.out.println("get: "+name+", "+myType); } else { if (name.startsWith("is")) { if (args.length != 0 || ret != Boolean.TYPE) continue; getter = method; pname = decapitalize(name.substring(2)); myType = ret; } else { if (name.startsWith("set")) { if (args.length != 1) continue; setter = method; pname = decapitalize(name.substring(3)); myType = args[0]; //System.out.println("set: "+name+", "+myType); } else { continue; } } } PyObject o = __dict__.__finditem__(new PyString(pname)); PyBeanProperty prop; if (o == null || !(o instanceof PyBeanProperty) ) { addProperty(pname, myType, getter, setter); } else { prop = (PyBeanProperty)o; if (prop.myType != myType) { if (getter != null) { addProperty(pname, myType, getter, setter); } } else { //System.out.println("p: "+prop.myType+", "+myType); if (getter != null) prop.getMethod = getter; if (setter != null && (ret == Void.TYPE || prop.setMethod==null)) prop.setMethod = setter; } } } for (i=0; i<n; i++) { Method method = meths[i]; if (method.getDeclaringClass() != c || Modifier.isStatic(method.getModifiers())) { continue; } String mname = method.getName(); if (!(mname.startsWith("add") || mname.startsWith("set")) || !mname.endsWith("Listener")) { continue; } Class[] args = method.getParameterTypes(); Class ret = method.getReturnType(); String pname=""; if (args.length != 1 || ret != Void.TYPE) continue; Class eClass = args[0]; // This test and call of getClassLoader() function as a // workaround for a bug in MRJ2.2.4. The bug occured when // this program was compiled with jythonc: // import java // print dir(java.awt.Button) // The 'actionPerformed' attributed would be missing. if (eClass.getInterfaces().length > 0) eClass.getInterfaces()[0].getClassLoader(); // And of Mac workaround if (!(java.util.EventListener.class.isAssignableFrom(eClass))) continue; String name = eClass.getName(); int idot = name.lastIndexOf('.'); if (idot != -1) name = decapitalize(name.substring(idot+1)); addEvent(name, eClass, method, eClass.getMethods()); } /*} catch (Throwable t) { System.err.println("Custom Bean error: "+t); t.printStackTrace(); }*/ } /** * Return the list of all accessible constructors for a class. This * will only the public constructors unless * Options.respectJavaAccessibility is false, in which case all * constructors are returned. Note that constructors are not * inherited like methods or fields. */ private static Constructor[] getAccessibleConstructors(Class c) { if (!JavaAccessibility.accessIsMutable()) // returns just the public fields return c.getConstructors(); // return all constructors Constructor[] declared = c.getDeclaredConstructors(); for (int i=0; i < declared.length; i++) { // TBD: this is a permanent change. Should we provide a way to // restore the original accessibility flag? JavaAccessibility.setAccessible(declared[i], true); } return declared; } private boolean ignoreConstructor(Constructor method) { Class[] exceptions = method.getExceptionTypes(); for (int j = 0; j < exceptions.length; j++) { if (exceptions[j] == PyIgnoreMethodTag.class) { return true; } } return false; } private void setConstructors(Class c) { if (Modifier.isInterface(c.getModifiers())) { __init__ = null; } else { Constructor[] constructors = getAccessibleConstructors(c); for (int i = 0; i < constructors.length; i++) { if (ignoreConstructor(constructors[i])) { continue; } if (__init__ == null) { __init__ = new PyReflectedConstructor(constructors[i]); } else { __init__.addConstructor(constructors[i]); } } if (__init__ != null) { __dict__.__setitem__("__init__", __init__); } } } private boolean constructorsInitialized=false; synchronized void initConstructors() { if (constructorsInitialized) return; initialize(); setConstructors(proxyClass); constructorsInitialized = true; } /* If the new name conflicts with a Python keyword, add an '_' */ private static java.util.Hashtable keywords=null; private static String unmangleKeyword(String name) { if (keywords == null) { keywords = new java.util.Hashtable(); String[] words = new String[] {"or", "and", "not", "is", "in", "lambda", "if", "else", "elif", "while", "for", "try", "except", "def", "class", "finally", "print", "pass", "break", "continue", "return", "import", "from", "del", "raise", "global", "exec", "assert"}; for (int i=0; i<words.length; i++) { keywords.put(words[i]+"_", words[i].intern()); } } return (String)keywords.get(name); } PyObject[] lookupGivingClass(String name, boolean stop_at_java) { if (stop_at_java) return new PyObject[] {null, null}; if (!initialized) initialize(); if (name == "__init__") { initConstructors(); return new PyObject[] {__init__, null}; } // For backwards compatibilty, support keyword_ as a substitute for // keyword. An improved parser makes this no longer necessary. if (Options.deprecatedKeywordMangling && name.endsWith("_")) { String newName = unmangleKeyword(name); if (newName != null) name = newName; } return super.lookupGivingClass(name, stop_at_java); } public PyObject __dir__() { initialize(); if (__dict__ instanceof PyStringMap) { return ((PyStringMap)__dict__).keys(); } else { return __dict__.invoke("keys"); } } private PyStringMap missingAttributes = null; public PyObject __findattr__(String name) { if (name == "__dict__") { if (__dict__ == null) initialize(); return __dict__; } if (name == "__name__") return new PyString(__name__); if (name == "__bases__") { if (__bases__ == null) initialize(); return __bases__; } if (name == "__init__") { initConstructors(); if (__init__ == null) return super.lookupGivingClass(name, false)[0]; return __init__; } PyObject result = lookup(name, false); if (result != null) return result._doget(null); // A cache of missing attributes to short-circuit later tests if (missingAttributes != null && missingAttributes.__finditem__(name) != null) { return null; } // These two tests can be expensive, see above for short-circuiting result = findClassAttr(name); if (result != null) return result; result = findInnerClass(name); if (result != null) return result; // Add this attribute to missing attributes cache if (missingAttributes == null) { missingAttributes = new PyStringMap(); } missingAttributes.__setitem__(name, this); return null; } private PyJavaInstance classInstance; private PyObject findClassAttr(String name) { if (classInstance == null) { classInstance = new PyJavaInstance(proxyClass); } PyObject result = classInstance.__findattr__(name); return result; //if (result == null) return null; //__dict__.__setitem__(name, result); //return result; } private PyObject findInnerClass(String name) { Class p = getProxyClass(); Class innerClass = Py.relFindClass(p, p.getName()+"$"+name); if (innerClass == null) return null; PyJavaClass jinner = lookup(innerClass); __dict__.__setitem__(name, jinner); return jinner; } public void __setattr__(String name, PyObject value) { PyObject field = lookup(name, false); if (field != null) { if (field._doset(null, value)) return; } __dict__.__setitem__(name, value); } public void __delattr__(String name) { PyObject field = lookup(name, false); if (field == null) { throw Py.NameError("attribute not found: "+name); } if (!field._dodel(null)) { __dict__.__delitem__(name); //throw Py.TypeError("attr not deletable: "+name); } } public PyObject __call__(PyObject[] args, String[] keywords) { if (!constructorsInitialized) initConstructors(); PyInstance inst = new PyJavaInstance(this); inst.__init__(args, keywords); if (proxyClass != null && PyObject.class.isAssignableFrom(proxyClass)) { // It would be better if we didn't have to create a PyInstance // in the first place. ((PyObject)inst.javaProxy).__class__ = this; return (PyObject)inst.javaProxy; } return inst; } public Object __tojava__(Class c) { initialize(); return super.__tojava__(c); } public String toString() { return "<jclass "+__name__+" "+Py.idstr(this)+">"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -