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.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.NotFoundException;
23: import com.google.gwt.core.ext.typeinfo.TypeOracle;
24: import com.google.gwt.i18n.rebind.util.AbstractResource;
25: import com.google.gwt.user.rebind.SourceWriter;
26:
27: /**
28: * Creates the class implementation for a given resource bundle using the
29: * standard <code>AbstractGeneratorClassCreator</code>.
30: */
31: class MessagesImplCreator extends AbstractLocalizableImplCreator {
32: /**
33: * Constructor for <code>ConstantsImplCreator</code>.
34: *
35: * @param writer <code>Writer</code> to print to
36: * @param localizableClass Class/Interface to conform to
37: * @param messageBindings resource bundle used to generate the class
38: * @param oracle types
39: * @param logger logger to print errors
40: * @throws UnableToCompleteException
41: */
42: public MessagesImplCreator(TreeLogger logger, SourceWriter writer,
43: JClassType localizableClass,
44: AbstractResource messageBindings, TypeOracle oracle)
45: throws UnableToCompleteException {
46: super (writer, localizableClass, messageBindings);
47: try {
48: JClassType stringClass = oracle.getType(String.class
49: .getName());
50: register(stringClass, new MessagesMethodCreator(this ));
51: } catch (NotFoundException e) {
52: // never expect this error in practice
53: throw error(logger, e);
54: }
55: }
56:
57: /**
58: * Checks that the method has the right structure to implement
59: * <code>Messages</code>.
60: *
61: * @param method
62: * @throws UnableToCompleteException
63: */
64: private void checkMessagesMethod(TreeLogger logger, JMethod method)
65: throws UnableToCompleteException {
66: if (!method.getReturnType().getQualifiedSourceName().equals(
67: "java.lang.String")) {
68: throw error(
69: logger,
70: "All methods in interfaces extending Messages must have a return type of String.");
71: }
72: }
73:
74: /**
75: * Create the method body associated with the given method. Arguments are
76: * arg0...argN.
77: *
78: * @param m method to emit
79: * @throws UnableToCompleteException
80: */
81: @Override
82: protected void emitMethodBody(TreeLogger logger, JMethod m)
83: throws UnableToCompleteException {
84: checkMessagesMethod(logger, m);
85: delegateToCreator(logger, m);
86: }
87: }
|