01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.typeinfo.JMethod;
20: import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
21: import com.google.gwt.core.ext.typeinfo.JType;
22: import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
23: import com.google.gwt.user.rebind.AbstractMethodCreator;
24: import com.google.gwt.user.rebind.AbstractSourceCreator;
25:
26: import java.text.MessageFormat;
27:
28: /**
29: * Method creator to call the correct Map for the given Dictionary.
30: */
31: class LookupMethodCreator extends AbstractMethodCreator {
32: private JType returnType;
33:
34: /**
35: * Constructor for <code>LookupMethodCreator</code>.
36: *
37: * @param classCreator parent class creator
38: * @param returnType associated return type
39: */
40: public LookupMethodCreator(
41: AbstractGeneratorClassCreator classCreator, JType returnType) {
42: super (classCreator);
43: this .returnType = returnType;
44: }
45:
46: @Override
47: public void createMethodFor(TreeLogger logger,
48: JMethod targetMethod, String value) {
49: createMethodFor(targetMethod);
50: }
51:
52: void createMethodFor(JMethod targetMethod) {
53: String template = "{0} target = ({0})cache.get(arg0);";
54: String type;
55: JPrimitiveType s = returnType.isPrimitive();
56: if (s != null) {
57: type = AbstractSourceCreator.getJavaObjectTypeFor(s);
58: } else {
59: type = returnType.getQualifiedSourceName();
60: }
61: Object[] args = { type };
62: String lookup = MessageFormat.format(template, args);
63: println(lookup);
64: println("if (target != null)");
65: printReturnTarget();
66: JMethod[] methods = ((ConstantsWithLookupImplCreator) currentCreator).allInterfaceMethods;
67: for (int i = 0; i < methods.length; i++) {
68: if (methods[i].getReturnType().equals(returnType)
69: && methods[i] != targetMethod) {
70: String methodName = methods[i].getName();
71: String body = "if(arg0.equals(" + wrap(methodName)
72: + ")){";
73: println(body);
74: printFound(methodName);
75: println("}");
76: }
77: }
78: String format = "throw new java.util.MissingResourceException(\"Cannot find constant ''\" + {0} + \"''; expecting a method name\", \"{1}\", {0});";
79: String result = MessageFormat.format(format, "arg0",
80: this .currentCreator.getTarget()
81: .getQualifiedSourceName());
82: println(result);
83: }
84:
85: void printFound(String methodName) {
86: Object[] args2 = { methodName };
87: println(MessageFormat.format(returnTemplate(), args2));
88: }
89:
90: void printReturnTarget() {
91: println("return target;");
92: }
93:
94: String returnTemplate() {
95: return "return {0}();";
96: }
97: }
|