KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > impl > TypeMap


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs.impl;
17
18 import com.google.gwt.dev.jjs.InternalCompilerException;
19 import com.google.gwt.dev.jjs.ast.JArrayType;
20 import com.google.gwt.dev.jjs.ast.JNode;
21 import com.google.gwt.dev.jjs.ast.JProgram;
22 import com.google.gwt.dev.jjs.ast.JType;
23
24 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
25 import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding;
26 import org.eclipse.jdt.internal.compiler.lookup.Binding;
27
28 import java.util.IdentityHashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Contains the list of the top-level and array types.
33  */

34 public class TypeMap {
35
36   /**
37    * Maps Eclipse AST nodes to our JNodes.
38    */

39   private final Map JavaDoc/* <Binding, JNode> */crossRefMap = new IdentityHashMap JavaDoc();
40
41   /**
42    * Centralizes creation and singleton management.
43    */

44   private final JProgram program;
45
46   public TypeMap(JProgram program) {
47     this.program = program;
48   }
49
50   public JNode get(Binding binding) {
51     JNode result = internalGet(binding);
52     if (result == null) {
53       InternalCompilerException ice = new InternalCompilerException(
54           "Failed to get JNode");
55       ice.addNode(binding.getClass().getName(), binding.toString(), null);
56       throw ice;
57     }
58     return result;
59   }
60
61   public JProgram getProgram() {
62     return program;
63   }
64
65   public void put(Binding binding, JNode to) {
66     if (binding == null) {
67       throw new InternalCompilerException("Trying to put null into typeMap.");
68     }
69
70     Object JavaDoc old = crossRefMap.put(binding, to);
71     assert (old == null);
72   }
73
74   public JNode tryGet(Binding binding) {
75     return internalGet(binding);
76   }
77
78   private JNode internalGet(Binding binding) {
79     JNode cached = (JNode) crossRefMap.get(binding);
80     if (cached != null) {
81       // Already seen this one.
82
return cached;
83     } else if (binding instanceof BaseTypeBinding) {
84       BaseTypeBinding baseTypeBinding = (BaseTypeBinding) binding;
85       // see org.eclipse.jdt.internal.compiler.lookup.TypeIds constants
86
switch (baseTypeBinding.id) {
87         case BaseTypeBinding.T_undefined:
88           return null;
89         case BaseTypeBinding.T_JavaLangObject:
90           // here for consistency, should already be cached
91
return program.getTypeJavaLangObject();
92         case BaseTypeBinding.T_char:
93           return program.getTypePrimitiveChar();
94         case BaseTypeBinding.T_byte:
95           return program.getTypePrimitiveByte();
96         case BaseTypeBinding.T_short:
97           return program.getTypePrimitiveShort();
98         case BaseTypeBinding.T_boolean:
99           return program.getTypePrimitiveBoolean();
100         case BaseTypeBinding.T_void:
101           return program.getTypeVoid();
102         case BaseTypeBinding.T_long:
103           return program.getTypePrimitiveLong();
104         case BaseTypeBinding.T_double:
105           return program.getTypePrimitiveDouble();
106         case BaseTypeBinding.T_float:
107           return program.getTypePrimitiveFloat();
108         case BaseTypeBinding.T_int:
109           return program.getTypePrimitiveInt();
110         case BaseTypeBinding.T_JavaLangString:
111           // here for consistency, should already be cached
112
return program.getTypeJavaLangString();
113         case BaseTypeBinding.T_null:
114           return program.getTypeNull();
115         default:
116           return null;
117       }
118     } else if (binding instanceof ArrayBinding) {
119       ArrayBinding arrayBinding = (ArrayBinding) binding;
120
121       // Compute the JType for the leaf type
122
JType leafType = (JType) get(arrayBinding.leafComponentType);
123
124       // Don't create a new JArrayType; use TypeMap to get the singleton
125
// instance
126
JArrayType arrayType = program.getTypeArray(leafType,
127           arrayBinding.dimensions);
128
129       return arrayType;
130     } else {
131       return null;
132     }
133   }
134
135 }
136
Popular Tags