Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: openjava/ptree/CompilationUnit.java


1   /*
2    * CompilationUnit.java 1.0
3    *
4    * This subclass of symbol represents (at least) terminal symbols returned 
5    * by the scanner and placed on the parse stack.  At present, this 
6    * class does nothing more than its super class.
7    *
8    * Jun 11, 1997 by mich
9    * Sep 27, 1997 by mich
10   *   
11   * @see openjava.ptree.ParseTree
12   * @version 1.0  last updated: Sep 27, 1997
13   * @author  Michiaki Tatsubori
14   */
15  package openjava.ptree;
16  
17  import openjava.ptree.util.ParseTreeVisitor;
18  
19  /**
20   * The CompilationUnit class presents for the whole parse tree in a file.
21   *
22   * CompilationUnits consists of
23   * (package statement) (import statement list) (type declaration list)
24   * QualifiedName       ImportStatementList     ClassDeclarationList
25   * 
26   * @see openjava.ptree.ClassDeclarationList
27   */
28  public class CompilationUnit extends NonLeaf {
29    /**
30     * Allocates this object with specified parse-tree elements.
31     *
32     */
33    public CompilationUnit(String e0, String[] e1, ClassDeclarationList e2) {
34      super();
35      if (e1 == null)
36        e1 = new String[0];
37      if (e2 == null)
38        e2 = new ClassDeclarationList();
39      set(e0, e1, e2);
40    }
41  
42    /**
43     * Sets the package of this compilation unit
44     *
45     * @param  qn  the qualified name indicating this package
46     */
47    public void setPackage(String qn) {
48      setElementAt(qn, 0);
49    }
50  
51    /**
52     * Obtains the package of this compilation unit
53     *
54     * @return  the qualified name indicating this package
55     */
56    public String getPackage() {
57      return (String) elementAt(0);
58    }
59  
60    /**
61     * Sets the import statement list of this compilation unit
62     *
63     * @param  islst  the import statement list of this compilation unit
64     */
65    public void setDeclaredImports(String[] islst) {
66      setElementAt(islst, 1);
67    }
68  
69    /**
70     * Obtains the import statement list of this compilation unit
71     *
72     * @return  the import statement list of this compilation unit
73     */
74    public String[] getDeclaredImports() {
75      return (String[]) elementAt(1);
76    }
77  
78    /**
79     * Sets the type declaration list of this compilation unit
80     *
81     * @param  tdlst  the type declaration list of this compilation unit
82     */
83    public void setClassDeclarations(ClassDeclarationList tdlst) {
84      setElementAt(tdlst, 2);
85    }
86  
87    /**
88     * Obtains the type declaration list of this compilation unit
89     *
90     * @return  the type declaration list of this compilation unit
91     */
92    public ClassDeclarationList getClassDeclarations() {
93      return (ClassDeclarationList) elementAt(2);
94    }
95  
96    /**
97     * Obtains the public class in this compilation unit.
98     *
99     * @return  the public class
100    * @exception  ParseTreeException  if not one public class is declared.
101    */
102   public ClassDeclaration getPublicClass() throws ParseTreeException {
103     ClassDeclaration ret = null;
104 
105     ClassDeclarationList tdecls = getClassDeclarations();
106     for (int i = 0, len = tdecls.size(); i < len; ++i) {
107       ClassDeclaration cdecl = tdecls.get(i);
108       if (cdecl.getModifiers().contains(ModifierList.PUBLIC)) {
109         if (ret != null) {
110           throw new ParseTreeException(
111             "getPublicClass() "
112               + "in CompileationUnit : "
113               + "multiple public class");
114         }
115         ret = cdecl;
116       }
117     }
118 
119     return ret;
120   }
121 
122   /**
123    * Tests if the declared import string represents on demand
124    * importation.  For example, if the specified string is
125    * <code>java.lang.*</code>, this returns true, and if
126    * <code>java.lang.Object</code>, returns false;
127    *
128    * @param  import_decl  declared importation.
129    * @return  true if the string ends with ".*".
130    **/
131   public static boolean isOnDemandImport(String import_decl) {
132     return (import_decl.endsWith(".*"));
133   }
134 
135   /**
136    * Removes ".*" at tail if it exists.
137    *
138    * @param  import_decl  declared importation.
139    * @return  a string trimmed ".*" off
140    **/
141   public static String trimOnDemand(String import_decl) {
142     if (isOnDemandImport(import_decl)) {
143       return import_decl.substring(0, import_decl.length() - 2);
144     }
145     return import_decl;
146   }
147 
148   public void accept(ParseTreeVisitor v) throws ParseTreeException {
149     v.visit(this);
150   }
151 }