简体   繁体   中英

Referencing a Java file in Rascal

I have trouble referencing a Java file from Rascal. I want to do an operation in multiple threads, and I don't think Rascal has support for that. To try using Java source code in Rascal I first tried to reimplement the trim function of the Rascal source code . 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 ):

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.

How can I call the method trim in the JavaThread file?

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. 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.

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. There are currently good reasons to this GIL but for the future we might be moving away from that design. So for now, you'll have to keep it single threaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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