I want to write a Groovy script, which utilizes @TypedChecked and @CompileStatic as much as possible. My aim is to convert the groovy file to an almost identical class file, which utilizes no dynamic logic at runtime and executes just as fast as a .class file:
import org.openhab.core.automation.*
import org.openhab.core.automation.util.TriggerBuilder
import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule
import org.openhab.core.config.core.Configuration
import org.slf4j.LoggerFactory
scriptExtension.importPreset("RuleSupport")
@groovy.transform.TypeChecked
@groovy.transform.CompileStatic
class m {
org.slf4j.Logger log = LoggerFactory.getLogger("org.openhab.core.model.script.rele1")
m(org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager automationManager) {
SimpleRule s1 = new SimpleRule() {
String name = "B2"
@Override
Object execute(Action module, Map<String, ?> inputs) {
log.info("ABC")
}
List<Trigger> triggers = [
TriggerBuilder.create().withId("trig2a").withTypeUID("core.ItemStateChangeTrigger")
.withConfiguration(new Configuration([itemName: "inv1_Inverter_mode"])).build()
]
}
automationManager.addRule(s1) // Problematic Line
}
}
new m(automationManager)
Why does the Problematic Line above produce:
2024-09-01 17:52:12.115 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script ‘/etc/openhab/automation/jsr223/m3.groovy’: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during canonicalization: java.lang.NoClassDefFoundError: org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleHandlerFactoryjava.lang.RuntimeException: java.lang.NoClassDefFoundError: org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCusto
mModuleHandlerFactory
at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:972)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:692)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:666)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:373)
at groovy.lang.GroovyClassLoader.lambda$parseClass$2(GroovyClassLoader.java:316)
at org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163)
at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:314)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:350)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:137)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at org.openhab.core.automation.module.script.internal.ScriptEngineManagerImpl.loadScript(ScriptEngineManagerImpl.java:165)
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.createAndLoad(AbstractScriptFileWatcher.java:336)
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.lambda$13(AbstractScriptFileWatcher.java:310)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.NoClassDefFoundError: org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleHandlerFactory
at org.codehaus.groovy.ast.decompiled.AsmReferenceResolver.resolveClass(AsmReferenceResolver.java:46)
at org.codehaus.groovy.ast.decompiled.AsmReferenceResolver.resolveNonArrayType(AsmReferenceResolver.java:79)
at org.codehaus.groovy.ast.decompiled.AsmReferenceResolver.resolveType(AsmReferenceResolver.java:70)
at org.codehaus.groovy.ast.decompiled.MemberSignatureParser.resolve(MemberSignatureParser.java:172)
at org.codehaus.groovy.ast.decompiled.MemberSignatureParser.createMethodNode(MemberSignatureParser.java:64)
at org.codehaus.groovy.ast.decompiled.DecompiledClassNode.lambda$createConstructor$2(DecompiledClassNode.java:277)
at org.codehaus.groovy.ast.decompiled.DecompiledClassNode.createConstructor(DecompiledClassNode.java:283)
at org.codehaus.groovy.ast.decompiled.DecompiledClassNode.lazyInitMembers(DecompiledClassNode.java:238)
at org.codehaus.groovy.ast.decompiled.DecompiledClassNode.getDeclaredMethods(DecompiledClassNode.java:212)
at org.codehaus.groovy.ast.ClassNode.getDeclaredMethods(ClassNode.java:882)
at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:897)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethodsWithGenerated(StaticTypeCheckingVisitor.java:4785)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethod(StaticTypeCheckingVisitor.java:4884)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethodCallExpression(StaticTypeCheckingVisitor.java:3455)
at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitMethodCallExpression(StaticCompilationVisitor.java:422)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:77)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:117)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:204)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitExpressionStatement(StaticTypeCheckingVisitor.java:2160)
at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:41)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:86)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:168)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBlockStatement(StaticTypeCheckingVisitor.java:3956)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:72)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:142)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:115)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorOrMethod(StaticTypeCheckingVisitor.java:2595)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructor(ClassCodeVisitorSupport.java:105)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructor(StaticTypeCheckingVisitor.java:2544)
at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitConstructorOrMethod(StaticCompilationVisitor.java:222)
at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitConstructor(StaticCompilationVisitor.java:247)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1121)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitClass(StaticTypeCheckingVisitor.java:446)
at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitClass(StaticCompilationVisitor.java:198)
at org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:68)
at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:155)
at org.codehaus.groovy.transform.ASTTransformationVisitor.lambda$addPhaseOperations$2(ASTTransformationVisitor.java:237)
at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:938)
… 23 more1 error