Search code examples
javarascal

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?


Solution

  • 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!