@Override public Object __tojava__(Class<?> c) { if (c == Boolean.TYPE || c == Boolean.class || c == Object.class ) { return Boolean.valueOf(getBooleanValue()); } if (c == Integer.TYPE || c == Number.class || c == Integer.class) { return Integer.valueOf(getValue()); } if (c == Byte.TYPE || c == Byte.class) { return Byte.valueOf((byte)(getValue())); } if (c == Short.TYPE || c == Short.class) { return Short.valueOf((short)(getValue())); } if (c == Long.TYPE || c == Long.class) { return Long.valueOf(getValue()); } if (c == Float.TYPE || c == Float.class) { return Float.valueOf(getValue()); } if (c == Double.TYPE || c == Double.class) { return Double.valueOf(getValue()); } return super.__tojava__(c); }
public Object __tojava__(Class c) { // If we are not being asked by the "default" conversion to java, then // we can provide this as the result, as long as it is a instance of the // specified class. Without this, derived.__tojava__(PyObject.class) // would broke. (And that's not pure speculation: PyReflectedFunction's // ReflectedArgs asks for things like that). if ((c!=Object.class)&&(c!=Serializable.class)&&(c.isInstance(this))) { return this; } // Otherwise, we call the derived __tojava__, if it exists: PyType self_type=getType(); PyObject impl=self_type.lookup("__tojava__"); if (impl!=null) { PyObject delegate=impl.__get__(this,self_type).__call__(Py.java2py(c)); if (delegate!=this) return delegate.__tojava__(Object.class); } return super.__tojava__(c); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testJavaOperator() { PythonOperator oper = new PythonOperator(); String setupScript = "import operator\n"; setupScript += "def square():\n"; setupScript += " return val*val\n\n"; oper.addSetupScript(setupScript); oper.setScript("square()"); oper.setPassThru(true); CollectorTestSink sink = new CollectorTestSink(); oper.result.setSink(sink); HashMap<String, Object> tuple = new HashMap<String, Object>(); tuple.put("val", new Integer(2)); oper.setup(null); oper.beginWindow(0); oper.inBindings.process(tuple); oper.endWindow(); Assert.assertEquals("number emitted tuples", 1, sink.collectedTuples.size()); for (Object o : sink.collectedTuples) { PyInteger val = (PyInteger)o; Assert.assertEquals("emitted should be 4", new Integer(4), (Integer)val.__tojava__(Integer.class)); } }