KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > rebind > ConstantsWithLookupImplCreator


1 /*
2  * Copyright 2006 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.i18n.rebind;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.core.ext.typeinfo.JClassType;
21 import com.google.gwt.core.ext.typeinfo.JMethod;
22 import com.google.gwt.core.ext.typeinfo.JParameter;
23 import com.google.gwt.core.ext.typeinfo.JType;
24 import com.google.gwt.core.ext.typeinfo.TypeOracle;
25 import com.google.gwt.core.ext.typeinfo.TypeOracleException;
26 import com.google.gwt.i18n.rebind.util.AbstractResource;
27 import com.google.gwt.user.rebind.AbstractMethodCreator;
28 import com.google.gwt.user.rebind.SourceWriter;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 class ConstantsWithLookupImplCreator extends ConstantsImplCreator {
34   final JMethod[] allInterfaceMethods;
35
36   private final Map JavaDoc namesToMethodCreators = new HashMap JavaDoc();
37
38   ConstantsWithLookupImplCreator(TreeLogger logger, SourceWriter writer,
39       JClassType localizableClass, AbstractResource messageBindings,
40       TypeOracle oracle) throws UnableToCompleteException {
41     super(logger, writer, localizableClass, messageBindings, oracle);
42     try {
43
44       // Boolean
45
JType booleanType = oracle.parse(boolean.class.getName());
46       LookupMethodCreator booleanMethod = new LookupMethodCreator(this,
47           booleanType) {
48         public void printReturnTarget() {
49           println("return target.booleanValue();");
50         }
51
52         public String JavaDoc returnTemplate() {
53           return "boolean answer = {0}();\n cache.put(\"{0}\",new Boolean(answer));return answer;";
54         }
55       };
56       namesToMethodCreators.put("getBoolean", booleanMethod);
57
58       // Double
59
JType doubleType = oracle.parse(double.class.getName());
60       LookupMethodCreator doubleMethod = new LookupMethodCreator(this,
61           doubleType) {
62         public void printReturnTarget() {
63           println("return target.doubleValue();");
64         }
65
66         public String JavaDoc returnTemplate() {
67           return "double answer = {0}();\n cache.put(\"{0}\",new Double(answer));return answer;";
68         }
69       };
70       namesToMethodCreators.put("getDouble", doubleMethod);
71
72       // Int
73
JType intType = oracle.parse(int.class.getName());
74       LookupMethodCreator intMethod = new LookupMethodCreator(this, intType) {
75         public void printReturnTarget() {
76           println("return target.intValue();");
77         }
78
79         public String JavaDoc returnTemplate() {
80           return "int answer = {0}();\n cache.put(\"{0}\",new Integer(answer));return answer;";
81         }
82       };
83
84       namesToMethodCreators.put("getInt", intMethod);
85
86       // Float
87
JType floatType = oracle.parse(float.class.getName());
88       LookupMethodCreator floatMethod = new LookupMethodCreator(this, floatType) {
89         public String JavaDoc returnTemplate() {
90           String JavaDoc val = "float v ={0}(); cache.put(\"{0}\", new Float(v));return v;";
91           return val;
92         }
93
94         protected void printReturnTarget() {
95           println("return target.floatValue();");
96         }
97       };
98       namesToMethodCreators.put("getFloat", floatMethod);
99
100       // Map
101
JType mapType = oracle.parse(Map JavaDoc.class.getName());
102       namesToMethodCreators.put("getMap",
103           new LookupMethodCreator(this, mapType));
104
105       // String
106
JType stringType = oracle.parse(String JavaDoc.class.getName());
107       LookupMethodCreator stringMethod = new LookupMethodCreator(this,
108           stringType) {
109         public String JavaDoc returnTemplate() {
110           return "String answer = {0}();\n cache.put(\"{0}\",answer);return answer;";
111         }
112       };
113       namesToMethodCreators.put("getString", stringMethod);
114
115       // String Array
116
JType stringArray = oracle.getArrayType(stringType);
117       namesToMethodCreators.put("getStringArray", new LookupMethodCreator(this,
118           stringArray));
119
120       setNeedCache(true);
121       allInterfaceMethods = getAllInterfaceMethods(localizableClass);
122     } catch (TypeOracleException e) {
123       throw error(logger, e);
124     }
125   }
126
127   /**
128    * Create the method body associated with the given method. Arguments are
129    * arg0...argN.
130    */

131   protected void emitMethodBody(TreeLogger logger, JMethod method)
132       throws UnableToCompleteException {
133     checkMethod(logger, method);
134     if (method.getParameters().length == 1) {
135       String JavaDoc name = method.getName();
136       AbstractMethodCreator c = (AbstractMethodCreator) namesToMethodCreators.get(name);
137       if (c != null) {
138         c.createMethodFor(logger, method, null);
139         return;
140       }
141     }
142     // fall through
143
super.emitMethodBody(logger, method);
144   }
145
146   /**
147    * Checks that the method has the right structure to implement
148    * <code>Constant</code>.
149    *
150    * @param method method to check
151    */

152   private void checkMethod(TreeLogger logger, JMethod method)
153       throws UnableToCompleteException {
154     if (namesToMethodCreators.get(method.getName()) != null) {
155       JParameter[] params = method.getParameters();
156       // getString() might be returning a String argument, so leave it alone.
157
if (params.length == 0) {
158         return;
159       }
160       if (params.length != 1
161           || !params[0].getType().getQualifiedSourceName().equals(
162               "java.lang.String")) {
163         String JavaDoc s = method + " must have a single String argument.";
164         throw error(logger, s);
165       }
166     } else {
167       if (method.getParameters().length > 0) {
168         throw error(
169             logger,
170             "User-defined methods in interfaces extending ConstantsWithLookup must have no parameters and a return type of int, String, String[], ...");
171       }
172     }
173   }
174 }
175
Popular Tags