Issues Upgrading for m14 to 1.0b


#1

I have a gradle project that i'm using the kotlin plugin for. I hopefully have something configured wrong and it's an easy fix :D My build.gradle

group 'com.iamshb’

version '1.0-SNAPSHOT’

apply plugin: 'java’
apply plugin: 'kotlin’

buildscript {
  ext.kotlin_version = ‘1.0.0-beta-1038’
  repositories {
  mavenCentral()
  }
  dependencies {
  classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version
  }
}

sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  testCompile group: ‘junit’, name: ‘junit’, version: ‘4.11’
  compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version
}
sourceSets {
  main.java.srcDirs += ‘src/main/kotlin’
}

Things that do not work anymore:

this worked before but now “javaClass” is not found

  
fun getResource(prefix: String, name: String) : InputStream {
  return Any::class.javaClass.classLoader.getResourceAsStream(</span>prefix<span>name)

}

 
This is inside a forEach Unit. it is no longer found, and toRegex() is no longer found.
and println is longer found.
  
if (it.matches("\s*#.*$".toRegex())) return@forEach;
println(parse(it, readBuffer)); 


Screen Shot 2015-10-22 at 1.54.37 PM.png (35.6 KB)

#2

Does anything else from the Kotlin runtime work? Can you check if a simple hello world program would compile and run?

Please try to invalidate caches and restart (File -> Invalidate Caches / Restart…).

Also BTW “Any::class.javaClass” is probably not what you meant since this expression returns the runtime Java class of the expression “Any::class”, which happens to be KClass. More likely you meant “Any::class.java”, which will give you the Java class that is the runtime representation of Any (java.lang.Object).


#3

Thanks so much, invalidating and restarting worked. I had restarted and updated but not invalidated mhmm.

As for the Any::class.java i get a NPE when i use that but Any::class.javaClass does not.


#4

I'm glad it worked out. The NPE is strange though, could you please paste the stack trace for that? The simple "println(Any::class.java)" works for me.


#5

 

Code:
 
fun getResource(prefix: String, name: String) : InputStream {
  println(Any::class.java)
  println(Any::class.java.javaClass)
  println(Any::class.java.classLoader)
  println(Any::class.java.javaClass.classLoader)
  println(Any::class.javaClass)
  println(Any::class.javaClass.classLoader)
  return Any::class.java.javaClass.classLoader.getResourceAsStream(</span><span>prefix</span><span>name)
}

Output:
class java.lang.Object
class java.lang.Class
null
null
class kotlin.jvm.internal.ClassReference
sun.misc.Launcher$AppClassLoader@12a3a380
Exception in thread “main” java.lang.IllegalStateException: Any::class.java.javaClas…eAsStream("$prefix$name") must not be null
at com.iamshb.hexinterpret.MainKt.getResource(Main.kt:37)
at com.iamshb.hexinterpret.MainKt.parse(Main.kt:23)
at com.iamshb.hexinterpret.MainKt.main(Main.kt:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

i tried  every combination of the prints to get the resource as a stream and only the last one work.ed the stack trace were all pretty much the same


#6

I see. This happens because java.lang.Object (which represents Any at runtime) is a core JRE class and is loaded by the bootstrap class loader. Class#getClassLoader returns null in that case, as specified in the javadoc. I recommend you to take the class loader which loaded any of the classes in your application instead of the construct that you're using because it's rather confusing and may change unexpectedly when the app is run in another environment (where the Kotlin runtime would be loaded with a different class loader). So I would just use "SomeClass::class.java.classLoader.getResourceAsStream(...)" where SomeClass is any class in your code.


#7

*nod* easy enought. Originally when i started this project there were no classes. I wanted to use the MainKt class (that java sees) to load it however in the context of kotlin it didn't seem to be avaialbe (in m14 at least)

Normally in java I would use what ever class contaned the main(String…) method but this was not present in kotlin.

that said perhaps to support purely functional programming kotlin could provide a substiute in some way.


#8

You can just declare a local class Dummy and get its class loader :)


#9

ah yes very true, i often forget about local classes...