- QDox is a library from thoughtwork for java source parsing.
- Qdox provides a class called JavaBuilder to create java document.
- QDox library download link :-
http://qdox.codehaus.org/download.html
Project Structure:-
A Sample Java File DemoJava.java ,
package com.sandeep.qdox.demo;
import java.util.ArrayList;
import java.util.List;
public class DemoJava {
public void printName() {
System.out.println("Hi print name method");
}
public ListcreateListOfNames() {
Listnames = new ArrayList ();
names.add("sandeep");
names.add("Sangeeta");
return names;
}
}
- It has two method and a package name with imported java util list package.
QDox Parsing Java Source:-
- It provides JavaDocBuilder, JavaClass, JavaMethod etc. to parse java source file.
package com.sandeep.qdox.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaPackage;
import com.thoughtworks.qdox.model.JavaSource;
public class QDoxDemo {
public static void main(String[] args) {
JavaDocBuilder builder = new JavaDocBuilder();
try {
builder.addSource(new File("D:\workspace\QDoxDemo\src\com\sandeep\qdox\demo\DemoJava.java"));
JavaSource src = builder.getSources()[0];
JavaPackage pkg = src.getPackage();
String name = pkg.getName();
String toString = pkg.toString();
JavaPackage parent = pkg.getParentPackage();
JavaClass[] classes = pkg.getClasses();
JavaMethod[] methods = classes[0].getMethods();
for(JavaMethod method :methods){
System.out.println("Method Name : "+method.getName());
}
System.out.println("pkg name : "+name);
System.out.println("pkg to String : "+toString);
System.out.println("pkg parent name : "+parent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
Method Name : printName
Method Name : createListOfNames
pkg name : com.sandeep.qdox.demo
pkg to String : package com.sandeep.qdox.demo
pkg parent name : null