KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > pao > types > PaoTypeSystem_c


1 package polyglot.ext.pao.types;
2
3 import java.util.Collections JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.LinkedList JavaDoc;
6 import java.util.List JavaDoc;
7
8 import polyglot.ext.jl.types.TypeSystem_c;
9 import polyglot.frontend.Source;
10 import polyglot.types.*;
11 import polyglot.util.InternalCompilerError;
12
13 /**
14  * Implementation of the PAO type system interface. Also overrides some
15  * methods of <code>TypeSystem_c</code>.
16  */

17 public class PaoTypeSystem_c extends TypeSystem_c implements PaoTypeSystem {
18
19     /**
20      * Returns a new instance of <code>PaoPrimitiveType_c</code>
21      * @see PaoPrimitiveType_c
22      */

23     public PrimitiveType createPrimitive(PrimitiveType.Kind kind) {
24         return new PaoPrimitiveType_c(this, kind);
25     }
26
27     /**
28      * Returns a new instance of <code>PaoParsedClassType_c</code>
29      * @see PaoParsedClassType_c
30      */

31     public ParsedClassType createClassType(LazyClassInitializer init,
32                                            Source fromSource) {
33         return new PaoParsedClassType_c(this, init, fromSource);
34     }
35
36     /**
37      * The package that contains the runtime classes for boxing primitive
38      * values as objects.
39      */

40     private static final String JavaDoc RUNTIME_PACKAGE = "polyglot.ext.pao.runtime";
41
42     /**
43      * @see polyglot.ext.pao.types.PaoTypeSystem#primitiveEquals()
44      */

45     public MethodInstance primitiveEquals() {
46         // The method instance could be cached for greater efficiency,
47
// but we are not too worried about this.
48
String JavaDoc name = RUNTIME_PACKAGE + ".Primitive";
49
50         try {
51             // use the system resolver to find the type named by name.
52
Type ct = (Type) systemResolver().find(name);
53
54             // create an argument list: two arguments of type Object.
55
List JavaDoc args = new LinkedList JavaDoc();
56             args.add(Object());
57             args.add(Object());
58
59             // take the first method "equals(Object, Object)" in ct.
60
List JavaDoc l = ct.toClass().methods("equals", args);
61             if (!l.isEmpty()) {
62                 return (MethodInstance)l.get(0);
63             }
64         }
65         catch (SemanticException e) {
66             throw new InternalCompilerError(e.getMessage());
67         }
68
69         throw new InternalCompilerError("Could not find equals method.");
70     }
71
72     public MethodInstance getter(PrimitiveType t) {
73         // The method instances could be cached for greater efficiency,
74
// but we are not too worried about this.
75

76         String JavaDoc methodName = t.toString() + "Value";
77         
78         // get the type used to represent boxed values of type t
79
ReferenceType boxedType = boxedType(t);
80
81         // take the first method with the appropriate name and an empty
82
// argument list, in the type boxedType
83
List JavaDoc l = boxedType.methods(methodName, Collections.EMPTY_LIST);
84         if (!l.isEmpty()) {
85             return (MethodInstance)l.get(0);
86         }
87
88         throw new InternalCompilerError("Could not find getter for " + t);
89     }
90
91     public ClassType boxedType(PrimitiveType t) {
92         // The class types could be cached for greater efficiency,
93
// but we are not too worried about this.
94

95         String JavaDoc name = RUNTIME_PACKAGE + "."
96                 + wrapperTypeString(t).substring("java.lang.".length());
97
98         try {
99             return ((Type)systemResolver().find(name)).toClass();
100
101         }
102         catch (SemanticException e) {
103             throw new InternalCompilerError(e.getMessage());
104         }
105     }
106
107     public ConstructorInstance wrapper(PrimitiveType t) {
108         // The constructor instances could be cached for greater efficiency,
109
// but we are not too worried about this.
110

111         ClassType ct = boxedType(t);
112         for (Iterator JavaDoc i = ct.constructors().iterator(); i.hasNext(); ) {
113             ConstructorInstance ci = (ConstructorInstance) i.next();
114             if (ci.formalTypes().size() == 1) {
115                 Type argType = (Type) ci.formalTypes().get(0);
116                 if (equals(argType, t)) {
117                     // found the appropriate constructor
118
return ci;
119                 }
120             }
121         }
122
123         throw new InternalCompilerError("Could not find constructor for " + t);
124     }
125 }
126
Popular Tags