Search code examples
javagroovygroovyshellgroovyclassloader

Execute groovy script in java project, why the operator “>” is converted to ">"?


This is my groovy script:

def map = ["curr_dept":codes.get("ICU_DEPT").value];
def list = getActLists(visit,"TRAN",map,[]);
boolean flag = false;
if(null!=list && list.size()>=1){
    return true;
}
return false;

This is my java code for executing script, in class "SingleDiseaseAction" method "testConfig()":

GroovyClassLoader loader = new GroovyClassLoader();
Map<String, Class<?>> totalMap = new HashMap<String, Class<?>>();
Class<?> newClazz = loader.parseClass(hsdqTargetConfig.getTarget_compute());
totalMap.put(hsdqTargetConfig.getTarget_compute(), newClazz);
Object obj = newClazz.newInstance();
Script script = (Script) obj;
Object[] paramters = new Object[] { script, getTestVisit(), hsdqRecord, map };
GroovyUtils.newInstance().invokeMethod("initScript", paramters);
value = script.run();

In above code, my groovy script can be getted by the code "hsdqTargetConfig.getTarget_compute()".

The "initScript" method is just for import some util class:

def initScript(Script dslScript,Object obj,Object record,Map codes){
    dslScript.setProperty('codes', codes);
    def rootName =  obj.getClass().getSimpleName();
    def recordName = record.getClass().getSimpleName();
    dslScript.setProperty(rootName.substring(0,1).toLowerCase() + rootName.substring(1), obj);
    dslScript.setProperty(recordName.substring(0,1).toLowerCase() + recordName.substring(1), record);
    dslScript.setProperty('HLRUtils', HLRUtils);
    dslScript.setProperty('DateUtils', DateUtils);
    dslScript.setProperty('EnumUtils', EnumUtils);
}

Thank you for read this. Sorry for my bad English.
Here is the error report :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1440555137250324574603.groovy: 4: expecting ')', found ';' @ line 4, column 32.
if(null!=list && list.size()&gt;=1){
                              ^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:162)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:912)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:574)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:202)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:212)
at com.goodwill.hdeap.web.console.record.singleDisease.SingleDiseaseAction.testConfig(SingleDiseaseAction.java:699)

My groovy jar is "groovy-all-2.1.6.jar".
I have check groovy's Syntax. Groovy should support the ">" operator.
But now I don't know why it is error.
Is it because of the convert operate from ">" to "&gt;" ? If sure, what I can do for this problem?


Solution

  • Wow, that is weird. It looks like an HTML encoding error, but that doesn't make sense.

    Try the safe dereference operator:

    def map = ["curr_dept":codes.get("ICU_DEPT").value];
    def list = getActLists(visit,"TRAN",map,[]);
    boolean flag = false;
    return list?.size() != 0