java groovy 参数传递_java-带参数的Groovy的invokeMethod

我想从下面的类中调用groovy方法

package infa9

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.HashMap;

import com.ABC.csm.context.AppCtxProperties;

import com.ABC.csm.context.AppContext;

public class LicenseInfo

{

private StringBuffer licenseInformation;

public LicenseInfo() {

licenseInformation = new StringBuffer();

}

public StringBuffer getLicenseInformation(){

return licenseInformation;

}

public void fetchLicenseInformation(HashMap params,Map env)

{

ArrayList licenseList = fetchLicenses(params);

.

.

.

}

private ArrayList fetchLicenses(HashMap params,Map env)

{

ArrayListlicenseList = new ArrayList();

.

.

.

return licenseList;

}

}

这就是我想要做的

//getting user parameters

HashMap params = IntermediateResults.get("userparams")

//getting environment variables

Map env=AppContext.get(AppCtxProperties.environmentVariables)

Object[] arguments=new Object[2]

arguments.putAt("userparams", params)

arguments.putAt("env", env)

GroovyShell shell = new GroovyShell()

Script infa9LicenseScript = shell.parse("plugins.infa9.LicenseInfo")

infa9LicenseScript.invokeMethod(fetchLicenseInformation, arguments)

String lic=(String)infa9LicenseScript.invokeMethod(getLicenseInformation,null)

我是否将参数正确传递给fetchLicenseInformation?我需要传递HashMap< String,String> ,Map请帮助我使用参数调用groovy方法

错误:线程“主”中的异常groovy.lang.MissingPropertyException:无此类属性:类的用户参数:[Ljava.lang.Object;

更新

public List fetchLicenses( Map params, Map env ) {

//[ 'a', 'b', 'c' ]

ArrayListlicenseList = new ArrayList();

String infacmdListLicensesCommand = null;

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 " //this is line no 71, where exception is thrown

+ params.get("dn") + " -un " + params.get("un") + " -pd "

+ params.get("pd") + " -sdn " + params.get("sdn") + " -hp "

+ params.get("dh") + ":" + params.get("dp");}

try {

Process proc = Runtime.getRuntime().exec(infacmdListLicensesCommand);

InputStream stdin = proc.getInputStream();

InputStreamReader isr = new InputStreamReader(stdin);

BufferedReader br = new BufferedReader(isr);

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(line);

licenseList.add(line);

}

int exitVal = proc.waitFor();

System.out.println("Process exit value is: " + exitVal);

}catch (IOException io) {

io.printStackTrace();

}catch (InterruptedException ie) {

ie.printStackTrace();

} /* end catch */

return licenseList;

}

例外

线程“主”中的异常groovy.lang.MissingMethodException:没有方法签名:java.lang.String.positive()适用于参数类型:()值:[]

可能的解决方案:notify(),tokenize(),size()

在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)

在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unaryPlus(ScriptBytecodeAdapter.java:764)

位于infa9.LicenseInfo.fetchLicenses(Infa9LicensesUtil.groovy:71)

解决方法:

对…我在文件夹./test/内创建了这个groovy脚本LicenseInfo.groovy:

package test

public class LicenseInfo {

StringBuffer licenseInformation

public LicenseInfo() {

licenseInformation = new StringBuffer()

}

public void fetchLicenseInformation( Map params, Map env ) {

List licenseList = fetchLicenses( params, env )

println "List is $licenseList"

}

public List fetchLicenses( Map params, Map env ) {

[ 'a', 'b', 'c' ]

}

}

在当前文件夹./中,我创建了这个groovy脚本Test.groovy:

// Make some params...

def params = [ name:'tim', value:'text' ]

// Fake an env Map

def env = [ something:'whatever' ]

// Load the class from the script

def liClass = new GroovyClassLoader().parseClass( new File( 'test/LicenseInfo.groovy' ) )

// Run the method

liClass.newInstance().fetchLicenseInformation( params, env )

当我执行命令时

groovy Test.groovy

它输出:

List is [a, b, c]

更新后编辑

您得到的肯定错误是由于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 purposes

infacmdListLicensesCommand = [

"$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 together

println infacmdListLicensesCommand // print it out to see it's the same as before

标签:java,groovy

来源: https://codeday.me/bug/20191013/1910006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值