views:

98

answers:

6

I'd like to somehow create some kind of class that can read a source code file(ie java) and from that extract the method names, the method source-code, the methods description comment and such.

Is this possible in any simple way? I'd also like to avoid complicated parsers and such. Is regexp any good for this?

Programming language = java, source code language = java (for example)

+1  A: 

Doxygen is the tool for you. It will generate documentation containing method and its signature from the source code. I have used it and its good.

Rachel
Its unfortunately not in java, though I should have made it more clear that it was IN java as well as source code from java
Karleid
+3  A: 

Are you just looking to document your code? if so The simplest and built right into the JDK is Javadoc. Javadoc will parse the comments from your code along with the method names, classes etc and create a nice HTML structure for you. If you have ever read the Java API that was created using Javadoc.

Other then that if you want to look at writing your own program to show you the contents of a class, try using some tools from Java.

For example, the below code will take a JTextArea and print out to the user the methods which JTextArea defines and their corresponding parameters.

import java.lang.reflect.Method;
import java.lang.reflect.Type;

JTextArea myArea = new JTextArea();
Method[] methods = myArea.getClass().getDeclaredMethods();

for(int i=0; i<methods.length; i++){
     System.out.println("Method Name:: " + methods[i].getName() );
     System.out.println("Return Type:: " + methods[i].getReturnType());
     Type[] params = method[i].getGenericParameterTypes();
     for(int j=0; j<params.length; j++){
          System.out.println("Parameter: " + params[j].toString());
     }
} 
Sean
+1 for mentioning Javadoc.
Bernard
Exactly.. Reflection APIs and Javadoc.
Ashish Patil
Neither reflection nor Javadoc address method source code, and reflection will not address non-runtime annotations, an in general the assumption is that the code is compiled when it is inspected.
Yishai
A: 

What you need is something close to a compiler. There are probably compiler implementations that could do it for you, but this project looks like it can help you out there, and perhaps be a bit simpler than mucking with a compiler.

Yishai
+1  A: 

If extracting just method names, comments on method declarations, and method bodies is the only goal, you can probably write a simple finite statement machine [FSM] with counters (to track { } nesting, etc.) (or maybe a push down automaton [PDA]) to track just enough of the langauge to extract this information. You'll use regexes to detect "tokens" that cause transitions between states, and the content of items you want. (This is called an island parser).

Be prepared to spend a bunch of time tuning your FSM/PDA, because in essence you are trying to build a parser with a subset of the Java grammar rules, and you will be empirically rediscovering which part of the grammar matters. If you want to do anything more sophisticated you'll need a real parser, and you'll appreciate doing it that way.

Ira Baxter
A: 

JavaCC is a parser generator. I know you don't want a parser generator and only mention it because source for a java parser is included which you may be able to hack.

Michael Burrows
A: 

You can use Annotation Processing.

In order to use annotation processing there must be at least one annotation in your project, but it can be a meaningless one in an otherwise empty source file.

Extend AbstractProcessor

  1. Annotate that with SupportedAnnotationTypes ( "*" )
  2. Use getRootElements to implement the process method
  3. Implement an ElementVisitor to handle the elements
    1. For method names - getSimpleName
    2. For method description comment - getDocComment
    3. For method source code - getTree
emory