KickJava   Java API By Example, From Geeks To Geeks.

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


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.JType;
23 import com.google.gwt.core.ext.typeinfo.NotFoundException;
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.SourceWriter;
28
29 import java.util.Map JavaDoc;
30
31 /**
32  * Creates the class implementation for a given resource bundle using the
33  * standard <code>AbstractGeneratorClassCreator</code>.
34  */

35 class ConstantsImplCreator extends AbstractLocalizableImplCreator {
36   /**
37    * Does a Map need to be generated in order to store complex results?
38    */

39   private boolean needCache = false;
40
41   /**
42    * Constructor for <code>ConstantsImplCreator</code>.
43    *
44    * @param logger logger to print errors
45    * @param writer <code>Writer</code> to print to
46    * @param localizableClass class/interface to conform to
47    * @param messageBindings resource bundle used to generate the class
48    * @param oracle types
49    * @throws UnableToCompleteException
50    */

51   public ConstantsImplCreator(TreeLogger logger, SourceWriter writer,
52       JClassType localizableClass, AbstractResource messageBindings,
53       TypeOracle oracle) throws UnableToCompleteException {
54     super(writer, localizableClass, messageBindings);
55     try {
56       JClassType stringClass = oracle.getType(String JavaDoc.class.getName());
57       JClassType mapClass = oracle.getType(Map JavaDoc.class.getName());
58       JType stringArrayClass = oracle.getArrayType(stringClass);
59       JType intClass = oracle.parse(int.class.getName());
60       JType doubleClass = oracle.parse(double.class.getName());
61       JType floatClass = oracle.parse(float.class.getName());
62       JType booleanClass = oracle.parse(boolean.class.getName());
63       register(stringClass, new SimpleValueMethodCreator(this,
64           SimpleValueMethodCreator.STRING));
65       register(mapClass, new ConstantsMapMethodCreator(this));
66       register(intClass, new SimpleValueMethodCreator(this,
67           SimpleValueMethodCreator.INT));
68       register(doubleClass, new SimpleValueMethodCreator(this,
69           SimpleValueMethodCreator.DOUBLE));
70       register(floatClass, new SimpleValueMethodCreator(this,
71           SimpleValueMethodCreator.FLOAT));
72       register(booleanClass, new SimpleValueMethodCreator(this,
73           SimpleValueMethodCreator.BOOLEAN));
74
75       register(stringArrayClass, new ConstantsStringArrayMethodCreator(this));
76     } catch (NotFoundException e) {
77       throw error(logger, e);
78     } catch (TypeOracleException e) {
79       throw error(logger, e);
80     }
81   }
82
83   protected void classEpilog() {
84     if (isNeedCache()) {
85       getWriter().println(
86           "java.util.Map cache = new java.util.HashMap();".toString());
87     }
88   }
89
90   /**
91    * Create the method body associated with the given method. Arguments are
92    * arg0...argN.
93    */

94   protected void emitMethodBody(TreeLogger logger, JMethod method)
95       throws UnableToCompleteException {
96     checkConstantMethod(logger, method);
97     delegateToCreator(logger, method);
98   }
99
100   boolean isNeedCache() {
101     return needCache;
102   }
103
104   void setNeedCache(boolean needCache) {
105     this.needCache = needCache;
106   }
107
108   /**
109    * Checks that the method has the right structure to implement
110    * <code>Constant</code>.
111    *
112    * @param method method to check
113    */

114   private void checkConstantMethod(TreeLogger logger, JMethod method)
115       throws UnableToCompleteException {
116     if (method.getParameters().length > 0) {
117       String JavaDoc s = "Methods in interfaces extending Constant must have no parameters and a return type of String/int/float/boolean/double/String[]/Map";
118       throw error(logger, s);
119     }
120   }
121 }
122
Popular Tags