3.1.7 方法调用
例子:
foo.bar()
foo.bar
bar()
print "hello world\n"
print
语法:
[表达式 .] 标识符 [( 表达式 ... [* [表达式]] )]
若未指定被调用方法的对象,则调用 self 的方法。
方法名中除了通常的标识符以外,还可以添加“?”或“!”等后缀。通常在布尔型(返回真或伪)方法名后添加“?”,在比同名(无“!”)方法更具破坏性的方法名(例:tr 和 tr!)后添加“!”。
若最后一个参数前带“*”的话,将会先展开该参数的值,然后才传递。例如:
foo(1,*[2,3,4]) # 等同于 foo(1,2,3,4)
3.2 Java与JRuby的整合[size=x-large][/size]
当前版本的JRuby与Java的结合并不是非常完美,需要编写一些接口代码。笔者所使用的JRuby包是: jruby-complete-1.0.1.jar。
3.2.1 Java中的Ruby运行库环境
首先,Java程序中需要进行如下定义:
RubyInstanceConfig config = new RubyInstanceConfig();
Ruby runtime = Ruby.newInstance(config);
其中 RubyInstanceConfig config里可以配置JRuby运行环境的各种参数,比如
currentDirectory(当前目录),environment(环境变量)等。config作为Ruby.newInstance的参数来构造runtime ,而runtime 是JRuby的运行库环境,各种JRuby语句的执行都需要在runtime的上下文中执行 。
其次,需要在ParalleIRVirtualMachine进行如下定义:
IRubyObject rt = JavaUtil.convertJavaToRuby(runtime, this);
这句代码把ParalleIRVirtualMachine对象转换成IRubyObject对象,以便在这个对象上进行其他操作。
最后,我们需要把对象rt作为一个全局变量添加到runtime上去:
IAccessor d = new ValueAccessor(rt);
runtime.getGlobalVariables().define("$vm", d);
这步操作完成之后,runtime里就有了一个可供使用的全局对象$vm, 而$vm实际引用的是
IRubyObject对象rt, 由于rt是由ParalleIRVirtualMachine对象转化而来的,所以$vm实际引用的是ParalleIRVirtualMachine对象。
为了在$vm对象上添加方法,需要在rt.getMetaClass()返回的对象上调用defineMethod方法。defineMethod的第一个参数是函数名,第二个参数是一个org.jruby.runtime.callback.Callback对象。
例如,要在$vm上添加一个名为match的方法,可以这样写:
rt.getMetaClass().defineMethod("match", processMatch);
其中,第一个参数是函数名,第二个参数是一个org.jruby.runtime.callback.Callback对象。processMatch的定义是:
Callback processMatch = new IRSReflectionCallback(
ParalleIRVirtualMachine.class, "processMatchRuby",
new Class[] { RubyString.class }, false,// if restArg
false, // if staticArg
Arity.singleArgument(), true);// if fast
上述new IRSReflectionCallback构造函数的第一个参数ParalleIRVirtualMachine.class
是目标Java对象的类型,processMatchRuby是对象上的函数名,new Class[] { RubyString.class }是参数类型数组,参数Arity.singleArgument()指明有几个参数,single是指一个参数,其他参数一般情况下不需修改。
其中 IRSReflectionCallback类是一个自定义的类,它提供了Callback接口的实现。
Callback接口定义如下:
Callback.java:
public interface Callback {
public IRubyObject execute(IRubyObject recv, IRubyObject[] args, Block block);
public Arity getArity();
}
3.2.2 IRSReflectionCallback类实现
IRSReflectionCallback.java:
package com.irs..ruby;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.jruby.exceptions.JumpException;
import org.jruby.exceptions.MainExitException;
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.ThreadKill;
import org.jruby.javasupport.JavaObject;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callback.Callback;
/**
* A wrapper for <code>java.lang.reflect.Method</code> objects which implement Ruby methods.
*/
public class IRSReflectionCallback implements Callback {
private Method method;
private Class type;
private String methodName;
private Class[] argumentTypes;
private boolean isRestArgs;
private Arity arity;
private boolean isStaticMethod;
private boolean fast;
/*构造函数*/
public IRSReflectionCallback(Class type, String methodName, Class[] argumentTypes,
boolean isRestArgs, boolean isStaticMethod, Arity arity, boolean fast) {
this.type = type;
this.methodName = methodName;
this.argumentTypes = argumentTypes;
this.isRestArgs = isRestArgs;
this.isStaticMethod = isStaticMethod;
this.arity = arity;
this.fast = fast;
assert type != null;
assert methodName != null;
assert arity != null;
loadMethod(fast);
}
/*
按照是否为静态方法来传递函数参数
静态方法不需要传递this对象,成员方法需要传递this对象
*/
private void loadMethod(boolean fast) {
Class[] args;
if (isStaticMethod) {
Class[] types = new Class[argumentTypes.length + 1];
System.arraycopy(argumentTypes, 0, types, 1, argumentTypes.length);
types[0] = IRubyObject.class;
args = types;
} else {
args = argumentTypes;
}
// ENEBO: Perhaps slow but simple for now
if (!fast) {
Class[] types = new Class[args.length + 1];
System.arraycopy(args, 0, types, 0, args.length);
types[args.length] = Block.class;
args = types;
}
try {
List<Class> tmpArg=Arrays.asList(args);
//通过反射来从对象上获得方法,供Ruby引擎调用
method = type.getMethod(methodName, args);
} catch (NoSuchMethodException e) {
throw new RuntimeException("NoSuchMethodException: Cannot get method \"" + methodName
+ "\" in class \"" + type.getName() + "\" by Reflection.");
} catch (SecurityException e) {
throw new RuntimeException("SecurityException: Cannot get method \"" + methodName
+ "\" in class \"" + type.getName() + "\" by Reflection.");
}
}
/**
* Returns an object array that collects all rest arguments in its own object array which
* is then put into the last slot of the first object array. That is, assuming that this
* callback expects one required argument and any number of rest arguments, an input of
* <code>[1, 2, 3]</code> is transformed into <code>[1, [2, 3]]</code>.
*/
protected final Object[] packageRestArgumentsForReflection(final Object[] originalArgs) {
IRubyObject[] restArray = new IRubyObject[originalArgs.length - (argumentTypes.length - 1)];
Object[] result = new Object[argumentTypes.length];
try {
System.arraycopy(originalArgs, argumentTypes.length - 1, restArray, 0, originalArgs.length - (argumentTypes.length - 1));
} catch (ArrayIndexOutOfBoundsException e) {
assert false : e;
return null;
}
System.arraycopy(originalArgs, 0, result, 0, argumentTypes.length - 1);
result[argumentTypes.length - 1] = restArray;
return result;
}
/**
在IRubyObject recv对象上调用目标方法
* Invokes the Ruby method. Actually, this methods delegates to an internal version
* that may throw the usual Java reflection exceptions. Ruby exceptions are rethrown,
* other exceptions throw an AssertError and abort the execution of the Ruby program.
* They should never happen.
*/
/**
* Calls a wrapped Ruby method for the specified receiver with the specified arguments.
*/
public IRubyObject execute(IRubyObject recv, IRubyObject[] oargs, Block block) {
arity.checkArity(recv.getRuntime(), oargs);
Object[] methodArgs = oargs;
if (isRestArgs) {
methodArgs = packageRestArgumentsForReflection(methodArgs);
}
try {
JavaObject receiver = (JavaObject)recv;
Object target=receiver.getValue();
if (isStaticMethod) {
Object[] args = new Object[methodArgs.length + (fast ? 1 : 2)];
System.arraycopy(methodArgs, 0, args, 1, methodArgs.length);
args[0] = recv;
if (!fast) args[methodArgs.length + 1] = block;
receiver = null;
methodArgs = args;
} else {
Object[] args = new Object[methodArgs.length + (fast ? 0 : 1)];
System.arraycopy(methodArgs, 0, args, 0, methodArgs.length);
if (!fast) args[methodArgs.length] = block;
methodArgs = args;
}
return (IRubyObject) method.invoke(target, methodArgs);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RaiseException) {
throw (RaiseException) e.getTargetException();
} else if (e.getTargetException() instanceof JumpException) {
throw (JumpException) e.getTargetException();
} else if (e.getTargetException() instanceof ThreadKill) {
// allow it to bubble up
throw (ThreadKill) e.getTargetException();
} else if (e.getTargetException() instanceof Exception) {
if(e.getTargetException() instanceof MainExitException) {
throw (RuntimeException)e.getTargetException();
}
recv.getRuntime().getJavaSupport().handleNativeException(e.getTargetException());
return recv.getRuntime().getNil();
} else {
throw (Error) e.getTargetException();
}
} catch (IllegalAccessException e) {
StringBuffer message = new StringBuffer();
message.append(e.getMessage());
message.append(':');
message.append(" methodName=").append(methodName);
message.append(" recv=").append(recv.toString());
message.append(" type=").append(type.getName());
message.append(" methodArgs=[");
for (int i = 0; i < methodArgs.length; i++) {
message.append(methodArgs[i]);
message.append(' ');
}
message.append(']');
assert false : message.toString();
return null;
} catch (final IllegalArgumentException e) {
/* StringBuffer message = new StringBuffer();
message.append(e.getMessage());
message.append(':');
message.append(" methodName=").append(methodName);
message.append(" recv=").append(recv.toString());
message.append(" type=").append(type.getName());
message.append(" methodArgs=[");
for (int i = 0; i < methodArgs.length; i++) {
message.append(methodArgs[i]);
message.append(' ');
}
message.append(']');*/
assert false : e;
return null;
}
}
/**
* Returns the arity of the wrapped Ruby method.
*/
public Arity getArity() {
return arity;
}
}
3.2.3 在Java中编译执行Ruby脚本
public IRubyObject compileAndRun(String scriptTxt) {
// Node node
try {
//获得全局的runtime对象(Ruby运行库环境)
Ruby ruby = runtime;
Script script = null;
StringReader reader = new StringReader(scriptTxt);
Node parsedScriptNode = ruby.parse(reader, scriptTxt,
ruby.getCurrentContext().getCurrentScope(), 0);
// do the compile
StandardASMCompiler compiler = new StandardASMCompiler(
parsedScriptNode);
NodeCompilerFactory.getCompiler(parsedScriptNode).compile(
parsedScriptNode, compiler);
//加载类
Class scriptClass = compiler.loadClass(new JRubyClassLoader());
//生成script对象
script = (Script) scriptClass.newInstance();
ThreadContext tc = ruby.getCurrentContext();
//执行脚本
return script.run(tc, tc.getFrameSelf(), IRubyObject.NULL_ARRAY,
Block.NULL_BLOCK);
} catch (NotCompilableException nce) {
System.err.println("Error -- Not compileable: " + nce.getMessage());
return null;
} catch (JumpException je) {
if (je.getJumpType() == JumpException.JumpType.ReturnJump) {
return (IRubyObject) je.getValue();
} else {
throw je;
}
} catch (ClassNotFoundException e) {
System.err.println("Error -- Not compileable: " + e.getMessage());
return null;
} catch (InstantiationException e) {
System.err.println("Error -- Not compileable: " + e.getMessage());
return null;
} catch (IllegalAccessException e) {
System.err.println("Error -- Not compileable: " + e.getMessage());
return null;
}
}
3.2.4 Java内嵌Ruby方法总结
结合以上各个步骤,我们就可以在Java中定义方法,然后在Ruby中调用它了,下面总结一下整个过程。
//示例Java类,其中的helloword方法将在Ruby中被调用
Class JavaSampleCls{
public void helloworld(RubyString param){
System.out.println(“hello ”+param.toString());
}
}
//初始化Ruby运行环境
RubyInstanceConfig config = new RubyInstanceConfig();
Ruby runtime = Ruby.newInstance(config);
//初始化目标Java对象
JavaSampleCls javasample=new JavaSampleCls();
//把目标的普通Java对象转化为Ruby可识别的IRubyObject对象
IRubyObject targetObject = JavaUtil.convertJavaToRuby(runtime,javasample);
//定义callback对象: 类JavaSampleCls上的helloworld方法,一个String参数
Callback helloCallback = new IRSReflectionCallback(
JavaSampleCls.class, "helloworld",
new Class[] { RubyString.class }, false,// if restArg
false, // if staticArg
Arity.singleArgument(), true);// if fast
//把callback定义到targetObject上
targetObject.getMetaClass().defineMethod("helloworld", helloCallback);
IAccessor d = new ValueAccessor(targetObject);
//把targetObject绑定到Ruby运行库环境中
runtime.getGlobalVariables().define("$vm", d);
//声明Ruby脚本
String rubycode=”$vm.helloworld(“java”)”;
//编译执行
compileAndRun(rubycode);
分享到:
相关推荐
IRS系统使用手册.pdf
IRS2453DPBF中文资料
环境卫星IRS数据处理全部流程,很详细操作方便简单,便于使用
irs trading system, design the entity and need to connect to db
基于输入电压转换电路、正弦波发生电路和信号放大电路的原理设计了系统总体框架,重点探讨了该方法在硬件电路上的实现,并进行逆变电源性能测试。测试结果表明:该方法效率高,正弦波频率可通过简单的软件配置后进行调整...
3. 灵活的用户界面:Android 提供了丰富的用户界面控件和布局方式,可以实现漂亮、个性化的用户界面。 4. 响应式设计:Android 应用程序可以根据设备类型、屏幕尺寸等因素调整布局和显示方式,以适应不同的设备和...
IRS300-3系列6轴机器人用户手册-机械篇.pdf
电机驱动芯片IR2101是双通道、栅极驱动、高压高速功率驱动器,该器件采用了高度集成的电平转换技术,大大简化了逻辑电路对功率器件的控制要求,同时提高了驱动电路的可靠性。同时上管采用外部自举电容上电,使驱动...
CH340 USB转IRS232串口 TTL串口protel设计硬件原理图+PCB文件,Protel 99se 设计,包括原理图及PCB印制板图,可用Protel或 Altium Designer(AD)软件打开或修改,可作为你产品设计的参考。
IRS2453 自震荡全桥驱动芯片,耐压600伏
IRS辅助的SWIPT系统中基于能效优先的波束成形设计与优化 智能反射面辅助无线携能通信系统的波束赋型研究
IRS2334SPbF/MPbF:三相位芯片.pdf
199-一体化数字资源系统IRS架构及管理规范.pptx
IRS301-10系列6轴机器人用户手册-机械篇.pdf
超强PSP视频输出专用优化版RemoteJoy4iRS
基于LSTM实现IRS 的 WSN 性能分析附matlab代码.zip
资源分类:Python库 所属语言:Python 资源全名:irs-4.8.22.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
MC34063AD+IRS2184 DC_Motor电机驱动板ALTIUM设计硬件原理图+PCB文件,2层板设计,大小为65*36mm.包括原理图+PCB+封装库文件,可以做为你的学习设计参考。
教学互动反馈系统IRS.学生能够用手机与课堂教师进行交互,点名等等,是当今比较热门的研究。