KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Import_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4
5 import polyglot.types.*;
6 import polyglot.util.*;
7 import polyglot.visit.*;
8 import polyglot.main.Options;
9
10 /**
11  * An <code>Import</code> is an immutable representation of a Java
12  * <code>import</code> statement. It consists of the string representing the
13  * item being imported and the kind which is either indicating that a class
14  * is being imported, or that an entire package is being imported.
15  */

16 public class Import_c extends Node_c implements Import
17 {
18     protected Kind kind;
19     protected String JavaDoc name;
20
21     public Import_c(Position pos, Kind kind, String JavaDoc name) {
22     super(pos);
23     this.name = name;
24     this.kind = kind;
25     }
26
27     /** Get the name of the import. */
28     public String JavaDoc name() {
29     return this.name;
30     }
31
32     /** Set the name of the import. */
33     public Import name(String JavaDoc name) {
34     Import_c n = (Import_c) copy();
35     n.name = name;
36     return n;
37     }
38
39     /** Get the kind of the import. */
40     public Kind kind() {
41     return this.kind;
42     }
43
44     /** Set the kind of the import. */
45     public Import kind(Kind kind) {
46     Import_c n = (Import_c) copy();
47     n.kind = kind;
48     return n;
49     }
50
51     /** Build type objects for the import. */
52     public Node buildTypes(TypeBuilder tb) throws SemanticException {
53     ImportTable it = tb.importTable();
54
55     if (kind == CLASS) {
56         it.addClassImport(name);
57     }
58     else if (kind == PACKAGE) {
59         it.addPackageImport(name);
60     }
61
62     return this;
63     }
64
65     /** Check that imported classes and packages exist. */
66     public Node typeCheck(TypeChecker tc) throws SemanticException {
67         if (kind == PACKAGE && tc.typeSystem().packageExists(name)) {
68             return this;
69         }
70
71         // Must be importing a class, either as p.C, or as p.C.*
72

73         // The first component of the type name must be a package.
74
String JavaDoc pkgName = StringUtil.getFirstComponent(name);
75
76         if (! tc.typeSystem().packageExists(pkgName)) {
77             throw new SemanticException("Package \"" + pkgName +
78                 "\" not found.", position());
79         }
80
81         // The type must exist.
82
Named nt = tc.typeSystem().forName(name);
83
84         // And the type must be accessible.
85
if (nt instanceof Type) {
86             Type t = (Type) nt;
87             if (t.isClass()) {
88                 tc.typeSystem().classAccessibleFromPackage(t.toClass(),
89                     tc.context().package_());
90             }
91         }
92
93     return this;
94     }
95
96     public String JavaDoc toString() {
97     return "import " + name + (kind == PACKAGE ? ".*" : "");
98     }
99
100     /** Write the import to an output file. */
101     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
102     if (! Options.global.fully_qualified_names) {
103         w.write("import ");
104         w.write(name);
105
106         if (kind == PACKAGE) {
107             w.write(".*");
108         }
109
110         w.write(";");
111         w.newline(0);
112     }
113     }
114 }
115
Popular Tags