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.GeneratorContext;
19: import com.google.gwt.core.ext.TreeLogger;
20: import com.google.gwt.core.ext.UnableToCompleteException;
21: import com.google.gwt.core.ext.typeinfo.JClassType;
22: import com.google.gwt.core.ext.typeinfo.TypeOracle;
23: import com.google.gwt.dev.util.Util;
24:
25: /**
26: * A deferred binding condition to determine whether the type being rebound is
27: * assignment-compatible with a particular type.
28: */
29: public class ConditionWhenTypeAssignableTo extends Condition {
30:
31: private final String assignableToTypeName;
32:
33: public ConditionWhenTypeAssignableTo(String assignableToTypeName) {
34: this .assignableToTypeName = assignableToTypeName;
35: }
36:
37: public String getAssignableToTypeName() {
38: return assignableToTypeName;
39: }
40:
41: public String toString() {
42: return "<when-assignable class='" + assignableToTypeName
43: + "'/>";
44: }
45:
46: protected boolean doEval(TreeLogger logger,
47: GeneratorContext context, String testType)
48: throws UnableToCompleteException {
49: TypeOracle typeOracle = context.getTypeOracle();
50:
51: JClassType fromType = typeOracle.findType(testType);
52: if (fromType == null) {
53: Util.logMissingTypeErrorWithHints(logger, testType);
54: throw new UnableToCompleteException();
55: }
56:
57: JClassType toType = typeOracle.findType(assignableToTypeName);
58: if (toType == null) {
59: // If we don't know the type, it can't be assignable to it.
60: // This isn't a strict failure case because stale rules can reference
61: // types that have been deleted.
62: //
63: logger.log(TreeLogger.WARN, "Unknown type '"
64: + assignableToTypeName
65: + "' specified in deferred binding rule", null);
66: return false;
67: }
68:
69: if (fromType.isAssignableTo(toType)) {
70: return true;
71: } else {
72: return false;
73: }
74: }
75:
76: protected String getEvalAfterMessage(String testType, boolean result) {
77: if (result) {
78: return "Yes, the requested type was assignable";
79: } else {
80: return "No, the requested type was not assignable";
81: }
82: }
83:
84: protected String getEvalBeforeMessage(String testType) {
85: return toString();
86: }
87:
88: }
|