It's possible in Java to access the source code of a method “reflectively”?

前端 未结 3 1314
萌比男神i
萌比男神i 2021-01-17 12:27

I\'m afraid that the answer is no, but maybe one of you surprises me.

Thanks.

Edit 1: I\'m aware that the question doesn\'t make much sens

相关标签:
3条回答
  • 2021-01-17 13:11

    As other's pointed out: no.

    You can access objects of a class, its methods etc. the way the JVM can. This is only possible because every class stores information about itself and its members when being compiled.

    If I had to guess, this happens in Object, the rootobject in the inheritance tree. You may decompile the class file using a decompiler and use that one for examination. But you cannot access the sourcecode like a String or anything similar.

    Think about it: If you have scala-code compiled for JVM, you cannot get the scala-code back either. And you cannot get java-code.

    Is there any special reason you want to do this? May there be any other way you could try to achieve your goal, whatever it might be?

    regards

    0 讨论(0)
  • 2021-01-17 13:15

    I don't think so. When the .java is compiled it becomes a .class; as far as I know Java doesn't have a built-in decompiler to turn that .class back into a .java. All that a runnable application knows about is .class files.

    0 讨论(0)
  • 2021-01-17 13:21

    No you can't. Its a little illogical as well.

    0 讨论(0)
提交回复
热议问题