简体   繁体   中英

is there a way to write a Java method like toString in Javascript which if called on a method prints the source code of the method?

Sorry if this is a trivial question, so if this has already been asked, please direct me to the question.

I know that the tostring method in javascript, if called on a function will print the source code (more about it: link ). Is it possible to do the same thing in Java?

So if I have the following identity function definition:

public class class1 {

    int f1(int x){
        return  x;
    }
}

And the following main function:

class Main {
    public static void main(String args[]) {

        class1 c1 = new class1();

        ???? 

    }
}

Is there anything I can put in place of '????' that would print something like

int f1(int x){
    return  x;
}

Disclaimer: I'm not an expert at Java, or any programming language for that matter. However, I do know how to find information online.

This concept does not seem very doable within Java. To start:

JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.

Basically, JavaScript has the advantage of reading directly from the file with the source code, and executing it on the fly. Compiled languages such as Java won't have that sort of functionality built in by default for many reasons, including security. An application should be able to run without allowing hackers access to its source code as much as possible.

There have been attempts at doing various forms of what you're interested in, but the two easiest methods seem to be

  1. Printing the original .java file line by line
  2. Storing a string reference to the entire code or the method(s) required.

It also seems possible to print the method name, but not the body.

Aside from that, the only thing you might be able to get from a compiled, running java program, is bytecode, which would require a decompiler to have any hope of understanding the source behind it.

You can continue reading up on it through a few of the links here:

How do I print the method body reflectively?

Save a method into a variable, java 8

View a method's implementation (method body) using Java Reflection

Probably yes, but not a trivial one with a ready command. JavaScript is an interpreted language where the executing environment has access to the source code in its original form. That is how you can inspect it in a browser console and see the same variable names as are in the source code.

While the compiled/interpreted distinction is fuzzy for Java , it is definitely modified before execution. The bytecode used by Java's Just-in-Time compilation may be more readable than a fully compiled binary file, it is not the source. If the running program does not have access to the source code, it is less able to output it. A debugger running in an IDE can reference problems in the source; otherwise, you are limited to debugging the bytecodes .

This is why Keno Clayton suggested the question on Quine programs, which are meant to reproduce themselves. This answer outputs the source code by hard-coding it as a class attribute. You could take a similar approach with a pre-compilation script that went through all the methods and made strings out of their sources, but the result would be bulky and potentially sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM