Anderson Green
Anderson Green

Reputation: 31800

Can a Java program access its own source code?

In Java, I'd like to find a way to allow a program to access its own source code, mainly for debugging and metaprogramming purposes (such as printing a method signature at runtime, or allowing a program to read its own comments, or allowing a Java class to print all methods of a certain type, or allowing a program to generate a new version of its own source code, etc).

Is there any way to allow a Java program to access a copy of its own source code, and read it line-by-line?

//this is the first line of the program
//this method is not implemented
public class inspectSourceCode(){
    public static String getLine(int lineNumber){
        //get the line of the program's own source code as a string,
        //this is not currently implemented
    }
    //this method is implemented
    public static void main(String[] args){
        System.out.println(getLine(0));
        //should print "//this is the first line of the program",
        //if the method getLine works correctly
    }
}

Upvotes: 1

Views: 1709

Answers (5)

Stephen C
Stephen C

Reputation: 718758

Can a Java program access its own source code?

In general no. The source code is typically not available on the execution platform.

In the sub-cases where the source code is available, then yes (of course) a program can read it using the standard Java I/O APIs. However, there are no standard APIs that are specific to the task of reading source code.

... mainly for debugging purposes (such as printing a method signature at runtime, or allowing a program to read its own comments, or allowing a Java class to print all methods of a certain type)

There is no technical reason why you could not do those things, but it strikes me that you would have a lot of work to do before such a tool got to the point of being useful. And, frankly, a typical Java IDE's source code debugger does pretty much all of these things already, so I don't really see the point of that effort.

Upvotes: 0

user439407
user439407

Reputation: 1756

You COULD theoretically use a decompiler library in your source code to potentially get access to the classes, but keep in mind due to optimization and/or obfuscation etc you might not be able to reliably do a 1-1 translation between bytecode and Java code. Also keep in mind that you don't even necessarily have the line #s available to you if the code was not compiled with debugging information built in.

Upvotes: 0

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

I'm trying to set properties of each method

I'd suggest you to use annotations and then get them with Method.getAnnotation

Upvotes: 1

Mirko
Mirko

Reputation: 1552

Did you checked ASM? http://asm.ow2.org/

But I think, what you are trying to do is very cpu-expensive.

Upvotes: 0

calderonmluis
calderonmluis

Reputation: 547

You could just directly access the .java file in the code. Just point it to the correct directory and access the file as you would any other.

The program is not running the java file itself, there are compiled files instead that are used at runtime.

Upvotes: 2

Related Questions