Unable to compile Kotlin script

Hi Folks!

I’ve been doing some work with Alex T’s code for a vert.x wrapper in kotlin.  I seem to have a number of things working but when I try to execute a script I get the following error:

java.lang.IllegalStateException    at org.jetbrains.jet.lang.resolve.OverloadResolver.constructorsGrouped(OverloadResolver.java:111)    at org.jetbrains.jet.lang.resolve.OverloadResolver.checkOverloads(OverloadResolver.java:61)    at org.jetbrains.jet.lang.resolve.OverloadResolver.process(OverloadResolver.java:57)    at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.doProcess(TopDownAnalyzer.java:131)    at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.processClassOrObject(TopDownAnalyzer.java:201)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorForStatements.visitClass(ExpressionTypingVisitorForStatements.java:159)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorForStatements.visitClass(ExpressionTypingVisitorForStatements.java:50)    at org.jetbrains.jet.lang.psi.JetClass.accept(JetClass.java:68)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:125)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:100)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices.getBlockReturnedTypeWithWritableScope(ExpressionTypingServices.java:322)    at org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices.getBlockReturnedType(ExpressionTypingServices.java:206)    at org.jetbrains.jet.lang.resolve.ScriptBodyResolver.resolveScriptBodies(ScriptBodyResolver.java:80)    at org.jetbrains.jet.lang.resolve.BodyResolver.resolveBehaviorDeclarationBodies(BodyResolver.java:133)    at org.jetbrains.jet.lang.resolve.BodyResolver.resolveBodies(BodyResolver.java:141)    at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.doProcess(TopDownAnalyzer.java:134)    at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.analyzeFiles(TopDownAnalyzer.java:259)    at org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(AnalyzerFacadeForJVM.java:204)    at org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(AnalyzerFacadeForJVM.java:186)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$1.invoke(KotlinToJVMBytecodeCompiler.java:317)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$1.invoke(KotlinToJVMBytecodeCompiler.java:313)    at org.jetbrains.jet.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.java:212)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.java:312)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:292)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:273)    at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.java:246)    at org.vertx.kotlin.deploy.KotlinVerticleFactory.createVerticle(KotlinVerticleFactory.kt:65)    at org.vertx.java.deploy.impl.VerticleManager$10.run(VerticleManager.java:719)    at org.vertx.java.core.impl.Context$2.run(Context.java:122)    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processEventQueue(AbstractNioWorker.java:453)    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:330)    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)    at java.lang.Thread.run(Thread.java:722)

It appears to be my call to KotlinToJVMBytecodeCompile.compileScript that is failing.  Here is my class:

public class KotlinVerticleFactory() : VerticleFactory {

  private var mgr : VerticleManager? = null

  public override fun init(manager: VerticleManager?) {
  this.mgr = mgr
  }

  public override fun createVerticle(main: String?, parentCL: ClassLoader?): Verticle? {
  val messageRenderer: MessageRenderer = MessageRenderer.PLAIN
  val messageCollector: PrintingMessageCollector  = PrintingMessageCollector(System.err, messageRenderer, false)
  val rootDisposable: Disposable  = CompileEnvironmentUtil.createMockDisposable()!!
  val compilerConfiguration: CompilerConfiguration = CompilerConfiguration()
  val url = parentCL!!.getResource(main)
  val path: String? = url!!.getPath()
  val vertxParameter = AnalyzerScriptParameter(Name.identifier(“vertx”), JetTypeName.fromJavaClass(javaClass<Vertx>()))
  val containerParameter = AnalyzerScriptParameter(Name.identifier(“container”), JetTypeName.fromJavaClass(javaClass<Container>()))
  val verticleParameter = AnalyzerScriptParameter(Name.identifier(“verticle”), JetTypeName.fromJavaClass(javaClass<KotlinScriptVerticle>()))
  val scriptParameters: List<AnalyzerScriptParameter>? = Arrays.asList(vertxParameter, containerParameter, verticleParameter)?.toList()
  val scriptDefinitions: List<JetScriptDefinition>? = Arrays.asList(JetScriptDefinition(“.kts”, Collections.emptyList<AnalyzerScriptParameter>()), JetScriptDefinition(“.ktscript”, Collections.emptyList<AnalyzerScriptParameter>()))?.toList()

  &nbsp;&nbsp;compilerConfiguration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
  &nbsp;&nbsp;compilerConfiguration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(parentCL!!))
  &nbsp;&nbsp;compilerConfiguration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar())
  &nbsp;&nbsp;compilerConfiguration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, Collections.singletonList(

           PathUtil.getJdkAnnotationsPath())!!.toList()!!)
  compilerConfiguration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, path)
  compilerConfiguration.addAll(CommonConfigurationKeys.SCRIPT_DEFINITIONS_KEY, scriptDefinitions!!)
  compilerConfiguration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, scriptParameters)

  &nbsp;&nbsp;val environment: JetCoreEnvironment = JetCoreEnvironment(rootDisposable, compilerConfiguration)
  &nbsp;&nbsp;//val script = KotlinToJVMBytecodeCompiler.compileScript(parentCL, path, Arrays.asList(verxtParameter, containerParameter, verticleParameter), Arrays.asList(JetScriptDefinition(".kts", Collections.emptyList&lt;AnalyzerScriptParameter&gt;())))!!

  &nbsp;&nbsp;val script = KotlinToJVMBytecodeCompiler.compileScript(environment, parentCL)!!
  &nbsp;&nbsp;return KotlinScriptVerticle(script)

  }

  public override fun reportException(t: Throwable?) {
  t!!.printStackTrace()
  mgr?.getLogger()!!.error(“Exception in Kotlin verticle script”, t)
  }

  private fun getClasspath(loader: ClassLoader , files: LinkedList<File>  = LinkedList<File>()): jet.Collection<File> {
  val parent = loader.getParent()
  if(parent != null)
           getClasspath(parent, files);

  &nbsp;&nbsp;if(loader is URLClassLoader) {

           val urls = loader.getURLs()!!.toCollection()
           for (url in urls) {
           var urlFile = url?.getFile()!!

           if (urlFile.contains(“%”)) {
                   urlFile = url?.toURI()?.getPath()!!
           }

           val file: File = File(urlFile)
           val fileName: String = file.getName()!!
           if(file.exists() && (file.isDirectory() || fileName.endsWith(“.jar”))) {
                   files.add(file);
           }
           }
  }
  return files.toList();
  }
}

I had to do a bunch of work to get the types to match up (they were returning nullable types although the IDE thought they were non-nullable, or mutable when non-mutable was needed).  I also copied the work done to generate the classpath in the compiler because I thought that might be the issue to no avail.  

I considered filing a bug but was hoping someone knew a fix prior to that.

Thanks!

-Steve

Hi,

We’ll need some time to investigate this. Mind to report an issue to the tracker? Thanks

Done:

http://youtrack.jetbrains.com/issue/KT-3067