01: /*
02: * Copyright 2006 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.dev.cfg;
17:
18: import com.google.gwt.core.ext.Generator;
19: import com.google.gwt.core.ext.GeneratorContext;
20: import com.google.gwt.core.ext.TreeLogger;
21: import com.google.gwt.core.ext.UnableToCompleteException;
22:
23: /**
24: * A rule to replace the type being rebound with a class whose name is
25: * determined by a generator class. Generators usually generate new classes
26: * during the deferred binding process, but it is not required.
27: */
28: public class RuleGenerateWith extends Rule {
29:
30: private final Generator generator;
31:
32: public RuleGenerateWith(Generator generator) {
33: this .generator = generator;
34: }
35:
36: public String realize(TreeLogger logger, GeneratorContext context,
37: String typeName) throws UnableToCompleteException {
38:
39: String msg = "Invoking " + toString();
40: logger = logger.branch(TreeLogger.DEBUG, msg, null);
41:
42: long before = System.currentTimeMillis();
43: String className = generator
44: .generate(logger, context, typeName);
45: long after = System.currentTimeMillis();
46: if (className == null) {
47: msg = "Generator returned null, so the requested type will be used as is";
48: } else {
49: msg = "Generator returned class '" + className + "'";
50: }
51: logger.log(TreeLogger.DEBUG, msg, null);
52:
53: msg = "Finished in " + (after - before) + " ms";
54: logger.log(TreeLogger.DEBUG, msg, null);
55:
56: return className;
57: }
58:
59: public String toString() {
60: return "<generate-with class='"
61: + generator.getClass().getName() + "'/>";
62: }
63: }
|