@JRubyMethod(required = 1, optional = 1) public RubyObject new_instance(IRubyObject[] args, Block block) { final Ruby runtime = getRuntime(); final int last = Arity.checkArgumentCount(runtime, args, 1, 2) - 1; final RubyProc proc; // Is there a supplied proc arg or do we assume a block was supplied if (args[last] instanceof RubyProc) { proc = (RubyProc) args[last]; } else { proc = runtime.newProc(Block.Type.PROC, block); } final Object[] convertedArgs = convertArguments((RubyArray) args[0]); JavaProxyInvocationHandler handler = new ProcInvocationHandler(runtime, proc); try { return JavaObject.wrap(runtime, newInstance(convertedArgs, handler)); } catch (Exception e) { RaiseException ex = runtime.newArgumentError("Constructor invocation failed: " + e.getMessage()); ex.initCause(e); throw ex; } }
public Object invoke(Object proxy, JavaProxyMethod method, Object[] nargs) throws Throwable { final int length = nargs == null ? 0 : nargs.length; final IRubyObject[] rubyArgs = new IRubyObject[length + 2]; rubyArgs[0] = JavaObject.wrap(runtime, proxy); rubyArgs[1] = method; for (int i = 0; i < length; i++) { rubyArgs[i + 2] = JavaUtil.convertJavaToRuby(runtime, nargs[i]); } IRubyObject procResult = proc.call(runtime.getCurrentContext(), rubyArgs); return procResult.toJava(method.getReturnType()); }
public JavaObject newInstance(final IRubyObject self, Object[] args) throws RaiseException { final Ruby runtime = getRuntime(); JavaProxyInvocationHandler handler = new MethodInvocationHandler(runtime, self); try { return JavaObject.wrap(runtime, newInstance(args, handler)); } catch (Throwable t) { while (t.getCause() != null) t = t.getCause(); RaiseException ex = runtime.newArgumentError("Constructor invocation failed: " + t.getMessage()); ex.initCause(t); throw ex; } }
@JRubyMethod(rest = true) public RubyObject new_instance2(IRubyObject[] args, Block unusedBlock) { final Ruby runtime = getRuntime(); Arity.checkArgumentCount(runtime, args, 2, 2); final IRubyObject self = args[0]; final Object[] convertedArgs = convertArguments((RubyArray) args[1]); // constructor arguments JavaProxyInvocationHandler handler = new MethodInvocationHandler(runtime, self); try { return JavaObject.wrap(runtime, newInstance(convertedArgs, handler)); } catch (Exception e) { RaiseException ex = runtime.newArgumentError("Constructor invocation failed: " + e.getMessage()); ex.initCause(e); throw ex; } }
private void lazyJavaObject() { if (javaObject == null) { javaObject = JavaObject.wrap(getRuntime(), object); } }
@JRubyMethod(optional = 1, rest = true) public static IRubyObject execute_reader(IRubyObject recv, IRubyObject[] args) { Ruby runtime = recv.getRuntime(); IRubyObject connection_instance = api.getInstanceVariable(recv, "@connection"); IRubyObject wrapped_jdbc_connection = api.getInstanceVariable(connection_instance, "@connection"); if (wrapped_jdbc_connection.isNil()) { throw DataObjectsUtils.newDriverError( runtime, errorName, "This connection has already been closed."); } java.sql.Connection conn = getConnection(wrapped_jdbc_connection); RubyClass readerClass = Reader.createReaderClass(runtime, moduleName, errorName, driver); boolean inferTypes = false; int columnCount = 0; PreparedStatement sqlStatement = null; ResultSet resultSet = null; ResultSetMetaData metaData = null; // instantiate a new reader IRubyObject reader = readerClass.newInstance( runtime.getCurrentContext(), new IRubyObject[] {}, Block.NULL_BLOCK); // execute the query try { String sqlText = prepareSqlTextForPs(api.getInstanceVariable(recv, "@text").asJavaString(), recv, args); sqlStatement = conn.prepareStatement( sqlText, driver.supportsJdbcScrollableResultSets() ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); // sqlStatement.setMaxRows(); prepareStatementFromArgs(sqlStatement, recv, args); long startTime = System.currentTimeMillis(); resultSet = sqlStatement.executeQuery(); long endTime = System.currentTimeMillis(); debug(recv.getRuntime(), driver.toString(sqlStatement), Long.valueOf(endTime - startTime)); metaData = resultSet.getMetaData(); columnCount = metaData.getColumnCount(); // pass the response to the reader IRubyObject wrappedResultSet = Java.java_to_ruby(recv, JavaObject.wrap(recv.getRuntime(), resultSet), Block.NULL_BLOCK); reader.getInstanceVariables().setInstanceVariable("@reader", wrappedResultSet); wrappedResultSet.dataWrapStruct(resultSet); // handle each result // mark the reader as opened api.setInstanceVariable(reader, "@opened", runtime.newBoolean(true)); // TODO: if no response return nil api.setInstanceVariable(reader, "@position", runtime.newFixnum(0)); // save the field_count in reader api.setInstanceVariable(reader, "@field_count", runtime.newFixnum(columnCount)); // get the field types RubyArray field_names = runtime.newArray(); IRubyObject field_types = api.getInstanceVariable(recv, "@field_types"); // If no types are passed in, infer them if (field_types == null) { field_types = runtime.newArray(); inferTypes = true; } else { int fieldTypesCount = field_types.convertToArray().getLength(); if (field_types.isNil() || fieldTypesCount == 0) { field_types = runtime.newArray(); inferTypes = true; } else if (fieldTypesCount != columnCount) { // Wrong number of fields passed to set_types. Close the reader // and raise an error. api.callMethod(reader, "close"); throw runtime.newArgumentError( String.format( "Field-count mismatch. Expected %1$d fields, but the query yielded %2$d", fieldTypesCount, columnCount)); } } // for each field for (int i = 0; i < columnCount; i++) { RubyString field_name = runtime.newString(metaData.getColumnName(i + 1)); // infer the type if no types passed field_names.push_m(new IRubyObject[] {field_name}); if (inferTypes) { // TODO: do something } } // set the reader @field_names and @types (guessed or otherwise) api.setInstanceVariable(reader, "@fields", field_names); api.setInstanceVariable(reader, "@field_types", field_types); // keep the statement open // TODO why keep it open ??? // sqlStatement.close(); // sqlStatement = null; } catch (SQLException sqle) { // TODO: log sqle.printStackTrace(); throw newQueryError(runtime, sqle, sqlStatement); } finally { // if (sqlStatement != null) { // try { // sqlStatement.close(); // } catch (SQLException stsqlex) { // } // } } // return the reader return reader; }