Debugging a compiled Groovy script in Eclipse - eclipse

I'm trying to debug a Groovy script in Eclipse from a JUnit test. The Groovy code is part of a larger Java application that runs in Tomcat. For various reasons our system is set up to use compiled JSR223 expressions. Here's the abbreviated code snippet:
GroovyScriptEngineImpl engine = new GroovyScriptEngineImpl();
Resource r =
new ClassPathResource("groovy/transformations/input/Foo.groovy");
String expression = IOUtils.toString(r.getInputStream());
CompiledScript script = engine.compile(expression);
String result = (String) script.eval(new SimpleBindings(bindings));
The test runs fine, but even though I have a breakpoint set in Foo.groovy, and the file is on the classpath, the breakpoint never gets hit when debugging. I'm guessing this doesn't work because there's no association between the expression in String format and the actual file that contains it. So is there a way of creating this association between the String and its corresponding file name? As mentioned, I need to use a CompiledScript. As a side note, I have been able to hit the breakpoint in the debugger with the same Groovy script when using this approach:
Resource r =
new ClassPathResource("groovy/transformations/input/Foo.groovy");
GroovyShell shell = new GroovyShell(new Binding(bindings));
String str = (String) shell.evaluate(r.getFile());
But of course, in this case the Groovy engine loads the file directly. Any hints as to how to get the first example to work are greatly appreciated. Thanks.

You are exactly right that this has to do with creating a class from a string. GroovyScriptEngineImpl likes to assign arbitrary names to the compiled script since it assumes everything comes from a string. The GroovyShell, however, generates the script name based off of the file that the script comes from, and this is the link that the debugger needs.
I'd perhaps recommend that you avoid using GroovyScriptEngineImpl and use GroovyShell.parse instead. And then, you can create a GroovyCompiledScript from the result of GroovyShell.parse and using a new GroovyScriptEngineImpl. Something like this:
File f = getScriptFile();
Script s = new GroovyShell().parse(f);
CompiledScript cs = new GroovyCompiledScript(new GroovyScriptEngineImpl(), s.getClass());
...
Note that I haven't tried this yet, but based on my experience, this should work.
If you are feeling really good-spirited, I'd raise a jira on the groovy issue tracker to ensure that you can pass in a proper name for scripts created using the GroovyScriptEngineImpl.

Related

There is a way to use lsp4e for calling language server methods directly?

I'm new to the lsp4e & lsp technologies and as far as I have seen the framework provides almost everything for working with eclipse. However there is a way to use this features at will? i.e I would like to use the LS to get all the functions on a file, I think this will be done with textDocument/documentSymbol but how can I get this using the lsp4e framework?
NOTE:
I checked for SymbolKind and seems it was not the one I was looking for however that input helped me finding a sample of DocumentSymbol
DocumentSymbolParams params = new DocumentSymbolParams(
new TextDocumentIdentifier(documentUri.toString()));
CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols =
languageServer.getTextDocumentService().documentSymbol(params);
I checked for SymbolKind and seems it was not the one I was looking for. However that input helped me finding a sample of DocumentSymbol
DocumentSymbolParams params = new DocumentSymbolParams(
new TextDocumentIdentifier(documentUri.toString()));
CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols =
languageServer.getTextDocumentService().documentSymbol(params);

Writing string to specific dir using chaquopy 4.0.0

