i created an string extension function like so: `f...
# announcements
a
i created an string extension function like so:
fun String.resourceContent() : String = javaClass.getResource(this).readText()
if i call
"/token.json".resourceContent()
in the test i get null if i call
javaClass.getResource("/token.json")
in the test it works why does this not work in an extension function?
c
pls use backticks to format your code, or post as a snippet
☝️ 1
m
I think that's because in your extension
javaClass
points to the
Class
object of
java.lang.String
(which does not have any resources you are trying to load), and in your tests it most probably points to the
Class
object of your test class. UPD: not
java.lang.String
, but
kotlin.String
😃
a
ur were right..its an issue of the class loader. working version is now
Untitled
well the best/correct version working also between different modules is the following:
inline fun String.resourceContent() : String = object{}.javaClass.getResource(this).readText()
note the use of inline and object{} to create a class whos loader is same as where the method gets called. the version before might not find the resource if called from a different module