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