001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.i18n.rebind;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.core.ext.typeinfo.JClassType;
021: import com.google.gwt.core.ext.typeinfo.JMethod;
022: import com.google.gwt.core.ext.typeinfo.JParameter;
023: import com.google.gwt.core.ext.typeinfo.JType;
024: import com.google.gwt.core.ext.typeinfo.TypeOracle;
025: import com.google.gwt.core.ext.typeinfo.TypeOracleException;
026: import com.google.gwt.i18n.rebind.util.AbstractResource;
027: import com.google.gwt.user.rebind.AbstractMethodCreator;
028: import com.google.gwt.user.rebind.SourceWriter;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: class ConstantsWithLookupImplCreator extends ConstantsImplCreator {
034: final JMethod[] allInterfaceMethods;
035:
036: private final Map<String, AbstractMethodCreator> namesToMethodCreators = new HashMap<String, AbstractMethodCreator>();
037:
038: ConstantsWithLookupImplCreator(TreeLogger logger,
039: SourceWriter writer, JClassType localizableClass,
040: AbstractResource messageBindings, TypeOracle oracle)
041: throws UnableToCompleteException {
042: super (logger, writer, localizableClass, messageBindings, oracle);
043: try {
044:
045: // Boolean
046: JType booleanType = oracle.parse(boolean.class.getName());
047: LookupMethodCreator booleanMethod = new LookupMethodCreator(
048: this , booleanType) {
049: @Override
050: public void printReturnTarget() {
051: println("return target.booleanValue();");
052: }
053:
054: @Override
055: public String returnTemplate() {
056: return "boolean answer = {0}();\n cache.put(\"{0}\",new Boolean(answer));return answer;";
057: }
058: };
059: namesToMethodCreators.put("getBoolean", booleanMethod);
060:
061: // Double
062: JType doubleType = oracle.parse(double.class.getName());
063: LookupMethodCreator doubleMethod = new LookupMethodCreator(
064: this , doubleType) {
065: @Override
066: public void printReturnTarget() {
067: println("return target.doubleValue();");
068: }
069:
070: @Override
071: public String returnTemplate() {
072: return "double answer = {0}();\n cache.put(\"{0}\",new Double(answer));return answer;";
073: }
074: };
075: namesToMethodCreators.put("getDouble", doubleMethod);
076:
077: // Int
078: JType intType = oracle.parse(int.class.getName());
079: LookupMethodCreator intMethod = new LookupMethodCreator(
080: this , intType) {
081: @Override
082: public void printReturnTarget() {
083: println("return target.intValue();");
084: }
085:
086: @Override
087: public String returnTemplate() {
088: return "int answer = {0}();\n cache.put(\"{0}\",new Integer(answer));return answer;";
089: }
090: };
091:
092: namesToMethodCreators.put("getInt", intMethod);
093:
094: // Float
095: JType floatType = oracle.parse(float.class.getName());
096: LookupMethodCreator floatMethod = new LookupMethodCreator(
097: this , floatType) {
098: @Override
099: public String returnTemplate() {
100: String val = "float v ={0}(); cache.put(\"{0}\", new Float(v));return v;";
101: return val;
102: }
103:
104: @Override
105: protected void printReturnTarget() {
106: println("return target.floatValue();");
107: }
108: };
109: namesToMethodCreators.put("getFloat", floatMethod);
110:
111: // Map
112: JType mapType = oracle.parse(Map.class.getName());
113: namesToMethodCreators.put("getMap",
114: new LookupMethodCreator(this , mapType));
115:
116: // String
117: JType stringType = oracle.parse(String.class.getName());
118: LookupMethodCreator stringMethod = new LookupMethodCreator(
119: this , stringType) {
120: @Override
121: public String returnTemplate() {
122: return "String answer = {0}();\n cache.put(\"{0}\",answer);return answer;";
123: }
124: };
125: namesToMethodCreators.put("getString", stringMethod);
126:
127: // String Array
128: JType stringArray = oracle.getArrayType(stringType);
129: namesToMethodCreators.put("getStringArray",
130: new LookupMethodCreator(this , stringArray));
131:
132: setNeedCache(true);
133: allInterfaceMethods = getAllInterfaceMethods(localizableClass);
134: } catch (TypeOracleException e) {
135: throw error(logger, e);
136: }
137: }
138:
139: /**
140: * Create the method body associated with the given method. Arguments are
141: * arg0...argN.
142: */
143: @Override
144: protected void emitMethodBody(TreeLogger logger, JMethod method)
145: throws UnableToCompleteException {
146: checkMethod(logger, method);
147: if (method.getParameters().length == 1) {
148: String name = method.getName();
149: AbstractMethodCreator c = namesToMethodCreators.get(name);
150: if (c != null) {
151: c.createMethodFor(logger, method, null);
152: return;
153: }
154: }
155: // fall through
156: super .emitMethodBody(logger, method);
157: }
158:
159: /**
160: * Checks that the method has the right structure to implement
161: * <code>Constant</code>.
162: *
163: * @param method method to check
164: */
165: private void checkMethod(TreeLogger logger, JMethod method)
166: throws UnableToCompleteException {
167: if (namesToMethodCreators.get(method.getName()) != null) {
168: JParameter[] params = method.getParameters();
169: // getString() might be returning a String argument, so leave it alone.
170: if (params.length == 0) {
171: return;
172: }
173: if (params.length != 1
174: || !params[0].getType().getQualifiedSourceName()
175: .equals("java.lang.String")) {
176: String s = method
177: + " must have a single String argument.";
178: throw error(logger, s);
179: }
180: } else {
181: if (method.getParameters().length > 0) {
182: throw error(
183: logger,
184: "User-defined methods in interfaces extending ConstantsWithLookup must have no parameters and a return type of int, String, String[], ...");
185: }
186: }
187: }
188: }
|