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.JType;
023: import com.google.gwt.core.ext.typeinfo.NotFoundException;
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.SourceWriter;
028:
029: import java.util.Map;
030:
031: /**
032: * Creates the class implementation for a given resource bundle using the
033: * standard <code>AbstractGeneratorClassCreator</code>.
034: */
035: class ConstantsImplCreator extends AbstractLocalizableImplCreator {
036: /**
037: * Does a Map need to be generated in order to store complex results?
038: */
039: private boolean needCache = false;
040:
041: /**
042: * Constructor for <code>ConstantsImplCreator</code>.
043: *
044: * @param logger logger to print errors
045: * @param writer <code>Writer</code> to print to
046: * @param localizableClass class/interface to conform to
047: * @param messageBindings resource bundle used to generate the class
048: * @param oracle types
049: * @throws UnableToCompleteException
050: */
051: public ConstantsImplCreator(TreeLogger logger, SourceWriter writer,
052: JClassType localizableClass,
053: AbstractResource messageBindings, TypeOracle oracle)
054: throws UnableToCompleteException {
055: super (writer, localizableClass, messageBindings);
056: try {
057: JClassType stringClass = oracle.getType(String.class
058: .getName());
059: JClassType mapClass = oracle.getType(Map.class.getName());
060: JType stringArrayClass = oracle.getArrayType(stringClass);
061: JType intClass = oracle.parse(int.class.getName());
062: JType doubleClass = oracle.parse(double.class.getName());
063: JType floatClass = oracle.parse(float.class.getName());
064: JType booleanClass = oracle.parse(boolean.class.getName());
065: register(stringClass, new SimpleValueMethodCreator(this ,
066: SimpleValueMethodCreator.STRING));
067: register(mapClass, new ConstantsMapMethodCreator(this ));
068: register(intClass, new SimpleValueMethodCreator(this ,
069: SimpleValueMethodCreator.INT));
070: register(doubleClass, new SimpleValueMethodCreator(this ,
071: SimpleValueMethodCreator.DOUBLE));
072: register(floatClass, new SimpleValueMethodCreator(this ,
073: SimpleValueMethodCreator.FLOAT));
074: register(booleanClass, new SimpleValueMethodCreator(this ,
075: SimpleValueMethodCreator.BOOLEAN));
076:
077: register(stringArrayClass,
078: new ConstantsStringArrayMethodCreator(this ));
079: } catch (NotFoundException e) {
080: throw error(logger, e);
081: } catch (TypeOracleException e) {
082: throw error(logger, e);
083: }
084: }
085:
086: @Override
087: protected void classEpilog() {
088: if (isNeedCache()) {
089: getWriter().println(
090: "java.util.Map cache = new java.util.HashMap();"
091: .toString());
092: }
093: }
094:
095: /**
096: * Create the method body associated with the given method. Arguments are
097: * arg0...argN.
098: */
099: @Override
100: protected void emitMethodBody(TreeLogger logger, JMethod method)
101: throws UnableToCompleteException {
102: checkConstantMethod(logger, method);
103: delegateToCreator(logger, method);
104: }
105:
106: boolean isNeedCache() {
107: return needCache;
108: }
109:
110: void setNeedCache(boolean needCache) {
111: this .needCache = needCache;
112: }
113:
114: /**
115: * Checks that the method has the right structure to implement
116: * <code>Constant</code>.
117: *
118: * @param method method to check
119: */
120: private void checkConstantMethod(TreeLogger logger, JMethod method)
121: throws UnableToCompleteException {
122: if (method.getParameters().length > 0) {
123: String s = "Methods in interfaces extending Constant must have no parameters and a return type of String/int/float/boolean/double/String[]/Map";
124: throw error(logger, s);
125: }
126: }
127: }
|