0

I need to get 2 things logged from the current line of code:

  • the current class which is currently executed
  • the full path to the file of this class

In Kotlin, I can get class by

this::class

or

this::class.java

Then, I should be able to get path to the file of the class via

class.getProtectionDomain().getCodeSource().getLocation().getPath()

As is advised e. g. here. The problem is getProtectionDomain returns null for Android Activity:

    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7288)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7258)
        at android.view.View.access$4000(View.java:808)
        at android.view.View$PerformClick.run(View.java:28019)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7615)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7288) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7258) 
        at android.view.View.access$4000(View.java:808) 
        at android.view.View$PerformClick.run(View.java:28019) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7615) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 
     Caused by: java.lang.NullPointerException: this::class.java.protectionDomain must not be null

Meanwhile the class is of course not null because I can successfully see its name

log(this::class.java.name)

So how can i get class file path without getProtectionDomain?

Liker777
  • 2,156
  • 4
  • 18
  • 25
  • Did you try the solution included in [this](https://stackoverflow.com/a/17541055/13546426) answer? – Shubham Panchal Oct 14 '21 at 05:09
  • @ShubhamPanchal thank you for that. However that webpage refers to defining location for the known class. I need to define what class is currently program in and then defina path to it. I can do in Kotlin val path = File(this::class.java.getResource(classJava.name).path) following the reference from that webpage, however it gives null... – Liker777 Oct 15 '21 at 03:59

0 Answers0