duke@1: /* duke@1: * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.util; duke@1: duke@1: import com.sun.javadoc.*; duke@1: import java.util.*; duke@1: duke@1: /** duke@1: * Map all class uses for a given class. duke@1: * duke@1: * This code is not part of an API. duke@1: * It is implementation that is subject to change. duke@1: * Do not use it as an API duke@1: * duke@1: * @since 1.2 duke@1: * @author Robert G. Field duke@1: */ duke@1: public class ClassUseMapper { duke@1: duke@1: private final ClassTree classtree; duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to set of PackageDoc used by that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToPackage = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of Annotations to set of PackageDoc that use the annotation. duke@1: */ duke@1: public Map classToPackageAnnotations = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to set of ClassDoc used by that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToClass = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of ClassDoc which are direct or duke@1: * indirect subclasses of that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToSubclass = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of ClassDoc which are direct or duke@1: * indirect subinterfaces of that interface. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToSubinterface = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of ClassDoc which implement duke@1: * this interface. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToImplementingClass = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of FieldDoc declared as that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToField = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of MethodDoc returning that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToMethodReturn = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of MethodDoc having that class duke@1: * as an arg. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToMethodArgs = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of MethodDoc which throws that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToMethodThrows = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of ConstructorDoc having that class duke@1: * as an arg. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToConstructorArgs = new HashMap(); duke@1: duke@1: /** duke@1: * Mapping of ClassDocs to list of ConstructorDoc which throws that class. duke@1: * Entries may be null. duke@1: */ duke@1: public Map classToConstructorThrows = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to constructors that use them. duke@1: */ duke@1: public Map classToConstructorAnnotations = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to Constructor parameters that use them. duke@1: */ duke@1: public Map classToConstructorParamAnnotation = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to Constructor arguments that use them as type parameters. duke@1: */ duke@1: public Map classToConstructorDocArgTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to ClassDocs that use them as type parameters. duke@1: */ duke@1: public Map classToClassTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to ClassDocs that use them. duke@1: */ duke@1: public Map classToClassAnnotations = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to ExecutableMemberDocs that use them as type parameters. duke@1: */ duke@1: public Map classToExecMemberDocTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to ExecutableMemberDocs arguments that use them as type parameters. duke@1: */ duke@1: public Map classToExecMemberDocArgTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to ExecutableMemberDocs that use them. duke@1: */ duke@1: public Map classToExecMemberDocAnnotations = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to ExecutableMemberDocs that have return type duke@1: * with type parameters of that class. duke@1: */ duke@1: public Map classToExecMemberDocReturnTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to MethodDoc parameters that use them. duke@1: */ duke@1: public Map classToExecMemberDocParamAnnotation = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of ClassDocs to FieldDocs that use them as type parameters. duke@1: */ duke@1: public Map classToFieldDocTypeParam = new HashMap(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to FieldDocs that use them. duke@1: */ duke@1: public Map annotationToFieldDoc = new HashMap(); duke@1: duke@1: duke@1: public ClassUseMapper(RootDoc root, ClassTree classtree) { duke@1: this.classtree = classtree; duke@1: duke@1: // Map subclassing, subinterfacing implementing, ... duke@1: for (Iterator it = classtree.baseclasses().iterator(); it.hasNext();) { duke@1: subclasses((ClassDoc)it.next()); duke@1: } duke@1: for (Iterator it = classtree.baseinterfaces().iterator(); it.hasNext();) { duke@1: // does subinterfacing as side-effect duke@1: implementingClasses((ClassDoc)it.next()); duke@1: } duke@1: // Map methods, fields, constructors using a class. duke@1: ClassDoc[] classes = root.classes(); duke@1: for (int i = 0; i < classes.length; i++) { duke@1: PackageDoc pkg = classes[i].containingPackage(); duke@1: mapAnnotations(classToPackageAnnotations, pkg, pkg); duke@1: ClassDoc cd = classes[i]; duke@1: mapTypeParameters(classToClassTypeParam, cd, cd); duke@1: mapAnnotations(classToClassAnnotations, cd, cd); duke@1: FieldDoc[] fields = cd.fields(); duke@1: for (int j = 0; j < fields.length; j++) { duke@1: FieldDoc fd = fields[j]; duke@1: mapTypeParameters(classToFieldDocTypeParam, fd, fd); duke@1: mapAnnotations(annotationToFieldDoc, fd, fd); duke@1: if (! fd.type().isPrimitive()) { duke@1: add(classToField, fd.type().asClassDoc(), fd); duke@1: } duke@1: } duke@1: ConstructorDoc[] cons = cd.constructors(); duke@1: for (int j = 0; j < cons.length; j++) { duke@1: mapAnnotations(classToConstructorAnnotations, cons[j], cons[j]); duke@1: mapExecutable(cons[j]); duke@1: } duke@1: MethodDoc[] meths = cd.methods(); duke@1: for (int j = 0; j < meths.length; j++) { duke@1: MethodDoc md = meths[j]; duke@1: mapExecutable(md); duke@1: mapTypeParameters(classToExecMemberDocTypeParam, md, md); duke@1: mapAnnotations(classToExecMemberDocAnnotations, md, md); duke@1: if (! (md.returnType().isPrimitive() || md.returnType() instanceof TypeVariable)) { duke@1: mapTypeParameters(classToExecMemberDocReturnTypeParam, duke@1: md.returnType(), md); duke@1: add(classToMethodReturn, md.returnType().asClassDoc(), md); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Return all subclasses of a class AND fill-in classToSubclass map. duke@1: */ duke@1: private Collection subclasses(ClassDoc cd) { duke@1: Collection ret = (Collection)classToSubclass.get(cd.qualifiedName()); duke@1: if (ret == null) { duke@1: ret = new TreeSet(); duke@1: List subs = classtree.subclasses(cd); duke@1: if (subs != null) { duke@1: ret.addAll(subs); duke@1: for (Iterator it = subs.iterator(); it.hasNext();) { duke@1: ret.addAll(subclasses((ClassDoc)it.next())); duke@1: } duke@1: } duke@1: addAll(classToSubclass, cd, ret); duke@1: } duke@1: return ret; duke@1: } duke@1: duke@1: /** duke@1: * Return all subinterfaces of an interface AND fill-in classToSubinterface map. duke@1: */ duke@1: private Collection subinterfaces(ClassDoc cd) { duke@1: Collection ret = (Collection)classToSubinterface.get(cd.qualifiedName()); duke@1: if (ret == null) { duke@1: ret = new TreeSet(); duke@1: List subs = classtree.subinterfaces(cd); duke@1: if (subs != null) { duke@1: ret.addAll(subs); duke@1: for (Iterator it = subs.iterator(); it.hasNext();) { duke@1: ret.addAll(subinterfaces((ClassDoc)it.next())); duke@1: } duke@1: } duke@1: addAll(classToSubinterface, cd, ret); duke@1: } duke@1: return ret; duke@1: } duke@1: duke@1: /** duke@1: * Return all implementing classes of an interface (including duke@1: * all subclasses of implementing classes and all classes duke@1: * implementing subinterfaces) AND fill-in both classToImplementingClass duke@1: * and classToSubinterface maps. duke@1: */ duke@1: private Collection implementingClasses(ClassDoc cd) { duke@1: Collection ret = (List)classToImplementingClass.get(cd.qualifiedName()); duke@1: if (ret == null) { duke@1: ret = new TreeSet(); duke@1: List impl = classtree.implementingclasses(cd); duke@1: if (impl != null) { duke@1: ret.addAll(impl); duke@1: for (Iterator it = impl.iterator(); it.hasNext();) { duke@1: ret.addAll(subclasses((ClassDoc)it.next())); duke@1: } duke@1: } duke@1: for (Iterator it = subinterfaces(cd).iterator(); it.hasNext();) { duke@1: ret.addAll(implementingClasses((ClassDoc)it.next())); duke@1: } duke@1: addAll(classToImplementingClass, cd, ret); duke@1: } duke@1: return ret; duke@1: } duke@1: duke@1: /** duke@1: * Determine classes used by a method or constructor, so they can be duke@1: * inverse mapped. duke@1: */ duke@1: private void mapExecutable(ExecutableMemberDoc em) { duke@1: Parameter[] params = em.parameters(); duke@1: boolean isConstructor = em.isConstructor(); duke@1: List classArgs = new ArrayList(); duke@1: for (int k = 0; k < params.length; k++) { duke@1: Type pcd = params[k].type(); duke@1: // primitives don't get mapped, also avoid dups duke@1: if ((! params[k].type().isPrimitive()) && duke@1: ! classArgs.contains(pcd) && duke@1: ! (pcd instanceof TypeVariable)) { duke@1: add(isConstructor? classToConstructorArgs :classToMethodArgs, duke@1: pcd.asClassDoc(), em); duke@1: classArgs.add(pcd); duke@1: mapTypeParameters(isConstructor? duke@1: classToConstructorDocArgTypeParam : classToExecMemberDocArgTypeParam, duke@1: pcd, em); duke@1: } duke@1: mapAnnotations( duke@1: isConstructor ? duke@1: classToConstructorParamAnnotation : duke@1: classToExecMemberDocParamAnnotation, duke@1: params[k], em); duke@1: } duke@1: ClassDoc[] thr = em.thrownExceptions(); duke@1: for (int k = 0; k < thr.length; k++) { duke@1: add(isConstructor? classToConstructorThrows : classToMethodThrows, duke@1: thr[k], em); duke@1: } duke@1: } duke@1: duke@1: private List refList(Map map, ClassDoc cd) { duke@1: List list = (List)map.get(cd.qualifiedName()); duke@1: if (list == null) { duke@1: list = new ArrayList(); duke@1: map.put(cd.qualifiedName(), list); duke@1: } duke@1: return list; duke@1: } duke@1: duke@1: private Set packageSet(ClassDoc cd) { duke@1: Set pkgSet = (Set)classToPackage.get(cd.qualifiedName()); duke@1: if (pkgSet == null) { duke@1: pkgSet = new TreeSet(); duke@1: classToPackage.put(cd.qualifiedName(), pkgSet); duke@1: } duke@1: return pkgSet; duke@1: } duke@1: duke@1: private Set classSet(ClassDoc cd) { duke@1: Set clsSet = (Set)classToClass.get(cd.qualifiedName()); duke@1: if (clsSet == null) { duke@1: clsSet = new TreeSet(); duke@1: classToClass.put(cd.qualifiedName(), clsSet); duke@1: } duke@1: return clsSet; duke@1: } duke@1: duke@1: private void add(Map map, ClassDoc cd, ProgramElementDoc ref) { duke@1: // add to specified map duke@1: refList(map, cd).add(ref); duke@1: duke@1: // add ref's package to package map and class map duke@1: packageSet(cd).add(ref.containingPackage()); duke@1: duke@1: classSet(cd).add(ref instanceof MemberDoc? duke@1: ((MemberDoc)ref).containingClass() : duke@1: ref); duke@1: } duke@1: duke@1: private void addAll(Map map, ClassDoc cd, Collection refs) { duke@1: if (refs == null) { duke@1: return; duke@1: } duke@1: // add to specified map duke@1: refList(map, cd).addAll(refs); duke@1: duke@1: Set pkgSet = packageSet(cd); duke@1: Set clsSet = classSet(cd); duke@1: // add ref's package to package map and class map duke@1: for (Iterator it = refs.iterator(); it.hasNext();) { duke@1: ProgramElementDoc pedoc = (ProgramElementDoc)it.next(); duke@1: pkgSet.add(pedoc.containingPackage()); duke@1: clsSet.add(pedoc instanceof MemberDoc? duke@1: ((MemberDoc)pedoc).containingClass() : duke@1: pedoc); duke@1: duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Map the ClassDocs to the ProgramElementDocs that use them as duke@1: * type parameters. duke@1: * duke@1: * @param map the map the insert the information into. duke@1: * @param doc the doc whose type parameters are being checked. duke@1: * @param holder the holder that owns the type parameters. duke@1: */ duke@1: private void mapTypeParameters(Map map, Object doc, duke@1: ProgramElementDoc holder) { duke@1: TypeVariable[] typeVariables; duke@1: if (doc instanceof ClassDoc) { duke@1: typeVariables = ((ClassDoc) doc).typeParameters(); duke@1: } else if (doc instanceof WildcardType) { duke@1: Type[] extendsBounds = ((WildcardType) doc).extendsBounds(); duke@1: for (int k = 0; k < extendsBounds.length; k++) { duke@1: addTypeParameterToMap(map, extendsBounds[k], holder); duke@1: } duke@1: Type[] superBounds = ((WildcardType) doc).superBounds(); duke@1: for (int k = 0; k < superBounds.length; k++) { duke@1: addTypeParameterToMap(map, superBounds[k], holder); duke@1: } duke@1: return; duke@1: } else if (doc instanceof ParameterizedType) { duke@1: Type[] typeArguments = ((ParameterizedType) doc).typeArguments(); duke@1: for (int k = 0; k < typeArguments.length; k++) { duke@1: addTypeParameterToMap(map, typeArguments[k], holder); duke@1: } duke@1: return; duke@1: } else if (doc instanceof ExecutableMemberDoc) { duke@1: typeVariables = ((ExecutableMemberDoc) doc).typeParameters(); duke@1: } else if (doc instanceof FieldDoc) { duke@1: Type fieldType = ((FieldDoc) doc).type(); duke@1: mapTypeParameters(map, fieldType, holder); duke@1: return; duke@1: } else { duke@1: return; duke@1: } duke@1: for (int i = 0; i < typeVariables.length; i++) { duke@1: Type[] bounds = typeVariables[i].bounds(); duke@1: for (int j = 0; j < bounds.length; j++) { duke@1: addTypeParameterToMap(map, bounds[j], holder); duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Map the AnnotationType to the ProgramElementDocs that use them as duke@1: * type parameters. duke@1: * duke@1: * @param map the map the insert the information into. duke@1: * @param doc the doc whose type parameters are being checked. duke@1: * @param holder the holder that owns the type parameters. duke@1: */ duke@1: private void mapAnnotations(Map map, Object doc, duke@1: Object holder) { duke@1: TypeVariable[] typeVariables; duke@1: AnnotationDesc[] annotations; duke@1: boolean isPackage = false; duke@1: if (doc instanceof ProgramElementDoc) { duke@1: annotations = ((ProgramElementDoc) doc).annotations(); duke@1: } else if (doc instanceof PackageDoc) { duke@1: annotations = ((PackageDoc) doc).annotations(); duke@1: isPackage = true; duke@1: } else if (doc instanceof Parameter) { duke@1: annotations = ((Parameter) doc).annotations(); duke@1: } else { duke@1: throw new DocletAbortException(); duke@1: } duke@1: for (int i = 0; i < annotations.length; i++) { duke@1: AnnotationTypeDoc annotationDoc = annotations[i].annotationType(); duke@1: if (isPackage) duke@1: refList(map, annotationDoc).add(holder); duke@1: else duke@1: add(map, annotationDoc, (ProgramElementDoc) holder); duke@1: } duke@1: } duke@1: duke@1: private void addTypeParameterToMap(Map map, Type type, duke@1: ProgramElementDoc holder) { duke@1: if (type instanceof ClassDoc) { duke@1: add(map, (ClassDoc) type, holder); duke@1: } else if (type instanceof ParameterizedType) { duke@1: add(map, ((ParameterizedType) type).asClassDoc(), holder); duke@1: } duke@1: mapTypeParameters(map, type, holder); duke@1: } duke@1: }