The Model

After the source code has been parsed, the content of the files can be navigated using a simple to use and intuitive object model.

JavaSource

Represents a complete .java file. This contains a collection of classes.

Example Input

    package com.blah.foo;
    
    import java.awt.*;
    import java.util.List;
    
    public class Class1 {
      ...
    }
    
    class Class2 {
    }
    
    interface Interface1 {
    }
            

Example Code

    JavaProjectBuilder builder = new JavaProjectBuilder();
    JavaSource src = builder.addSource(myReader);
    
    JavaPackage pkg      = src.getPackage(); 
    List<String> imports     = src.getImports(); // {"java.awt.*",
                                         //  "java.util.List"}
    
    JavaClass class1     = src.getClasses().get(0);
    JavaClass class2     = src.getClasses().get(1);
    JavaClass interface1 = src.getClasses().get(2);
            

JavaPackage

Represents the package of the class.

Example input

    package com.blah.foo;
    
    public class BarClass  {
    ...
    }
  

Example Code

    JavaProjectBuilder builder = new JavaProjectBuilder();
    JavaSource src = builder.addSource(myReader);
    
    JavaPackage pkg      = src.getPackage();
    
    Collection<JavaClass> classes  = pkg.getClasses(); // BarClass
    String name          = pkg.getName(); // "com.blah.foo"
    String toString      = pkg.toString(); // "package com.blah.foo" conform javaAPI
    JavaPackage parent   = pkg.getParentPackage(); //
  

JavaClass

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.

Example Input

    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() { ... }
    
    }
  

Example Code

    JavaProjectBuilder builder = new JavaProjectBuilder();
    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.getCanonicalName(); // "com.blah.foo.MyClass";
    String canonicalName = 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
    
    JavaType superClass = cls.getSuperClass(); // "com.base.SubClass";
    List<JavaType> 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();
  

JavaField

Represents a field in a class. This has doclet tags, a name and a type.

Example Input

    import java.util.Date;
    
    public class MyClass  {
    
      /**
       * @magic
       */
      private String email;
    
      public static Date[][] dates;
    
    }
            

Example Code

    JavaField e = cls.getFields()[0];
    
    JavaType 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];
    
    JavaType 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();
            

JavaMethod

Represents a method in a class. This has doclet tags, a name, return type, parameters and exceptions.

Example Input

    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 {
        ...
      }
    
    }
  

Example Code

    JavaMethod m = cls.getMethods()[0];
    
    String mName = m.getName(); // "doStuff";
    JavaType 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"
    
    List<JavaType> exceptions = m.getExceptions();
      // {"java.lang.RuntimeException", "java.io.IOException"}
    
    JavaParameter numberParam = m.getParameters()[0];
    JavaParameter stuffParam = m.getParameters()[1];
    
    JavaClass javaClass = m.getParentClass();
  

JavaParameter

Represents a parameter passed to a method. This has a name and a type.

Example Input

    public class MyClass  {
    
      public void stuff(int n, Object[] objects) {
        ...
      }
    
    }
  

Example Code

    JavaMethod m = cls.getMethods()[0];
    
    JavaParameter n = m.getParameters()[0];
    String nName = n.getName(); // "n"
    JavaType nType   = n.getType(); // "int";
    
    JavaParameter o = m.getParameters()[1];
    String oName   = o.getName(); // "objects"
    JavaType oType     = o.getType(); // "java.lang.Object";
    boolean oArray = o.getType().isArray(); // true
    
    JavaMethod javaMethod = o.getParentMethod();
  

JavaType

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

Example Input

    
  
    import java.util.*;
    
    public class MyClass  {
    
      public void stuff(int n, Object[] objects,
    	                  Date[][] dates, List<String> stringList) {
        ...
      }
    
    }
    
  

Example Code

    JavaMethod m = cls.getMethods()[0];
    
    JavaType returns = m.getReturns();
    returns.getValue(); // "void"
    returns.isArray(); // false
    returns.getDimensions(); // 0
    
    JavaType n = m.getParameters()[0].getType();
    n.getValue(); // "int"
    n.isArray(); // false
    n.getDimensions(); // 0
    
    JavaType objects = m.getParameters()[1].getType();
    objects.getValue(); // "java.lang.Object"
    objects.isArray(); // true
    objects.getDimensions(); // 1
    
    JavaType dates = m.getParameters()[2].getType();
    dates.getValue(); // "java.util.Date"
    dates.isArray(); // true
    dates.getDimensions(); // 2
    
    JavaType stringList = m.getParameters()[3].getType();
    stringList.getValue(); // "java.util.List"
    stringList.getGenericValue(); // "java.util.List<java.lang.String>"
    stringList.isArray(); // false
    stringList.getDimensions(); // 0
          

DocletTag

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.

Example Input

    /**
     * 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);
            

Example Code

    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"