栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Groovy带参数的invokeMethod

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Groovy带参数的invokeMethod

是的…我

LicenseInfo.groovy
在一个文件夹中创建了这个时髦的脚本
./test/

package testpublic class LicenseInfo {  StringBuffer licenseInformation  public LicenseInfo() {    licenseInformation = new StringBuffer()  }  public void fetchLicenseInformation( Map<String,String> params, Map env ) {    List<String> licenseList = fetchLicenses( params, env )    println "List is $licenseList"  }  public List<String> fetchLicenses( Map<String,String> params, Map env ) {    [ 'a', 'b', 'c' ]  }}

在当前文件夹中

./
,我创建了这个groovy脚本
Test.groovy

// Make some params...def params = [ name:'tim', value:'text' ]// Fake an env Mapdef env = [ something:'whatever' ]// Load the class from the scriptdef liClass = new GroovyClassLoader().parseClass( new File( 'test/LicenseInfo.groovy' ) )// Run the methodliClass.newInstance().fetchLicenseInformation( params, env )

当我执行命令时

groovy Test.groovy

它输出:

List is [a, b, c]

更新后编辑

positive
您收到的错误是由于Groovy解析器的工作方式引起的。。。
+
加入字符串时,您不能将放在下一行的开头,
+
必须在上一行结尾(因为分号对于常规行的末尾,解析器无法知道您要添加到前一行)

这将起作用:

if (System.getProperty("os.name").contains("Win")) {  infacmdListLicensesCommand = env.get("INFA_HOME") + "/isp/bin/infacmd.bat ListLicenses -dn " +         params.get("dn") + " -un " + params.get("un") + " -pd " +         params.get("pd") + " -sdn " + params.get("sdn") + " -hp " +         params.get("dh") + ":" + params.get("dp")}else {  infacmdListLicensesCommand = env.get("INFA_HOME") + "/isp/bin/infacmd.sh ListLicenses -dn " +         params.get("dn") + " -un " + params.get("un") + " -pd " +         params.get("pd") + " -sdn " + params.get("sdn") + " -hp " +         params.get("dh") + ":" + params.get("dp")}

这将是执行相同操作的更 时髦的 方式:

boolean isWindows = System.getProperty("os.name").contains("Win")// Do it as a list of 3 items for formatting purposesinfacmdListLicensesCommand = [  "$env.INFA_HOME/isp/bin/infacmd.${isWindows?'bat':'sh'} ListLicenses"  "-dn $params.dn -un $params.un -pd $params.pd -sdn $params.sdn"  "-hp $params.dh:$params.dp" ].join( ' ' ) // then join them back togetherprintln infacmdListLicensesCommand // print it out to see it's the same as before


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号