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.core.ext.typeinfo;
17:
18: /**
19: * Represents the extends bound used in {@link JTypeParameter}s and
20: * {@link JWildcardType}s.
21: */
22: public class JUpperBound extends JBound {
23: public JUpperBound(JClassType upperBound) {
24: this (new JClassType[] { upperBound });
25: }
26:
27: /**
28: *
29: */
30: public JUpperBound(JClassType[] upperBounds) {
31: super (upperBounds);
32: }
33:
34: @Override
35: public JLowerBound isLowerBound() {
36: return null;
37: }
38:
39: @Override
40: public JUpperBound isUpperBound() {
41: return this ;
42: }
43:
44: @Override
45: JClassType[] getSubtypes() {
46: return getFirstBound().getSubtypes();
47: }
48:
49: @Override
50: boolean isAssignableFrom(JClassType otherType) {
51: JWildcardType wildcard = otherType.isWildcard();
52: if (wildcard != null) {
53: if (wildcard.getBounds().isLowerBound() != null) {
54: /*
55: * Upper bounds can be assigned from lower bounds if both of their
56: * bounding types are Object.
57: */
58: JClassType firstBound = getFirstBound();
59: JClassType javaLangObject = firstBound.getOracle()
60: .getJavaLangObject();
61: return firstBound == javaLangObject
62: && wildcard.getFirstBound() == javaLangObject;
63: }
64:
65: return getFirstBound().isAssignableFrom(
66: wildcard.getFirstBound());
67: } else {
68: /*
69: * This bound can be assigned from another type if each of the bounding
70: * types is assignable from the other type.
71: */
72: JClassType[] bounds = getBounds();
73: for (JClassType bound : bounds) {
74: if (!bound.isAssignableFrom(otherType)) {
75: return false;
76: }
77: }
78: }
79:
80: return true;
81: }
82:
83: @Override
84: boolean isAssignableTo(JClassType otherType) {
85: JClassType[] bounds = getBounds();
86: for (JClassType bound : bounds) {
87: if (bound.isAssignableTo(otherType)) {
88: return true;
89: }
90: }
91:
92: return false;
93: }
94: }
|