简体   繁体   English

在Rascal中引用Java文件

[英]Referencing a Java file in Rascal

I have trouble referencing a Java file from Rascal. 我在从Rascal引用Java文件时遇到麻烦。 I want to do an operation in multiple threads, and I don't think Rascal has support for that. 我想在多个线程中进行操作,我不认为Rascal对此提供支持。 To try using Java source code in Rascal I first tried to reimplement the trim function of the Rascal source code . 为了尝试在Rascal中使用Java源代码,我首先尝试重新实现Rascal源代码的trim功能。 I use the following rascal code: 我使用以下乱码:

module thread::threads

@javaClass{thread.JavaThread}
public java str trim(str s);

Simple enough. 很简单。 Now, I created the following Java file (based on the Rascal source file Prelude.java ): 现在,我创建了以下Java文件(基于Rascal源文件Prelude.java ):

package thread;

import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValueFactory;

public class JavaThread {
    protected final IValueFactory values;

    public JavaThread(IValueFactory values){
        super();
        this.values = values;
    }

    public IString trim(IString s) {
        return values.string(s.getValue().trim());
    }
}

Sadly, running this results in the following error: 可悲的是,运行此命令将导致以下错误:

rascal>import thread::threads;
|project://Software_Evolution/src/thread/threads.rsc|(42,58,<4,0>,<5,28>): No such Java method: thread.JavaThread.trim(io.usethesource.vallang.IString)
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UndeclaredJavaMethod/UndeclaredJavaMethod.html|

However, the Java file seems to be referenced correctly, as changing this name slightly would give the Cannot link method thread.JavaThreads because: class not found error. 但是,似乎正确引用了Java文件,因为稍微更改此名称将为Cannot link method thread.JavaThreads because: class not found错误。

How can I call the method trim in the JavaThread file? 如何在JavaThread文件中调用方法trim

What you did is right. 你做的是对的。 It just requires closing the terminal and reopening it, and importing the module again, unfortunately, to bind the compiled Java code to the Rascal module. 不幸的是,只需要关闭终端并重新打开它,然后再次导入模块,即可将编译后的Java代码绑定到Rascal模块。 Hope that helps! 希望有帮助!

The exception reports the following method signature it's looking for: 异常报告正在寻找的以下方法签名:

thread.JavaThread.runFunctionThreaded(io.usethesource.vallang.IInteger)

which would mean: 这意味着:

@javaClass{thread.JavaThread}
public java str runFunctionThreaded(int x);

So it could be that you are playing around with different classes and rascal files. 因此,可能是您在玩不同的类和无赖文件。 Since the class loader is a bit sensitive to reloading classes, it could be that you'll have to restart the REPL after a change in the Rascal class. 由于类加载器对重新加载类有点敏感,因此可能是您必须在更改Rascal类后必须重新启动REPL。

not your question, but still: it looks like you are trying to add multi-threading to Rascal, while this is a very good idea, Rascal has a global interpreter lock and that quickly stops multi-threading. 不是您的问题,但仍然是:看来您正在尝试向Rascal添加多线程,但这是一个很好的主意,Rascal具有全局解释器锁,并且可以快速停止多线程。 There are currently good reasons to this GIL but for the future we might be moving away from that design. 当前有此GIL的充分理由,但对于将来,我们可能会放弃该设计。 So for now, you'll have to keep it single threaded. 因此,现在,您必须使其保持单线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM