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: import java.util.ArrayList;
19: import java.util.Arrays;
20: import java.util.List;
21:
22: /**
23: * Represents a bound for a {@link JTypeParameter} or a {@link JWildcardType}.
24: */
25: public abstract class JBound {
26: /**
27: * Types which make up this bound.
28: */
29: private final List<JClassType> bounds = new ArrayList<JClassType>();
30:
31: JBound(JClassType[] bounds) {
32: this .bounds.addAll(Arrays.asList(bounds));
33: }
34:
35: public JClassType[] getBounds() {
36: return bounds.toArray(TypeOracle.NO_JCLASSES);
37: }
38:
39: public JClassType getFirstBound() {
40: assert (!bounds.isEmpty());
41:
42: return bounds.get(0);
43: }
44:
45: public String getQualifiedSourceName() {
46: return toString(true);
47: }
48:
49: public String getSimpleSourceName() {
50: return toString(false);
51: }
52:
53: public abstract JLowerBound isLowerBound();
54:
55: public abstract JUpperBound isUpperBound();
56:
57: @Override
58: public String toString() {
59: return getQualifiedSourceName();
60: }
61:
62: abstract JClassType[] getSubtypes();
63:
64: abstract boolean isAssignableFrom(JClassType otherType);
65:
66: abstract boolean isAssignableTo(JClassType otherType);
67:
68: private String toString(boolean useQualifiedNames) {
69: StringBuffer sb = new StringBuffer();
70:
71: if (isUpperBound() != null) {
72: sb.append(" extends ");
73: } else {
74: sb.append(" super ");
75: }
76:
77: boolean needsAmpersand = false;
78: for (JClassType bound : bounds) {
79:
80: if (needsAmpersand) {
81: sb.append(" & ");
82: } else {
83: needsAmpersand = true;
84: }
85:
86: String name;
87: if (useQualifiedNames) {
88: name = bound.getParameterizedQualifiedSourceName();
89: } else {
90: name = bound.getSimpleSourceName();
91: }
92:
93: sb.append(name);
94: }
95:
96: return sb.toString();
97: }
98: }
|