After the source code has been parsed, the content of the files can be navigated using a simple to use and intuitive object model.
Represents a complete .java file. This contains a collection of classes.
package com.blah.foo; import java.awt.*; import java.util.List; public class Class1 { ... } class Class2 { } interface Interface1 { }
JavaDocBuilder builder = new JavaDocBuilder(); builder.addSource(myReader); JavaSource src = builder.getSources[](0); JavaPackage pkg = src.getPackage(); String[] imports = src.getImports(); // {"java.awt.*", // "java.util.List"} JavaClass class1 = src.getClasses()[0]; JavaClass class2 = src.getClasses()[1]; JavaClass interface1 = src.getClasses()[2];
Represents the package of the class.
package com.blah.foo; public class BarClass { ... }
JavaDocBuilder builder = new JavaDocBuilder(); builder.addSource(myReader); JavaSource src = builder.getSources[](0); JavaPackage pkg = src.getPackage(); JavaClass[] classes = pkg.getClasses()[0]; // BarClass String name = pkg.getName(); // "com.blah.foo" String toString = pkg.toString(); // "package com.blah.foo" conform javaAPI JavaPackage parent = pkg.getParentPackage(); //
Represents a class or interface. This contains doclet tags, fields and methods. Information about the class definition is available, such as which classes are extended, which interfaces implemented and modifiers.
package com.blah.foo; import java.io.*; import com.custom.*; import com.base.SubClass; /** * @author Joe */ public abstract class MyClass extends SubClass implements Serializable, CustomInterface { private String name; public void doStuff() { ... } private int getNumber() { ... } }
JavaDocBuilder builder = new JavaDocBuilder(); builder.addSource(myReader); JavaClass cls = builder.getClassByName("com.blah.foo.MyClass"); String pkg = cls.getPackage(); // "com.blah.foo" String name = cls.getName(); // "MyClass" String fullName = cls.getFullyQualifiedName(); // "com.blah.foo.MyClass"; boolean isInterface = cls.isInterface(); // false boolean isPublic = cls.isPublic(); // true boolean isAbstract = cls.isAbstract(); // true boolean isFinal = cls.isFinal(); // false Type superClass = cls.getSuperClass(); // "com.base.SubClass"; Type[] imps = cls.getImplements(); // {"java.io.Serializable", // "com.custom.CustomInterface"} String author = cls.getTagsByName("author").getValue(); // "joe" JavaField nameField = cls.getFields()[0]; JavaMethod doStuff = cls.getMethods()[0]; JavaMethod getNumber = cls.getMethods()[1]; JavaSource javaSource = cls.getParentSource();
Represents a field in a class. This has doclet tags, a name and a type.
import java.util.Date; public class MyClass { /** * @magic */ private String email; public static Date[][] dates; }
JavaField e = cls.getFields()[0]; Type eType = e.getType(); // "java.lang.String"; String eName = e.getName(); // "email"; DocletTag eTag = e.getTagsByName("magic"); // @magic boolean eArray = e.getType().isArray(); // false; JavaField d = cls.getFields()[1]; Type dType = d.getType(); // "java.util.Date"; String dName = d.getName(); // "dates"; DocletTag dTag = d.getTagsByName("magic"); // null boolean dArray = d.getType().isArray(); // true; int dDimensions= d.getType().getDimensions(); // 2; boolean dStatic= d.isStatic(); // true; JavaClass javaClass = d.getParentClass();
Represents a method in a class. This has doclet tags, a name, return type, parameters and exceptions.
import java.util.Date; import java.io.*; public class MyClass { /** * @returns Lots of dates */ public static Date[] doStuff(int number, String stuff) throws RuntimeException, IOException { ... } }
JavaMethod m = cls.getMethods()[0]; String mName = m.getName(); // "doStuff"; Type mReturns = m.getReturns(); // "java.util.Date"; boolean mArray = m.getReturns().isArray(); // true boolean mStatic = m.isStatic(); // true boolean mPublic = m.isPublic(); // true String doc = m.getTagByName("returns").getValue(); // "Lots of dates" Type[] exceptions = m.getExceptions(); // {"java.lang.RuntimeException", "java.io.IOException"} JavaParameter numberParam = m.getParameters()[0]; JavaParameter stuffParam = m.getParameters()[1]; JavaClass javaClass = m.getParentClass();
Represents a parameter passed to a method. This has a name and a type.
public class MyClass { public void stuff(int n, Object[] objects) { ... } }
JavaMethod m = cls.getMethods()[0]; JavaParameter n = m.getParameters()[0]; String nName = n.getName(); // "n" Type nType = n.getType(); // "int"; JavaParameter o = m.getParameters()[1]; String oName = o.getName(); // "objects" Type oType = o.getType(); // "java.lang.Object"; boolean oArray = o.getType().isArray(); // true JavaMethod javaMethod = o.getParentMethod();
Represents a specific instance of a class used by another class (such as return value, superclass, etc). The value represents the name of the class. Array dimensions are also available. Since 1.8 it's also possible to get the generic value of the Type
import java.util.*; public class MyClass { public void stuff(int n, Object[] objects, Date[][] dates, ListstringList) { ... } }
JavaMethod m = cls.getMethods()[0]; Type returns = m.getReturns(); returns.getValue(); // "void" returns.isArray(); // false returns.getDimensions(); // 0 Type n = m.getParameters()[0].getType(); n.getValue(); // "int" n.isArray(); // false n.getDimensions(); // 0 Type objects = m.getParameters()[1].getType(); objects.getValue(); // "java.lang.Object" objects.isArray(); // true objects.getDimensions(); // 1 Type dates = m.getParameters()[2].getType(); dates.getValue(); // "java.util.Date" dates.isArray(); // true dates.getDimensions(); // 2 Type stringList = m.getParameters()[3].getType(); stringList.getValue(); // "java.util.List" stringList.getGenericValue(); // "java.util.List" stringList.isArray(); // false stringList.getDimensions(); // 0
Represents a JavaDoc tag. Each tag has a name and a value. Optionally, the value can be broken up into tokens accessed by index or name.
The JavaClass
,
JavaField
and
JavaMethod
classes all
support comments and DocletTags
The returned DocletTag
carries
the name, value and methods for breaking up the value into specific parameters.
/** * This method does nothing at all. * * @returns A boolean of whether we care or not. * @param email Someone's email address. * @param dob Date of birth. * * @permission administrator full-access * @webservice publish=true name=myservice type=rpc */ boolean doWeCare(String email, Date dob);
JavaMethod mth = cls.getMethods()[0]; // Access the JavaDoc comment String comment = mth.getComment(); // "This method does nothing at all." // Access a single doclet tag DocletTag returns = mth.getTagByName("returns"); returns.getName(); // "returns"; returns.getValue(); // "A boolean of whether we care or not." // Access multiple doclet tags with the same name DocletTag[] params = mth.getTagsByName("param"); params[0].getValue(); // "Someone's email address." params[1].getValue(); // "Date of birth." // Access specific parameters of a doclet tag by index DocletTag permission = mth.getTagByName("permission"); permission.getParameter[0]; // "administrator" permission.getParameter[1]; // "full-access" // Access specific parameters of a doclet tag by name DocletTag webservice = mth.getTagByName("webservice"); webservice.getNamedParameter("type"); // "rpc" webservice.getNamedParameter("name"); // "myservice"