KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > cfg > ConditionWhenTypeAssignableTo


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.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 JavaDoc assignableToTypeName;
32
33   public ConditionWhenTypeAssignableTo(String JavaDoc assignableToTypeName) {
34     this.assignableToTypeName = assignableToTypeName;
35   }
36
37   public String JavaDoc getAssignableToTypeName() {
38     return assignableToTypeName;
39   }
40
41   public String JavaDoc toString() {
42     return "<when-assignable class='" + assignableToTypeName + "'/>";
43   }
44
45   protected boolean doEval(TreeLogger logger, GeneratorContext context,
46       String JavaDoc testType) throws UnableToCompleteException {
47     TypeOracle typeOracle = context.getTypeOracle();
48
49     JClassType fromType = typeOracle.findType(testType);
50     if (fromType == null) {
51       Util.logMissingTypeErrorWithHints(logger, testType);
52       throw new UnableToCompleteException();
53     }
54
55     JClassType toType = typeOracle.findType(assignableToTypeName);
56     if (toType == null) {
57       // If we don't know the type, it can't be assignable to it.
58
// This isn't a strict failure case because stale rules can reference
59
// types that have been deleted.
60
//
61
logger.log(TreeLogger.WARN, "Unknown type '" + assignableToTypeName
62           + "' specified in deferred binding rule", null);
63       return false;
64     }
65
66     if (fromType.isAssignableTo(toType)) {
67       return true;
68     } else {
69       return false;
70     }
71   }
72
73   protected String JavaDoc getEvalAfterMessage(String JavaDoc testType, boolean result) {
74     if (result) {
75       return "Yes, the requested type was assignable";
76     } else {
77       return "No, the requested type was not assignable";
78     }
79   }
80
81   protected String JavaDoc getEvalBeforeMessage(String JavaDoc testType) {
82     return toString();
83   }
84
85 }
86
Popular Tags