KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.util.*;
6 import polyglot.visit.*;
7 import polyglot.main.Options;
8
9 /**
10  * A <code>CanonicalTypeNode</code> is a type node for a canonical type.
11  */

12 public class CanonicalTypeNode_c extends TypeNode_c implements CanonicalTypeNode
13 {
14   public CanonicalTypeNode_c(Position pos, Type type) {
15     super(pos);
16     this.type = type;
17   }
18
19   /**
20    * If the "use-fully-qualified-class-names" options is used, then the
21    * fully qualified names is written out (<code>java.lang.Object</code>).
22    * Otherwise, the string that originally represented the type in the
23    * source file is used.
24    */

25   public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
26     if (type == null) w.write("<unknown-type>");
27     else w.write(type.toString());
28   }
29
30   /** Type check the type node. Check accessibility of class types. */
31   public Node typeCheck(TypeChecker tc) throws SemanticException {
32       TypeSystem ts = tc.typeSystem();
33
34       if (type.isClass()) {
35           ClassType ct = type.toClass();
36           if (ct.isTopLevel() || ct.isMember()) {
37               if (! ts.classAccessible(ct, tc.context())) {
38                   throw new SemanticException("Cannot access class \"" +
39                       ct + "\" from the body of \"" +
40                       tc.context().currentClass() + "\".", position());
41               }
42           }
43       }
44
45       return this;
46   }
47
48   public void translate(CodeWriter w, Translator tr) {
49     TypeSystem ts = tr.typeSystem();
50
51     if (tr.outerClass() != null) {
52       w.write(type.translate(ts.classContextResolver(tr.outerClass())));
53     }
54     else if (Options.global.fully_qualified_names) {
55       // if a field or local variable in this context shadows the package
56
// name of the type, then the output code may not compile, e.g.
57
// ...
58
// java.lang.Object foo;
59
// foo.package.Bar = new foo.package.Bar();
60
// ...
61
//
62
// But, the user asked for fully qualified names, so that's what they
63
// get.
64
w.write(type.translate(null));
65     }
66     else {
67       w.write(type.translate(tr.context()));
68     }
69   }
70
71   public String JavaDoc toString() {
72     if (type == null) return "<unknown-type>";
73     return type.toString();
74   }
75
76   public void dump(CodeWriter w) {
77     super.dump(w);
78     w.allowBreak(4, " ");
79     w.begin(0);
80     w.write("(type " + type + ")");
81     w.end();
82   }
83 }
84
Popular Tags