1

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
    }
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • 1
    Sure, a simple `InputStream` or `BufferedReader` can do. The larger issue though, you're going to have to deploy your source code along with the your binaries. Take a look at [Java : parse java source code, extract methods](http://stackoverflow.com/questions/2206065/java-parse-java-source-code-extract-methods) – MadProgrammer Oct 01 '12 at 03:55
  • That's no issue, as long as I'm not developing closed-source software. – Anderson Green Oct 01 '12 at 03:56
  • Reading source code **mainly** for debugging purpose? – Bhesh Gurung Oct 01 '12 at 03:57
  • debugging tools are designed for debugging purpose. is there any reason you cannot use them? – gigadot Oct 01 '12 at 03:57
  • Learn about Reflection . Thanx to your question , i came to know of this concept and myself learning too. See this thread... http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful – Mudassir Hasan Oct 01 '12 at 03:57
  • Specifically, I'm trying to make it possible for a Java method to read the comment on the previous line. I'm trying to set properties of each method (like Javascript method prototypes), and I thought that using comments would be a decent workaround, since Java does not have object prototypes. – Anderson Green Oct 01 '12 at 04:00
  • you want the machines to see the source, to understand where they're coming from? we're doomed! – Denis Tulskiy Oct 01 '12 at 04:48

5 Answers5

2

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.

calderonmluis
  • 547
  • 4
  • 11
1

I'm trying to set properties of each method

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

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
  • That could be useful. I'll look into that. – Anderson Green Oct 01 '12 at 14:50
  • Here's an example usage: http://www.java2s.com/Code/JavaAPI/java.lang.reflect/MethodgetAnnotationClassMyAnnoannotationClass.htm – Anderson Green Oct 01 '12 at 15:14
  • @AndersonGreen: yes, something like that. This information can be remained in runtime so you don't need to include source code with your class files. – Denis Tulskiy Oct 01 '12 at 15:24
  • How can you get the properties of one specific method when you have multiple methods with the same name? (e.g., `public static void whoo(String)` and `public static void whoo(int)`)? – Anderson Green Oct 04 '12 at 01:32
  • `Class c = ob.getClass();` `Method m = c.getMethod("myMeth"); // how would I write this if I had multiple methods named myMeth that took different parameters?` – Anderson Green Oct 04 '12 at 01:34
  • 1
    @AndersonGreen: getMethod() also accepts a list of classes that identify method parameters. So you would write `Method m = c.getMethod("myMeth", String.class)` or `Method m = c.getMethod("myMeth", Integer.class)`. See javadoc: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getMethod(java.lang.String, java.lang.Class...) – Denis Tulskiy Oct 04 '12 at 03:24
0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Why is it unlikely that the source code would be available? Is this true for open source software as well as proprietary software? – Anderson Green Oct 01 '12 at 04:34
  • What I'm saying is that it is unlikely that the source code will be installed on the execution platform in a place where the application can find it ... unless you've made a conscious decision to put the source code into the application installer and install it. Generally, developers don't do that because (for typical end-users) it bloats the installer and wastes disc space. – Stephen C Oct 01 '12 at 06:00
  • I'm actually trying to set properties of a function (which is very easy to do in Javascript, but difficult to do in other programming languages.) I need to keep track of methods that have been tested, versus methods I've started writing that haven't been tested. Since Java methods don't have a `prototype`, as in Javascript, allowing Java methods to access their own source code (with comments that describe the properties of each function) could be used as a workaround. (Is there a better way to do this?) – Anderson Green Oct 01 '12 at 14:43
0

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

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

Mirko
  • 1,512
  • 1
  • 12
  • 19
  • Why would it be CPU-expensive? – Anderson Green Oct 01 '12 at 14:35
  • I thought that you want to access the code by reflecting over your classes/classfiles. So if you only want to read a file line by line... you just have to get the current linenumber of your program. And I think you can simply access it by using the remote debugging interface that is used by the most debugging tools. – Mirko Oct 02 '12 at 05:30
0

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.

user439407
  • 1,666
  • 2
  • 19
  • 40
  • Even with no optimization and no obfuscation, the decompiled source code cannot look like the original source code. For a start, all comments are lost ... and the OP specifically asks about accessing the comments. – Stephen C Oct 01 '12 at 06:04
  • True, you can access javadocs if you compile them in, but not regular comments... Missed that one. – user439407 Oct 01 '12 at 06:31