Let me explain what I have done, what I think.
I made a 15451.groovy file which contains just the same lines as you presented above. And I executed it with groovy command in command-line.
kazurayam@N01-0370 MINGW64 ~/tmp
$ cat 15451.groovy
import java.text.SimpleDateFormat as SimpleDateFormat
System.err.println datefnWithTime()
static String datefnWithTime() {
String pattern = "dd-MMM-yyyy hh:mm:ss"
SimpleDateFormat format = new SimpleDateFormat(pattern)
return format.format(new Date())
}
kazurayam@N01-0370 MINGW64 ~/tmp
$ groovy 15451.groovy
14-Dec-2018 09:48:51
This worked fine. This proves your script is just legal, has no problem as a Groovy script.
I made a Test Case in a new Katalon Studio project, copied & pasted your script into it, ran it, and got a error:
12-14-2018 09:25:47 AM Test Cases/TC1
Elapsed time: 0.435s
Test Cases/TC1 FAILED.
Reason:
BUG! exception in phase 'semantic analysis' in source unit 'file:/C:/Users/qcq0264/katalon-workspace/KatalonForum15451/Scripts/TC1/Script1544747107739.groovy' ClassNode#getTypeClass for Script1544747107739 is called before the type class is set
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:252)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:181)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:292)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:185)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:274)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:183)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:101)
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:71)
at com.kms.katalon.core.ast.AstTestStepTransformation.getKeywordNameForStatement(AstTestStepTransformation.groovy:306)
at com.kms.katalon.core.ast.AstTestStepTransformation.visit(AstTestStepTransformation.groovy:254)
at com.kms.katalon.core.ast.AstTestStepTransformation.visit(AstTestStepTransformation.groovy:78)
at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:199)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:321)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:312)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:291)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:283)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:222)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1544747144293.run(TempTestCase1544747144293.groovy:22)
Among the lines I got interested in the line:
ClassNode#getTypeClass for Script1544747107739 is called before the type class is set
at com.kms.katalon.core.ast.AstTextValueUtil.getTextValue(AstTextValueUtil.java:252)
This line firmly tells you that the method getTextValue
of the class com.kms.katalon.core.ast.AstTextValueUtil
is calling the getTypeClass
method of the org.codehaus.groovy.ast.ClassNode
class.
You can read the api document here:
The document tells you as follows:
public Class getTypeClass()
Returns the concrete class this classnode relates to. However, this method is inherently unsafe as it may return null depending on the compile phase you are using. AST transformations should never use this method directly, but rather obtain a new class node using getPlainNodeReference()
.
Returns:
the class this classnode relates to. May return null.
“this method is inherently unsafe as it may return null depending on the compile phase you are using” : I think this is the reason why we got “BUG! exception in phase ‘semantic analysis’ in source unit” message.
I want to point out that the document sais: “AST transformations should never use this method directly”. However, the StackTrace above clearly indicates that com.kms.katalon.core.ast.AstTextValueUtil.getTextValue
is directly using the getTypeClass
method. Katalon should not do this. I think this should be regarded as a bug.
By the way, ‘AST’ means “Abstract Syntax Tree”. See http://glaforge.appspot.com/article/groovy-ast-transformations-tutorials for Groovy’s internal magic.