Beanshell翻译9

上一篇 / 下一篇  2008-09-02 20:20:12 / 个人分类:Beanshell

Beanshell翻译9

http://user.qzone.qq.com/281696143/blog/1217928367  Ronger
1.Interface Types and Casting 接口和类型转换
It is legal,
but not usually necessary to perform an explicit cast of a BeanShell scrīpted object to an interface
type. For example:
这是合法的,但是通常不需要,将Beanshell脚本对象执行一个显式的转换变成一个接口类型,例如:
actionPerformed( event ) {
print( event );
}
button.addActionListener(
(ActionListener)this ); // added cast 增加转换
In the above,
the cast to ActionListener would have been done automatically by BeanShell
when it tried to match the 'this' type argument
to the signature of the addActionListener() method.
在上面的例子中,当它尝试将'this'类型的参数和addActionListener()方法相匹配时,
转换为ActionListener将被Beanshell自动完成。
Doing the cast explicitly has the same effect,
but takes a different route internally.
With the cast,
BeanShell creates the necessary adapter that implements the ActionListener interface first,
at the time of the cast, and then later finds that the method is a perfect match.
进行显式的转换也有同样的效果,但是在内部使用了不同的方法.
转换时,在转换的过程中,
Beanshell创建需要的实现ActionListener接口的适配器,后来可以找到这个匹配的方法。
What's the difference? Well,
there are times where performing an explicit cast to control
when the type is created may be important.
Specifically, when you are passing references out of your scrīpt,
to Java classes that don't immediately use them as their intended type.
In our earlier discussion we said that automatic casting happens "within your BeanShell scrīpts".
And in our examples so far BeanShell has always had the
opportunity to arrange for the scrīpted object to become the correct type,
before passing it on.
But it is possible for you to pass a 'this' reference to a method that,
for example, takes the type 'Object',
in which case BeanShell would have no way of knowning what it was destined for later.
You might do this, for example,
if you were placing your scrīpted objects into a collection (Map or List) of some kind.
In that case,
you can control the process by performing an explicit cast to the desired type
before the reference leaves your scrīpt.
区别是什么呢?有时当类型被创建,执行一个显式的转换控制是重要的.
特别的是,当你将你的引用从脚本中传递到Java类中时,不要马上当做打算好的类型使用他们。
在我们前面的讨论中,我们说自动的转型发生在"和你的Beanshell脚本在一起"。
在通过它之前,安排脚本对象变成正确类型的时机。
但是对你来说它可能将一个'this'引用赋给一个方法,例如,取到类型'Object',
在一个Beanshell也没有办法知道后来它的目的是什么的情况中。
你可以这样做,例如,如果你将你的脚本对象放到一个某种类型的集合中(Map或者List).
在上面这个情形中,在这个引用离开你的脚本之前,你可以通过执行显式的类型转换来控制这个过程。
Another case where you may have to perform a cast is
where you are using BeanShell in an embedded application
and returning a scrīpted object as the result of an eval() or a get() variable
from the Interpreter class.
There again is a case where BeanShell has no way of knowing the intended type within the scrīpt.
By performing an explicit cast you can create the type before the reference leaves your scrīpt.
另一个情况,你执行一个转换的地点是,
在一个内含的应用程序中,在哪儿使用Beanshell
并且从解释器中返回一个eval()或者get()的变量的脚本对象。
另外的一种情况,Beanshell没有方法知道在脚本中的对象在哪儿。
在引用离开脚本之前,通过执行一个显式的转换来创建类型。
We'll discuss embedded applications of BeanShell
in the "Embedding BeanShell" section a bit later,
along with the Interpreter getInterface() method,
which is another way of accomplishing this type of cast from outside a scrīpt.
稍后我们将在"Embedding Beanshell"章节中讨论Beanshell的嵌入式应用程序,
通过解释器中的getInterface()方法,这个方法是从脚本外完成类型转换的另一种方法。
2."Dummy" Adapters and Incomplete Interfaces
It is common in Java to see "dummy" adapters created for interfaces that have more than one method.
The job of a dummy adapter is to implement all of the methods of the interface with stubs
(empty bodies),
allowing the developer to extend the adapter and override just the methods of interest.
"Dummy"适配器和不完善的接口
为不止一个方法的接口创建"dummy"适配器,在Java中是很常见的。
一个哑适配器的工作是通过存根(内容为空的方法)创建接口中的所有方法,
允许开发者继承适配器并且仅仅重写感兴趣的方法。
We hinted in our earlier discussion that BeanShell could handle scrīpted interfaces
that implement only the subset of methods that are actually used and that is indeed the case.
You are free in BeanShell to scrīpt only the interface methods that you expect to be called.
The penalty for leaving out a method that is actually
invoked is a special run.time exception: java.lang.reflect.
UndeclaredThrowableException, which the caller will receive.
在早先的讨论中我们提到Beanshell可以处理仅仅实现方法子集的脚本接口,
这些方法是实际中用到的并且是在这个例子中的。
离开这个通常调用的方法就会调用运行时异常:java.lang.reflect.
UndeclaredThrowableException,调用者将会接受这个异常。
The UndeclaredThrowableException is an artifact of Java Proxy API that makes dynamic interfaces possible.
It says that an interface threw a checked exception type that was not prescribed by the method signature.
This is a situation that cannot normally happen in compiled Java.
So the Java reflection API handles it by wrapping the checked exception
in this special unchecked (RuntimeException) type in order to throw it.
You can get the underlying error using the exception's getCause() method,
which will, in this case, reveal the BeanShell EvalError exception,
reporting that the scrīpted method of the correct signature was not found.
UndeclaredThrowableException是一个Java代理API为实现动态接口的人造品。
它说明了,一个接口抛出一个没有在函数中标示出来的可检查异常类型。
这是一种不是通常发生在经过编译的Java的情况,所以
在发生普通的运行时异常时,为了可以抛出这个异常,Java反射API通过包装可检查异常来处理它。
你可以通过异常的getCause()方法(这个方法可以取到Beanshell的EvalError异常),取到潜在的错误。
3.The invoke() Meta.Method invoke() Meta.Method
BeanShell provides a very simple short.hand mechanism for scrīpting interfaces
with large numbers of methods.
You can implement the special method invoke( name, args ) in any scrīpted context.
The invoke() method will be called to handle the invocation of any method of the interface
that is not defined. For example:
Beanshell为脚本接口通过大量的方法提供了一个非常简单的short.hand机制。
在一些脚本环境中,你可以实现特殊的方法调用(名字,参数)。
invoke()方法被调用去处理没有定义的接口的方法。例如:
mouseHandler = new MouseListener() {
mousePressed( event ) {
print("mouse button pressed");
}
invoke( method, args ) {
print("Undefined method of MouseListener interface invoked:"
+ name +", with args: "+args
);
}
};
In the above example
we have neglected to implement four of the five methods of the MouseListener interface.
They will be handled by the invoke() method,
which will simply print the name of the method and its arguments.
However since mousePressed() is defined it will be called for the interface.
在上面的例子中,我们已经疏忽实现MouseListener接口中的五个方法中的四个方法。
他们将被invoke()方法处理,在这里这个方法仅仅简单的打印了方法的名字和参数。
但是由于mousePressed()方法定义了,它将被这个接口调用。
Here is a slightly more realistic example of where this comes in handy.
Let's use the invoke() method
to print the names of methods called via the ContentHandler interface of the Java SAX API,
while parsing an XML document.
这是一个轻微的实际的信手捻来的例子。
我们使用invoke()方法通过Java的SAX API的环境处理接口打印被调用的方法名。
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
factory = SAXParserFactory.newInstance();
saxParser = factory.newSAXParser();
parser = saxParser.getXMLReader();
parser.setContentHandler( this );
invoke( name, args ) {
print( name );
}
parser.parse( new InputSource(bsh.args[0]) );
By running this scrīpt with the XML file as an argument,
we can see which of the dozen or so methods of the SAX API
are being exercised by the structure of the document,
without having to write a stub for each of them.
通过当成一个参数一样来运行XML文件的脚本,
我们可以看到SAX API的一些方法可以通过文档的结构来演练。
没有为他们中的每一个写一个存根。
Tip:提示:
You can use the invoke( name, args ) meta.method directly
in your own scope or in the global scope as well,
in which case you can handle arbitrary "unknown" method invocations yourself,
perhaps to implement your own "virtual" commands.
Try typing this on the command line:
在你自己的作用域或者在全局的作用域中,你可以直接使用invoke(name,args)meta.method,
在某种情况下你可以处理任意未知的方法,或许是去实现你自己的"虚拟"命令。
尝试在你的命令行中定义这些:
invoke(name,args) { print("Command: "+name+" invoked!"); }
"run()" method in your bsh objects and make it the target of a Thread:
"run()"方法在你的bsh对象中并且使它成为一个线程的对象:
foo() {
run() {
// do work...
}
return this;
}
foo = foo();
// Start two threads on foo.run() 通过foo.run()开始两个线程
new Thread( foo ).start();
new Thread( foo ).start();
non.thread safe (e.g. access shared variables or objects) you can write multi.threaded scrīpts.
Note:
You can use the bg() "background" command to run an external scrīpt in a separate thread.
See bg().
没有线程的情况下很安全,你可以写多线程脚本。
注意:
你可以使用bg()"background"命令,在一个单独的线程中运行一个外部的脚本。
请看bg().
 


相关阅读:

TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 25672
  • 日志数: 25
  • 建立时间: 2008-08-27
  • 更新时间: 2008-09-02

RSS订阅

Open Toolbar