KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
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(AbstractGeneratorClassCreator classCreator,
41       JType returnType) {
42     super(classCreator);
43     this.returnType = returnType;
44   }
45
46   public void createMethodFor(TreeLogger logger, JMethod targetMethod,
47       String JavaDoc value) {
48     createMethodFor(targetMethod);
49   }
50
51   void createMethodFor(JMethod targetMethod) {
52     String JavaDoc template = "{0} target = ({0})cache.get(arg0);";
53     String JavaDoc type;
54     JPrimitiveType s = returnType.isPrimitive();
55     if (s != null) {
56       type = AbstractSourceCreator.getJavaObjectTypeFor(s);
57     } else {
58       type = returnType.getQualifiedSourceName();
59     }
60     Object JavaDoc[] args = {type};
61     String JavaDoc lookup = MessageFormat.format(template, args);
62     println(lookup);
63     println("if (target != null)");
64     printReturnTarget();
65     JMethod[] methods = ((ConstantsWithLookupImplCreator) currentCreator).allInterfaceMethods;
66     for (int i = 0; i < methods.length; i++) {
67       if (methods[i].getReturnType().equals(returnType)
68           && methods[i] != targetMethod) {
69         String JavaDoc methodName = methods[i].getName();
70         String JavaDoc body = "if(arg0.equals(" + wrap(methodName) + ")){";
71         println(body);
72         printFound(methodName);
73         println("}");
74       }
75     }
76     String JavaDoc format = "throw new java.util.MissingResourceException(\"Cannot find constant ''\" + {0} + \"''; expecting a method name\", \"{1}\", {0});";
77     String JavaDoc[] throwArgs = {
78         "arg0", this.currentCreator.getTarget().getQualifiedSourceName()};
79     String JavaDoc result = MessageFormat.format(format, throwArgs);
80     println(result);
81   }
82
83   void printFound(String JavaDoc methodName) {
84     Object JavaDoc[] args2 = {methodName};
85     println(MessageFormat.format(returnTemplate(), args2));
86   }
87
88   void printReturnTarget() {
89     println("return target;");
90   }
91
92   String JavaDoc returnTemplate() {
93     return "return {0}();";
94   }
95 }
96
Popular Tags