I am trying a proof of concept here:
Using Chaquopy 4.0.0 (I use python 2.7.15), I am trying to write a string to file in a specific folder (getFilesDir()) using Python, then reading in via Android.
To check whether the file was written, I am checking for the file's length (see code below).
I am expecting to get any length latger than 0 (to verify that the file indeed has been written to the specific location), but I keep getting 0.
Any help would be greatly appreciated!!
main.py:
import os.path
save_path = "/data/user/0/$packageName/files/"
name_of_file = raw_input("test")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w")
toFile = raw_input("testAsWell")
file1.write(toFile)
file1.close()
OnCreate:
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(this));
File file = new File(getFilesDir(), "test.txt");
Log.e("TEST", String.valueOf(file.length()));
}```
It's not clear whether you've based your app on the console example, so I'll give an answer for both cases.
If you have based your app on the console example, then the code in onCreate will run before the code in main.py, and the file won't exist the first time you start the activity. It should exist the second time: if it still doesn't, try using the Android Studio file explorer to see what's in the files directory.
If you haven't based your app on the console example, then you'll need to execute main.py manually, like this:
Python.getInstance().getModule("main");
Also, without the input UI which the console example provides, you won't be able to read anything from stdin. So you'll need to do one of the following:
Base your app on the console example; or
Replace the raw_input calls with a hard-coded file name and content; or
Create a normal Android UI with a text box or something, and get input from the user that way.

VSCode - auto_prepend_file for code suggestions

I have been using VS code instead of eclipse for the last few weeks and find it much quicker, easier to use. However there is one thing I can’t seem to figure out.
My php app calls a prepend file which initiates a class called GlobalClass:
$gc = new GlobalClass();
When I type $gc-> in eclipse I get all the functions prompted. This doesn’t work within VS Code.
If I add use GlobalClass or add the $gc = new GlobalClass(); to the top of the file then it works. Is there any way to declare this within VS Code or to point the editor to the code within the prepend file?

Referencing External Files in JModelica

I have a Modelica file that references c code during simulation through an external library *.a file.
For example:
model CallAdd
input Real FirstInput(start=0);
input Real SecondInput(start=0);
output Real FMUOutput(start=0);
function CAdd
input Real x(start=0);
input Real y(start=0);
output Real z(start=0);
external "C" annotation(Library = "CAdd", LibraryDirectory = "modelica://CallAdd");
end CAdd;
equation
FMUOutput = CAdd(FirstInput,SecondInput);
annotation(uses(Modelica(version = "3.2.1")));
end CallAdd;
When opening the Modelica model in OpenModelica the required files appear to be automatically loaded because it simulates and gives appropriate results.
However, when I try to compile the Modelica file with JModelica-SDK-1.12 I receive an error that the library *.a file could not be found.
So my question is: What is the proper way to reference additional files when using compile_fmu in JModelica?
With no success, I've tried:
# Import the compiler function
from pymodelica import compile_fmu
model_name = "CallAdd"
mo_file = "CallAdd.mo"
# Compile the model and save the return argument, for use later if wanted
my_fmu = compile_fmu(model_name, mo_file, target="cs",compiler_options = {'extra_lib_dirs':'C:/ToFolderContainingLib/'})
The strange thing is that when I was using JModelica-1.17 (non-SDK) the file compiled fine but the results didn't make sense. I was recommended to try the SDK version to see if it fixed my errors in my previous post here.
Try positioning the external library in sub-folder named as the platform your currently on. So in your example, I'd position the library (libCAdd.a) in sub-folder named linux64, as I'm on a 64bit Linux machine and then run the code.
If is a small piece of C code, as a last alternative you could try to include the C file directly in the Modelica code:
external "C" annotation(Include="
// the entire C code here
");
Hopefully the JModelica people will give you a better answer soon.
You could try to ask this on their website also:
http://www.jmodelica.org/forum

problems with prefs.get and set ImageJ Macro

I'm trying to write a macro to save preferences and read them after closing and reopening ImageJ.
The saving works, but the macro isn't reading the file. Moreover when I try to use one of these two lines an error occurs that the variable "Prefs" is unknown.
int myNumber = Prefs.get("my.persistent.number", 0);
Prefs.savePreferences();
What am I doing wrong? please help me :-)
The ImageJ macro language itself does not support storing custom preferences. (Only the set of built-in options (accessible via Edit > Options in the menu) can be saved, restored and adjusted.) You need to resort to calling the Java class via call("ij.Prefs.get", "my.persistent.number", "0");.
The following ImageJ macro works in an up-to-date Fiji/ImageJ installation:
myNumber = call("ij.Prefs.get", "my.persistent.number", "0");
print(myNumber);
call("ij.Prefs.set", "my.persistent.number", 3);
In the first run, it prints 0; every following run will print 3; after restarting Fiji, it will print 3 again. In case it does not work for you even after updating to the newest version, please report a bug via Help > Report a bug, which will also submit essential information about your installation to the developers to help them fix the issue.
Using one of the many scripting languages however, you can access the ij.Prefs java class directly, as you are trying to do it. Just do not forget to import the class before using it. This is an example Javascript:
importClass(Packages.ij.Prefs);
myNumber = Prefs.get("my.persistent.number", 0);
Prefs.set("my.persistent.number", myNumber);
Hope that helps.