Static initializers


#1

Is there a way to do static initializers or something similar for things like System.loadLibrary? I tried the following

class Sample {

  class object {
  this() {
           System.loadLibrary(“libfoo”)
  }
  }

}

but that only got me an internal error from the compiler

22:52:36 Throwable
  java.lang.UnsupportedOperationException: failed to get descriptor for secondary constructor
  at org.jetbrains.jet.codegen.ImplementationBodyCodegen.generateSecondaryConstructor(ImplementationBodyCodegen.java:924)
  at org.jetbrains.jet.codegen.ImplementationBodyCodegen.generateDeclaration(ImplementationBodyCodegen.java:862)
  at org.jetbrains.jet.codegen.ClassBodyCodegen.generateClassBody(ClassBodyCodegen.java:80)
  at org.jetbrains.jet.codegen.ClassBodyCodegen.generate(ClassBodyCodegen.java:63)
  at org.jetbrains.jet.codegen.ClassCodegen.generateImplementation(ClassCodegen.java:78)
  at org.jetbrains.jet.codegen.ClassCodegen.generate(ClassCodegen.java:68)
  at org.jetbrains.jet.codegen.ClassCodegen.generate(ClassCodegen.java:63)
  at org.jetbrains.jet.codegen.NamespaceCodegen.generate(NamespaceCodegen.java:147)
  at org.jetbrains.jet.codegen.NamespaceCodegen.generate(NamespaceCodegen.java:109)
  at org.jetbrains.jet.codegen.GenerationState.generateNamespace(GenerationState.java:180)
  at org.jetbrains.jet.codegen.GenerationState.compileCorrectFiles(GenerationState.java:156)
  at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.java:279)
  at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:232)
  at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:216)
  at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModule(KotlinToJVMBytecodeCompiler.java:80)
  at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.java:101)
  at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:117)
  at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:49)
  at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:117)
  at org.jetbrains.jet.cli.jvm.K2JVMCompiler.exec(K2JVMCompiler.java:188)
  at org.jetbrains.jet.cli.jvm.K2JVMCompiler.exec(K2JVMCompiler.java:49)
  at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:47)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod(CompilerUtils.java:175)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.execInProcess(JetCompiler.java:248)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.access$000(JetCompiler.java:54)
  at org.jetbrains.jet.plugin.compiler.JetCompiler$1.invoke(JetCompiler.java:235)
  at org.jetbrains.jet.plugin.compiler.JetCompiler$1.invoke(JetCompiler.java:232)
  at org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode(CompilerUtils.java:314)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.runInProcess(JetCompiler.java:232)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.runCompiler(JetCompiler.java:139)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.doCompile(JetCompiler.java:127)
  at org.jetbrains.jet.plugin.compiler.JetCompiler.compile(JetCompiler.java:102)
  at com.intellij.compiler.impl.CompileDriver.compileSources(CompileDriver.java:1931)
  at com.intellij.compiler.impl.CompileDriver.translate(CompileDriver.java:1254)
  at com.intellij.compiler.impl.CompileDriver.doCompile(CompileDriver.java:986)
  at com.intellij.compiler.impl.CompileDriver.doCompile(CompileDriver.java:747)
  at com.intellij.compiler.impl.CompileDriver.access$1000(CompileDriver.java:104)
  at com.intellij.compiler.impl.CompileDriver$8.run(CompileDriver.java:665)
  at com.intellij.compiler.progress.CompilerTask.run(CompilerTask.java:155)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:469)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:218)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:169)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$8.run(ProgressManagerImpl.java:378)
  at com.intellij.openapi.application.impl.ApplicationImpl$6.run(ApplicationImpl.java:434)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
  at java.util.concurrent.FutureTask.run(FutureTask.java:166)
  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)
  at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:145)

#2

You can write:

class Sample {
    class object {
        {
            System.loadLibrary("libfoo")
        }
    }
}

or

class Sample {
    class object {
         // some fields
         val field = <expression>; //semicolon is required if <expression> ends with some call (function or constructor)
         {
            System.loadLibrary("libfoo")
        }
    }
}

Message was edited by: Zalim Bashorov "Fixed second sample."


#3

The word "static" in your last sample doesn't mean what might think it means. In fact, the compiler thinks it's an annotation (which does not exist). It will be red when we fix the diagnostics.


#4

Thanks, I fixed the sample.


#5

Unfortunately, it's still wrong: 1) The property must have an initializer 2) A semicolon is not requitred unless the initializer ends with an identifier or a call


#6

1) Oops. Again fixed. 2) I think, a semicolon is required if expression in the initializer ends with some call (function or constructor)