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 com.google.gwt.core.ext.UnableToCompleteException;
019:
020: import java.lang.annotation.Annotation;
021: import java.util.HashSet;
022: import java.util.Map;
023: import java.util.Set;
024:
025: /**
026: * Type representing a Java class or interface type.
027: */
028: public abstract class JClassType extends JType implements
029: HasAnnotations, HasMetaData {
030:
031: /**
032: * Returns all of the superclasses and superinterfaces for a given type
033: * including the type itself.
034: */
035: protected static Set<JClassType> getFlattenedSuperTypeHierarchy(
036: JClassType type) {
037: Set<JClassType> typesSeen = new HashSet<JClassType>();
038: getFlattenedSuperTypeHierarchyRecursive(type, typesSeen);
039: return typesSeen;
040: }
041:
042: /**
043: * Returns the {@link JGenericType} base type if the otherType is raw or
044: * parameterized type.
045: */
046: protected static JClassType maybeGetGenericBaseType(
047: JClassType otherType) {
048: if (otherType.isParameterized() != null) {
049: return otherType.isParameterized().getBaseType();
050: } else if (otherType.isRawType() != null) {
051: return otherType.isRawType().getGenericType();
052: }
053:
054: return otherType;
055: }
056:
057: private static void getFlattenedSuperTypeHierarchyRecursive(
058: JClassType type, Set<JClassType> typesSeen) {
059: if (typesSeen.contains(type)) {
060: return;
061: }
062: typesSeen.add(type);
063:
064: // Superclass
065: JClassType super class = type.getSuperclass();
066: if (super class != null) {
067: getFlattenedSuperTypeHierarchyRecursive(super class,
068: typesSeen);
069: }
070:
071: // Check the interfaces
072: JClassType[] intfs = type.getImplementedInterfaces();
073: for (JClassType intf : intfs) {
074: getFlattenedSuperTypeHierarchyRecursive(intf, typesSeen);
075: }
076: }
077:
078: public abstract void addImplementedInterface(JClassType intf);
079:
080: public abstract void addMetaData(String tagName, String[] values);
081:
082: public abstract void addModifierBits(int bits);
083:
084: public abstract JConstructor findConstructor(JType[] paramTypes);
085:
086: public abstract JField findField(String name);
087:
088: public abstract JMethod findMethod(String name, JType[] paramTypes);
089:
090: public abstract JClassType findNestedType(String typeName);
091:
092: public abstract <T extends Annotation> T getAnnotation(
093: Class<T> annotationClass);
094:
095: public abstract Annotation[] getAnnotations();
096:
097: public abstract int getBodyEnd();
098:
099: public abstract int getBodyStart();
100:
101: public abstract CompilationUnitProvider getCompilationUnit();
102:
103: public abstract JConstructor getConstructor(JType[] paramTypes)
104: throws NotFoundException;
105:
106: public abstract JConstructor[] getConstructors();
107:
108: public abstract Annotation[] getDeclaredAnnotations();
109:
110: public abstract JClassType getEnclosingType();
111:
112: public abstract JClassType getErasedType();
113:
114: public abstract JField getField(String name);
115:
116: public abstract JField[] getFields();
117:
118: public abstract JClassType[] getImplementedInterfaces();
119:
120: public abstract String[][] getMetaData(String tagName);
121:
122: public abstract String[] getMetaDataTags();
123:
124: public abstract JMethod getMethod(String name, JType[] paramTypes)
125: throws NotFoundException;
126:
127: /*
128: * Returns the declared methods of this class (not any superclasses or
129: * superinterfaces).
130: */
131: public abstract JMethod[] getMethods();
132:
133: public abstract String getName();
134:
135: public abstract JClassType getNestedType(String typeName)
136: throws NotFoundException;
137:
138: public abstract JClassType[] getNestedTypes();
139:
140: public abstract TypeOracle getOracle();
141:
142: public abstract JMethod[] getOverloads(String name);
143:
144: /**
145: * Iterates over the most-derived declaration of each unique overridable
146: * method available in the type hierarchy of the specified type, including
147: * those found in superclasses and superinterfaces. A method is overridable if
148: * it is not <code>final</code> and its accessibility is <code>public</code>,
149: * <code>protected</code>, or package protected.
150: *
151: * Deferred binding generators often need to generate method implementations;
152: * this method offers a convenient way to find candidate methods to implement.
153: *
154: * Note that the behavior does not match
155: * {@link Class#getMethod(String, Class[])}, which does not return the most
156: * derived method in some cases.
157: *
158: * @return an array of {@link JMethod} objects representing overridable
159: * methods
160: */
161: public abstract JMethod[] getOverridableMethods();
162:
163: public abstract JPackage getPackage();
164:
165: public abstract JClassType[] getSubtypes();
166:
167: public abstract JClassType getSuperclass();
168:
169: public abstract String getTypeHash()
170: throws UnableToCompleteException;
171:
172: public abstract boolean isAbstract();
173:
174: public abstract boolean isAnnotationPresent(
175: Class<? extends Annotation> annotationClass);
176:
177: public abstract boolean isAssignableFrom(JClassType possibleSubtype);
178:
179: public abstract boolean isAssignableTo(JClassType possibleSupertype);
180:
181: /**
182: * Determines if the class can be constructed using a simple <code>new</code>
183: * operation. Specifically, the class must
184: * <ul>
185: * <li>be a class rather than an interface, </li>
186: * <li>have either no constructors or a parameterless constructor, and</li>
187: * <li>be a top-level class or a static nested class.</li>
188: * </ul>
189: *
190: * @return <code>true</code> if the type is default instantiable, or
191: * <code>false</code> otherwise
192: */
193: public abstract boolean isDefaultInstantiable();
194:
195: public abstract JGenericType isGenericType();
196:
197: @Override
198: public abstract JClassType isInterface();
199:
200: /**
201: * Tests if this type is a local type (within a method).
202: *
203: * @return true if this type is a local type, whether it is named or
204: * anonymous.
205: */
206: public abstract boolean isLocalType();
207:
208: /**
209: * Tests if this type is contained within another type.
210: *
211: * @return true if this type has an enclosing type, false if this type is a
212: * top-level type
213: */
214: public abstract boolean isMemberType();
215:
216: public abstract boolean isPrivate();
217:
218: public abstract boolean isProtected();
219:
220: public abstract boolean isPublic();
221:
222: public abstract boolean isStatic();
223:
224: public abstract void setSuperclass(JClassType type);
225:
226: @Override
227: public String toString() {
228: return this .getQualifiedSourceName();
229: }
230:
231: protected abstract void acceptSubtype(JClassType me);
232:
233: protected abstract int getModifierBits();
234:
235: protected abstract void getOverridableMethodsOnSuperclassesAndThisClass(
236: Map<String, JMethod> methodsBySignature);
237:
238: /**
239: * Gets the methods declared in interfaces that this type extends. If this
240: * type is a class, its own methods are not added. If this type is an
241: * interface, its own methods are added. Used internally by
242: * {@link #getOverridableMethods()}.
243: *
244: * @param methodsBySignature
245: */
246: protected abstract void getOverridableMethodsOnSuperinterfacesAndMaybeThisInterface(
247: Map<String, JMethod> methodsBySignature);
248:
249: protected final String makeCompoundName(JClassType type) {
250: if (type.getEnclosingType() == null) {
251: return type.getSimpleSourceName();
252: } else {
253: return makeCompoundName(type.getEnclosingType()) + "."
254: + type.getSimpleSourceName();
255: }
256: }
257:
258: /**
259: * Tells this type's superclasses and superinterfaces about it.
260: */
261: protected abstract void notifySuperTypesOf(JClassType me);
262:
263: protected abstract void removeSubtype(JClassType me);
264:
265: abstract void addConstructor(JConstructor ctor);
266:
267: abstract void addField(JField field);
268:
269: abstract void addMethod(JMethod method);
270:
271: abstract void addNestedType(JClassType type);
272:
273: abstract JClassType findNestedTypeImpl(String[] typeName, int index);
274:
275: @Override
276: abstract JClassType getSubstitutedType(
277: JParameterizedType parameterizedType);
278:
279: abstract void notifySuperTypes();
280:
281: /**
282: * Removes references to this instance from all of its super types.
283: */
284: abstract void removeFromSupertypes();
285: }
|