我在Groovy shell中看不到任何从内存中删除变量的方法......这可能吗?我该怎么做呢?我可以在Beanshell和Javascript(Rhino)中看到这样做的方法,但在Groovy中却看不到.
我正在尝试将groovy嵌入到大型Java应用程序中.
Java应用程序应该在启动时加载一些实用程序Groovy脚本.
然后应用程序应该多次运行其他脚本.还需要在GUI处输入一些代码并在用户请求时执行它.
我面临的问题是:
我正在加载启动脚本,如下所示:
GroovyShell gShell = new GroovyShell();
gShell.evaluate(new FileReader("scripts/autoload.groovy"));
Run Code Online (Sandbox Code Playgroud)
假设我的autoload.groovy包含:
def prnt(m) {
println("From Groovy: " + m);
}
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,当我想使用以下命令运行用户命令时:
gShell.evaluate("prnt 66");
Run Code Online (Sandbox Code Playgroud)
我收到错误:
groovy.lang.MissingMethodException: No signature of method: Script2.prnt() is applicable for argument types: (java.lang.Integer) values: [66]
我的用户脚本如何访问已加载的方法?
注意:我也试过"autoload.prnt 88",仍然得到错误.
我在我的Java类中使用GroovyClassLoader来解析某个(理想情况下很复杂的)groovy文件(将在下一步执行):
在MyClass.java中调用
final Class parsedClass = groovyClassLoader.parseClass(groovyFile);
Run Code Online (Sandbox Code Playgroud)
知道:
GroovyFile.groovy进口
import com.my.import.one.Import1DTO
import com.my.import.two.Import2DTO
import com.my.import.three.Import3DTO
import com.my.import.four.Import4DTO
import com.my.import.five.Import5DTO
Run Code Online (Sandbox Code Playgroud)
当调用parseClass方法时,此异常引发:
例外
unable to resolve class com.my.import.one.Import1DTO;
unable to resolve class com.my.import.two.Import2DTO;
unable to resolve class com.my.import.three.Import3DTO;
unable to resolve class com.my.import.four.Import4DTO;
unable to resolve class com.my.import.five.Import5DTO;
Run Code Online (Sandbox Code Playgroud)
在解析基类之前,我是否可以获得我期望的行为而不解析每个导入类?
谢谢!
我刚刚开始学习groovy,正在阅读“ Groovy in Action”。在这本书中,我遇到了一条声明,即声明变量还是强制类型为int或Integer都无关紧要.Groovy都使用引用类型(Integer)。
所以我试图为类型为int的变量赋空值
int a = null
Run Code Online (Sandbox Code Playgroud)
但这给了我例外
org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法将类为“ null”的对象“ null”转换为类“ int”。在Script1.run(Script1.groovy:2)上尝试使用“ java.lang.Integer”
然后我尝试将空值分配给Integer类型的变量
Integer a = null
Run Code Online (Sandbox Code Playgroud)
而且工作正常。
谁能帮助我了解groovy这种行为方式或背后的原因?
我正在使用Java + Groovy脚本。是否可以更改由Groovy类名称(Script1.groovy,Script777.groovy等)生成的值?如果出现异常,很难找到正确的脚本:/
Caused by: org.json.JSONException: JSONObject["value14"] not found.
at org.json.JSONObject.get(JSONObject.java:498)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetMethodMetaProperty.getProperty(MethodMetaProperty.java:59)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:61)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
at Script4.run(Script4.groovy:23)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:346)
... 13 more
Run Code Online (Sandbox Code Playgroud) 我需要在运行时检索 Groovy 2.3 中脚本文件的完整路径。实际上,我遇到了与此处描述的问题相同的问题:在运行时获取 groovy 源文件的路径。
我的脚本是否可以从 GroovyShell 执行。
我的脚本位于:
C:\users\myname\documents\scripts\myscript.groovy
Run Code Online (Sandbox Code Playgroud)
=> 我只是想在运行时检索此路径。
如果我使用被接受的解决方案:
println new File(".").absolutePath
Run Code Online (Sandbox Code Playgroud)
我得到的实际上是:
C:\groovy-2.3.7\.
Run Code Online (Sandbox Code Playgroud)
这是 groovy 主文件夹。那是不正确的。另一个建议的答案即:
URL scriptUrl = getClass().classLoader.resourceLoader.loadGroovySource(getClass().name)
Run Code Online (Sandbox Code Playgroud)
仅当我的脚本位于类路径中时,或者在 groovy 启动时使用 groovy-starter.conf 中的 load 指令加载它时才有效。否则返回空值。我可以使用这种方法,但它并不令人满意,因为它就像传递一个参数,这里的目标是用户无需修改或配置即可从任何地方执行脚本。
解决方案似乎使用为此目的创建的@SourceURI 注释。问题是我无法让它工作。我尝试执行如下所示的代码用法:SourceURI
@groovy.transform.SourceURI def sourceURI
assert sourceURI instanceof java.net.URI
path = sourceURI.toString()
println "path = $path"
Run Code Online (Sandbox Code Playgroud)
我得到的是(groovy 2.3.7)不是路径而是源代码:
path = data:,@groovy.transform.SourceURI%20def%20sourceURI%0A%0A%20assert%20sourceURI%20instanceof%20java.net.URI%0Apath%20=%20sourceURI.toString()%0Aprintln%20%22path%20=%20$path%22
Run Code Online (Sandbox Code Playgroud)
如何使用@SourceURI 注解来检索脚本文件的路径?
你好我是一个groovy的初学者我是如何检查如何检查给定的输入是否是一个数字我试过以下
def a= ' 12.571245ERROR'
if(a.isNan()==0)
{
println("not a number")
}
else
{
println("number")
}
Run Code Online (Sandbox Code Playgroud)
请帮助我如何在groovy中使用isNan.我google了很多但没有找到任何结果.提前致谢
我尝试从Java类调用我自己的groovy脚本函数,用户也可以使用标准表达式.
例如:
GroovyShell shell = new GroovyShell();
Script scrpt = shell.parse("C:/Users/Cagri/Desktop/MyCustomScript.groovy");
Binding binding = new Binding();
binding.setVariable("str1", "foo");
binding.setVariable("str2", "boo");
scrpt.setBinding(binding);
System.out.println(scrpt.evaluate("customConcat(str1, str2)")); //my custom method
System.out.println(scrpt.evaluate("str1.concat(str2)"));
Run Code Online (Sandbox Code Playgroud)
这是MyCustomScript.groovy
def area(def sf) {
Feature f = new Feature(sf);
f.getGeom().area;
}
def customConcat(def string1, def string2) {
string1.concat(string2)
}
Run Code Online (Sandbox Code Playgroud)
运行时,此行scrpt.evaluate("str1.concat(str2)")按预期工作,但scrpt.evaluate("customConcat(str1, str2)")会引发异常:
groovy.lang.MissingMethodException: No signature of method: Script1.customConcat() is applicable for argument types: (java.lang.String, java.lang.String) values: [foo, boo]
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
at …Run Code Online (Sandbox Code Playgroud) 我有一个 groovy 脚本,需要从外部 groovy 脚本解析一个类。我不确定如何传递参数。这是有效的:
我正在运行的 Groovy 脚本使用这一行来解析 external.groovy 中的外部类:
new GroovyShell().parse(new File('External.groovy'))
Run Code Online (Sandbox Code Playgroud)
这是 external.groovy 的样子:
class External {
public external() {
println "Hello"
}
}
Run Code Online (Sandbox Code Playgroud)
有用。
我遇到的问题,我找不到将参数传递给外部方法的方法。这是 external.groovy 的样子:
class External {
public external(String name) {
println name
}
}
Run Code Online (Sandbox Code Playgroud)
如何向正在运行的脚本添加参数:
new GroovyShell().parse(new File('external.groovy')) //need to include the 'Name' parameter to this
Run Code Online (Sandbox Code Playgroud) 我有一个JSON数组(地图列表)类似于:
def listOfMap = [[TESTCASE:1, METHOD:'CLICK', RESULT:'PASS'],
[TESTCASE:2, METHOD:'CLICK', RESULT:'FAIL'],
[TESTCASE:3, METHOD:'CLICK', RESULT:'FAIL'],
[TESTCASE:4, METHOD:'TYPETEXT', RESULT:'FAIL']]
Run Code Online (Sandbox Code Playgroud)
1)我想获取/过滤/返回包含键值对" METHOD:CLICK "和" RESULT:FAIL "的所有列表
My output should return 2 lists out of 4: [TESTCASE:2, METHOD:CLICK, RESULT:FAIL], [TESTCASE:3, METHOD:CLICK, RESULT:FAIL]
Run Code Online (Sandbox Code Playgroud)
2)我想获得包含键值对" METHOD:CLICK "和" RESULT:FAIL " 的列表计数
My output should be : 2
Run Code Online (Sandbox Code Playgroud)
3)从上面的地图列表中,我想获得关键字" METHOD "的所有唯一/不同值
My output should return unique values of the key method : CLICK, TYPETEXT
Run Code Online (Sandbox Code Playgroud)