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: import java.util.Set;
021:
022: /**
023: *
024: */
025: public class JGenericType extends JRealClassType implements
026: HasTypeParameters {
027: /**
028: * Returns <code>true</code> if lhsType is assignable to rhsType.
029: */
030: private static boolean isAssignable(JClassType lhsType,
031: JClassType rhsType) {
032: if (lhsType == rhsType) {
033: return true;
034: }
035:
036: Set<JClassType> super types = getFlattenedSuperTypeHierarchy(rhsType);
037: for (JClassType super type : super types) {
038: if (super type.isParameterized() == null) {
039: continue;
040: }
041:
042: if (super type.isParameterized().getBaseType() == lhsType) {
043: return true;
044: }
045: }
046:
047: return false;
048: }
049:
050: private JRawType lazyRawType = null;
051:
052: private final List<JTypeParameter> typeParams = new ArrayList<JTypeParameter>();
053:
054: public JGenericType(TypeOracle oracle, CompilationUnitProvider cup,
055: JPackage declaringPackage, JClassType enclosingType,
056: boolean isLocalType, String name, int declStart,
057: int declEnd, int bodyStart, int bodyEnd, boolean isInterface) {
058: super (oracle, cup, declaringPackage, enclosingType,
059: isLocalType, name, declStart, declEnd, bodyStart,
060: bodyEnd, isInterface);
061: }
062:
063: @Override
064: public JClassType getErasedType() {
065: return getRawType();
066: }
067:
068: @Override
069: public String getParameterizedQualifiedSourceName() {
070: StringBuffer sb = new StringBuffer();
071:
072: if (getEnclosingType() != null) {
073: sb.append(getEnclosingType()
074: .getParameterizedQualifiedSourceName());
075: sb.append(".");
076: sb.append(getSimpleSourceName());
077: } else {
078: sb.append(getQualifiedSourceName());
079: }
080:
081: sb.append('<');
082: boolean needComma = false;
083: for (JClassType typeParam : typeParams) {
084: if (needComma) {
085: sb.append(", ");
086: } else {
087: needComma = true;
088: }
089: sb.append(typeParam.getParameterizedQualifiedSourceName());
090: }
091: sb.append('>');
092: return sb.toString();
093: }
094:
095: public JRawType getRawType() {
096: if (lazyRawType == null) {
097: lazyRawType = new JRawType(this , getEnclosingType());
098: }
099:
100: return lazyRawType;
101: }
102:
103: public JTypeParameter[] getTypeParameters() {
104: return typeParams
105: .toArray(new JTypeParameter[typeParams.size()]);
106: }
107:
108: @Override
109: public boolean isAssignableFrom(JClassType otherType) {
110: otherType = maybeGetGenericBaseType(otherType);
111:
112: return isAssignable(this , otherType);
113: }
114:
115: @Override
116: public boolean isAssignableTo(JClassType otherType) {
117: otherType = maybeGetGenericBaseType(otherType);
118:
119: return isAssignable(otherType, this );
120: }
121:
122: @Override
123: public JGenericType isGenericType() {
124: return this ;
125: }
126:
127: @Override
128: public String toString() {
129: if (isInterface() != null) {
130: return "interface " + getParameterizedQualifiedSourceName();
131: }
132:
133: return "class " + getParameterizedQualifiedSourceName();
134: }
135:
136: void addTypeParameter(JTypeParameter typeParameter) {
137: typeParams.add(typeParameter);
138: }
139: }
|