duke@1: /* jjg@1357: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.util; duke@1: jjg@1357: import java.util.*; jjg@1357: duke@1: import com.sun.javadoc.*; duke@1: duke@1: /** duke@1: * Map all class uses for a given class. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. 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: */ jjg@74: public Map> classToPackage = new HashMap>(); duke@1: duke@1: /** duke@1: * Mapping of Annotations to set of PackageDoc that use the annotation. duke@1: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: public Map> classToConstructorThrows = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to constructors that use them. duke@1: */ jjg@74: public Map> classToConstructorAnnotations = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to Constructor parameters that use them. duke@1: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: public Map> classToClassTypeParam = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to ClassDocs that use them. duke@1: */ jjg@74: 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: */ jjg@74: 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: */ jjg@74: public Map> classToExecMemberDocArgTypeParam = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to ExecutableMemberDocs that use them. duke@1: */ jjg@74: 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: */ jjg@74: public Map> classToExecMemberDocReturnTypeParam = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to MethodDoc parameters that use them. duke@1: */ jjg@74: 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: */ jjg@74: public Map> classToFieldDocTypeParam = new HashMap>(); duke@1: duke@1: /** duke@1: * The mapping of AnnotationTypeDocs to FieldDocs that use them. duke@1: */ jjg@74: 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, ... mcimadamore@184: for (Iterator it = classtree.baseclasses().iterator(); it.hasNext();) { mcimadamore@184: subclasses(it.next()); duke@1: } mcimadamore@184: for (Iterator it = classtree.baseinterfaces().iterator(); it.hasNext();) { duke@1: // does subinterfacing as side-effect mcimadamore@184: implementingClasses(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: */ jjg@74: private Collection subclasses(ClassDoc cd) { jjg@74: Collection ret = classToSubclass.get(cd.qualifiedName()); duke@1: if (ret == null) { jjg@74: ret = new TreeSet(); jjg@74: List subs = classtree.subclasses(cd); duke@1: if (subs != null) { duke@1: ret.addAll(subs); jjg@74: for (Iterator it = subs.iterator(); it.hasNext();) { jjg@74: ret.addAll(subclasses(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: */ jjg@74: private Collection subinterfaces(ClassDoc cd) { jjg@74: Collection ret = classToSubinterface.get(cd.qualifiedName()); duke@1: if (ret == null) { jjg@74: ret = new TreeSet(); jjg@74: List subs = classtree.subinterfaces(cd); duke@1: if (subs != null) { duke@1: ret.addAll(subs); jjg@74: for (Iterator it = subs.iterator(); it.hasNext();) { jjg@74: ret.addAll(subinterfaces(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: */ jjg@74: private Collection implementingClasses(ClassDoc cd) { jjg@74: Collection ret = classToImplementingClass.get(cd.qualifiedName()); duke@1: if (ret == null) { jjg@74: ret = new TreeSet(); jjg@74: List impl = classtree.implementingclasses(cd); duke@1: if (impl != null) { duke@1: ret.addAll(impl); mcimadamore@184: for (Iterator it = impl.iterator(); it.hasNext();) { mcimadamore@184: ret.addAll(subclasses(it.next())); duke@1: } duke@1: } mcimadamore@184: for (Iterator it = subinterfaces(cd).iterator(); it.hasNext();) { mcimadamore@184: ret.addAll(implementingClasses(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(); jjg@74: 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: jjg@74: private List refList(Map> map, ClassDoc cd) { jjg@74: List list = map.get(cd.qualifiedName()); duke@1: if (list == null) { mcimadamore@184: List l = new ArrayList(); jjg@74: list = l; duke@1: map.put(cd.qualifiedName(), list); duke@1: } duke@1: return list; duke@1: } duke@1: jjg@74: private Set packageSet(ClassDoc cd) { jjg@74: Set pkgSet = classToPackage.get(cd.qualifiedName()); duke@1: if (pkgSet == null) { jjg@74: pkgSet = new TreeSet(); duke@1: classToPackage.put(cd.qualifiedName(), pkgSet); duke@1: } duke@1: return pkgSet; duke@1: } duke@1: jjg@74: private Set classSet(ClassDoc cd) { jjg@74: Set clsSet = classToClass.get(cd.qualifiedName()); duke@1: if (clsSet == null) { mcimadamore@184: Set s = new TreeSet(); jjg@74: clsSet = s; duke@1: classToClass.put(cd.qualifiedName(), clsSet); duke@1: } duke@1: return clsSet; duke@1: } duke@1: jjg@74: private void add(Map> map, ClassDoc cd, T 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() : jjg@74: (ClassDoc)ref); duke@1: } duke@1: jjg@74: 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: jjg@74: Set pkgSet = packageSet(cd); jjg@74: Set clsSet = classSet(cd); duke@1: // add ref's package to package map and class map jjg@74: for (Iterator it = refs.iterator(); it.hasNext();) { jjg@74: ClassDoc cls = it.next(); jjg@74: pkgSet.add(cls.containingPackage()); jjg@74: clsSet.add(cls); 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: */ jjg@74: private void mapTypeParameters(Map> map, Object doc, jjg@74: T 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: */ jjg@74: private void mapAnnotations(Map> map, Object doc, jjg@74: T holder) { 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 jjg@74: add(map, annotationDoc, holder); duke@1: } duke@1: } duke@1: jjg@74: jjg@74: /** jjg@74: * Map the AnnotationType to the ProgramElementDocs that use them as jjg@74: * type parameters. jjg@74: * jjg@74: * @param map the map the insert the information into. jjg@74: * @param doc the doc whose type parameters are being checked. jjg@74: * @param holder the holder that owns the type parameters. jjg@74: */ jjg@74: private void mapAnnotations(Map> map, PackageDoc doc, jjg@74: T holder) { jjg@74: AnnotationDesc[] annotations; jjg@74: annotations = doc.annotations(); jjg@74: for (int i = 0; i < annotations.length; i++) { jjg@74: AnnotationTypeDoc annotationDoc = annotations[i].annotationType(); jjg@74: refList(map, annotationDoc).add(holder); jjg@74: } jjg@74: } jjg@74: jjg@74: private void addTypeParameterToMap(Map> map, Type type, jjg@74: T 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: }