001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: /**
022: * Represents one of the type parameters in a generic type.
023: */
024: public class JTypeParameter extends JDelegatingClassType implements
025: HasBounds {
026: private JBound bounds;
027: private final JGenericType declaringClass;
028: private final JAbstractMethod declaringMethod;
029: private final int ordinal;
030: private final String typeName;
031:
032: public JTypeParameter(String typeName,
033: JAbstractMethod declaringMethod, int ordinal) {
034: this .typeName = typeName;
035: this .declaringMethod = declaringMethod;
036: this .declaringClass = null;
037: this .ordinal = ordinal;
038: declaringMethod.addTypeParameter(this );
039: }
040:
041: public JTypeParameter(String typeName, JGenericType declaringClass,
042: int ordinal) {
043: this .typeName = typeName;
044: this .declaringClass = declaringClass;
045: this .declaringMethod = null;
046: this .ordinal = ordinal;
047: declaringClass.addTypeParameter(this );
048: }
049:
050: @Override
051: public JField findField(String name) {
052: return getBaseType().findField(name);
053: }
054:
055: @Override
056: public JMethod findMethod(String name, JType[] paramTypes) {
057: return getBaseType().findMethod(name, paramTypes);
058: }
059:
060: public JBound getBounds() {
061: return bounds;
062: }
063:
064: public JRealClassType getDeclaringClass() {
065: return declaringClass;
066: }
067:
068: @Override
069: public JField getField(String name) {
070: return getBaseType().getField(name);
071: }
072:
073: @Override
074: public JField[] getFields() {
075: return getBaseType().getFields();
076: }
077:
078: public JClassType getFirstBound() {
079: return getBaseType();
080: }
081:
082: @Override
083: public JMethod getMethod(String name, JType[] paramTypes)
084: throws NotFoundException {
085: return getBaseType().getMethod(name, paramTypes);
086: }
087:
088: @Override
089: public JMethod[] getMethods() {
090: return getBaseType().getMethods();
091: }
092:
093: @Override
094: public String getName() {
095: return typeName;
096: }
097:
098: @Override
099: public String getParameterizedQualifiedSourceName() {
100: return typeName;
101: }
102:
103: @Override
104: public String getQualifiedSourceName() {
105: return typeName + bounds.getQualifiedSourceName();
106: }
107:
108: @Override
109: public String getSimpleSourceName() {
110: return typeName + bounds.getSimpleSourceName();
111: }
112:
113: @Override
114: public JClassType[] getSubtypes() {
115: JClassType[] subtypes = super .getSubtypes();
116: List<JClassType> intersectionTypes = new ArrayList<JClassType>();
117:
118: if (getFirstBound().isInterface() == null
119: && isAssignableFrom(getFirstBound())) {
120: // Include the first bound as a subtype if it is not an interface and it
121: // is assignable to all of our bounds.
122: intersectionTypes.add(getFirstBound());
123: }
124:
125: for (JClassType subtype : subtypes) {
126: if (isAssignableFrom(subtype)) {
127: intersectionTypes.add(subtype);
128: }
129: }
130:
131: // Only types that intersect with all our bounds make it here.
132: return intersectionTypes.toArray(TypeOracle.NO_JCLASSES);
133: }
134:
135: @Override
136: public boolean isAssignableFrom(JClassType otherType) {
137: if (otherType == this ) {
138: return true;
139: }
140:
141: return getBounds().isAssignableFrom(otherType);
142: }
143:
144: @Override
145: public boolean isAssignableTo(JClassType otherType) {
146: if (otherType == this ) {
147: return true;
148: }
149:
150: return getBounds().isAssignableTo(otherType);
151: }
152:
153: @Override
154: public JGenericType isGenericType() {
155: return null;
156: }
157:
158: @Override
159: public JParameterizedType isParameterized() {
160: return null;
161: }
162:
163: @Override
164: public JRawType isRawType() {
165: return null;
166: }
167:
168: @Override
169: public JTypeParameter isTypeParameter() {
170: return this ;
171: }
172:
173: @Override
174: public JWildcardType isWildcard() {
175: return null;
176: }
177:
178: public void setBounds(JBound bounds) {
179: this .bounds = bounds;
180: super .setBaseType(bounds.getFirstBound());
181: }
182:
183: @Override
184: public String toString() {
185: if (getBaseType().isInterface() != null) {
186: return "interface " + getQualifiedSourceName();
187: } else {
188: return "class " + getQualifiedSourceName();
189: }
190: }
191:
192: int getOrdinal() {
193: return ordinal;
194: }
195:
196: @Override
197: JClassType getSubstitutedType(JParameterizedType parameterizedType) {
198: return parameterizedType.getTypeParameterSubstitution(this);
199: }
200: }
|