aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.internal.toolkit.util; aoqi@0: aoqi@0: import java.util.*; aoqi@0: aoqi@0: import com.sun.javadoc.*; aoqi@0: aoqi@0: /** aoqi@0: * Map all class uses for a given class. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @since 1.2 aoqi@0: * @author Robert G. Field aoqi@0: */ aoqi@0: public class ClassUseMapper { aoqi@0: aoqi@0: private final ClassTree classtree; aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to set of PackageDoc used by that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToPackage = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of Annotations to set of PackageDoc that use the annotation. aoqi@0: */ aoqi@0: public Map> classToPackageAnnotations = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to set of ClassDoc used by that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToClass = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of ClassDoc which are direct or aoqi@0: * indirect subclasses of that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToSubclass = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of ClassDoc which are direct or aoqi@0: * indirect subinterfaces of that interface. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToSubinterface = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of ClassDoc which implement aoqi@0: * this interface. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToImplementingClass = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of FieldDoc declared as that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToField = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of MethodDoc returning that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToMethodReturn = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of MethodDoc having that class aoqi@0: * as an arg. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToMethodArgs = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of MethodDoc which throws that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToMethodThrows = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of ConstructorDoc having that class aoqi@0: * as an arg. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToConstructorArgs = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * Mapping of ClassDocs to list of ConstructorDoc which throws that class. aoqi@0: * Entries may be null. aoqi@0: */ aoqi@0: public Map> classToConstructorThrows = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to constructors that use them. aoqi@0: */ aoqi@0: public Map> classToConstructorAnnotations = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to Constructor parameters that use them. aoqi@0: */ aoqi@0: public Map> classToConstructorParamAnnotation = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to Constructor arguments that use them as type parameters. aoqi@0: */ aoqi@0: public Map> classToConstructorDocArgTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to ClassDocs that use them as type parameters. aoqi@0: */ aoqi@0: public Map> classToClassTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to ClassDocs that use them. aoqi@0: */ aoqi@0: public Map> classToClassAnnotations = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to ExecutableMemberDocs that use them as type parameters. aoqi@0: */ aoqi@0: public Map> classToExecMemberDocTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to ExecutableMemberDocs arguments that use them as type parameters. aoqi@0: */ aoqi@0: public Map> classToExecMemberDocArgTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to ExecutableMemberDocs that use them. aoqi@0: */ aoqi@0: public Map> classToExecMemberDocAnnotations = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to ExecutableMemberDocs that have return type aoqi@0: * with type parameters of that class. aoqi@0: */ aoqi@0: public Map> classToExecMemberDocReturnTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to MethodDoc parameters that use them. aoqi@0: */ aoqi@0: public Map> classToExecMemberDocParamAnnotation = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of ClassDocs to FieldDocs that use them as type parameters. aoqi@0: */ aoqi@0: public Map> classToFieldDocTypeParam = new HashMap>(); aoqi@0: aoqi@0: /** aoqi@0: * The mapping of AnnotationTypeDocs to FieldDocs that use them. aoqi@0: */ aoqi@0: public Map> annotationToFieldDoc = new HashMap>(); aoqi@0: aoqi@0: aoqi@0: public ClassUseMapper(RootDoc root, ClassTree classtree) { aoqi@0: this.classtree = classtree; aoqi@0: aoqi@0: // Map subclassing, subinterfacing implementing, ... aoqi@0: for (Iterator it = classtree.baseclasses().iterator(); it.hasNext();) { aoqi@0: subclasses(it.next()); aoqi@0: } aoqi@0: for (Iterator it = classtree.baseinterfaces().iterator(); it.hasNext();) { aoqi@0: // does subinterfacing as side-effect aoqi@0: implementingClasses(it.next()); aoqi@0: } aoqi@0: // Map methods, fields, constructors using a class. aoqi@0: ClassDoc[] classes = root.classes(); aoqi@0: for (int i = 0; i < classes.length; i++) { aoqi@0: PackageDoc pkg = classes[i].containingPackage(); aoqi@0: mapAnnotations(classToPackageAnnotations, pkg, pkg); aoqi@0: ClassDoc cd = classes[i]; aoqi@0: mapTypeParameters(classToClassTypeParam, cd, cd); aoqi@0: mapAnnotations(classToClassAnnotations, cd, cd); aoqi@0: FieldDoc[] fields = cd.fields(); aoqi@0: for (int j = 0; j < fields.length; j++) { aoqi@0: FieldDoc fd = fields[j]; aoqi@0: mapTypeParameters(classToFieldDocTypeParam, fd, fd); aoqi@0: mapAnnotations(annotationToFieldDoc, fd, fd); aoqi@0: if (! fd.type().isPrimitive()) { aoqi@0: add(classToField, fd.type().asClassDoc(), fd); aoqi@0: } aoqi@0: } aoqi@0: ConstructorDoc[] cons = cd.constructors(); aoqi@0: for (int j = 0; j < cons.length; j++) { aoqi@0: mapAnnotations(classToConstructorAnnotations, cons[j], cons[j]); aoqi@0: mapExecutable(cons[j]); aoqi@0: } aoqi@0: MethodDoc[] meths = cd.methods(); aoqi@0: for (int j = 0; j < meths.length; j++) { aoqi@0: MethodDoc md = meths[j]; aoqi@0: mapExecutable(md); aoqi@0: mapTypeParameters(classToExecMemberDocTypeParam, md, md); aoqi@0: mapAnnotations(classToExecMemberDocAnnotations, md, md); aoqi@0: if (! (md.returnType().isPrimitive() || md.returnType() instanceof TypeVariable)) { aoqi@0: mapTypeParameters(classToExecMemberDocReturnTypeParam, aoqi@0: md.returnType(), md); aoqi@0: add(classToMethodReturn, md.returnType().asClassDoc(), md); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return all subclasses of a class AND fill-in classToSubclass map. aoqi@0: */ aoqi@0: private Collection subclasses(ClassDoc cd) { aoqi@0: Collection ret = classToSubclass.get(cd.qualifiedName()); aoqi@0: if (ret == null) { aoqi@0: ret = new TreeSet(); aoqi@0: List subs = classtree.subclasses(cd); aoqi@0: if (subs != null) { aoqi@0: ret.addAll(subs); aoqi@0: for (Iterator it = subs.iterator(); it.hasNext();) { aoqi@0: ret.addAll(subclasses(it.next())); aoqi@0: } aoqi@0: } aoqi@0: addAll(classToSubclass, cd, ret); aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return all subinterfaces of an interface AND fill-in classToSubinterface map. aoqi@0: */ aoqi@0: private Collection subinterfaces(ClassDoc cd) { aoqi@0: Collection ret = classToSubinterface.get(cd.qualifiedName()); aoqi@0: if (ret == null) { aoqi@0: ret = new TreeSet(); aoqi@0: List subs = classtree.subinterfaces(cd); aoqi@0: if (subs != null) { aoqi@0: ret.addAll(subs); aoqi@0: for (Iterator it = subs.iterator(); it.hasNext();) { aoqi@0: ret.addAll(subinterfaces(it.next())); aoqi@0: } aoqi@0: } aoqi@0: addAll(classToSubinterface, cd, ret); aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return all implementing classes of an interface (including aoqi@0: * all subclasses of implementing classes and all classes aoqi@0: * implementing subinterfaces) AND fill-in both classToImplementingClass aoqi@0: * and classToSubinterface maps. aoqi@0: */ aoqi@0: private Collection implementingClasses(ClassDoc cd) { aoqi@0: Collection ret = classToImplementingClass.get(cd.qualifiedName()); aoqi@0: if (ret == null) { aoqi@0: ret = new TreeSet(); aoqi@0: List impl = classtree.implementingclasses(cd); aoqi@0: if (impl != null) { aoqi@0: ret.addAll(impl); aoqi@0: for (Iterator it = impl.iterator(); it.hasNext();) { aoqi@0: ret.addAll(subclasses(it.next())); aoqi@0: } aoqi@0: } aoqi@0: for (Iterator it = subinterfaces(cd).iterator(); it.hasNext();) { aoqi@0: ret.addAll(implementingClasses(it.next())); aoqi@0: } aoqi@0: addAll(classToImplementingClass, cd, ret); aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Determine classes used by a method or constructor, so they can be aoqi@0: * inverse mapped. aoqi@0: */ aoqi@0: private void mapExecutable(ExecutableMemberDoc em) { aoqi@0: Parameter[] params = em.parameters(); aoqi@0: boolean isConstructor = em.isConstructor(); aoqi@0: List classArgs = new ArrayList(); aoqi@0: for (int k = 0; k < params.length; k++) { aoqi@0: Type pcd = params[k].type(); aoqi@0: // primitives don't get mapped, also avoid dups aoqi@0: if ((! params[k].type().isPrimitive()) && aoqi@0: ! classArgs.contains(pcd) && aoqi@0: ! (pcd instanceof TypeVariable)) { aoqi@0: add(isConstructor? classToConstructorArgs :classToMethodArgs, aoqi@0: pcd.asClassDoc(), em); aoqi@0: classArgs.add(pcd); aoqi@0: mapTypeParameters(isConstructor? aoqi@0: classToConstructorDocArgTypeParam : classToExecMemberDocArgTypeParam, aoqi@0: pcd, em); aoqi@0: } aoqi@0: mapAnnotations( aoqi@0: isConstructor ? aoqi@0: classToConstructorParamAnnotation : aoqi@0: classToExecMemberDocParamAnnotation, aoqi@0: params[k], em); aoqi@0: } aoqi@0: ClassDoc[] thr = em.thrownExceptions(); aoqi@0: for (int k = 0; k < thr.length; k++) { aoqi@0: add(isConstructor? classToConstructorThrows : classToMethodThrows, aoqi@0: thr[k], em); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private List refList(Map> map, ClassDoc cd) { aoqi@0: List list = map.get(cd.qualifiedName()); aoqi@0: if (list == null) { aoqi@0: List l = new ArrayList(); aoqi@0: list = l; aoqi@0: map.put(cd.qualifiedName(), list); aoqi@0: } aoqi@0: return list; aoqi@0: } aoqi@0: aoqi@0: private Set packageSet(ClassDoc cd) { aoqi@0: Set pkgSet = classToPackage.get(cd.qualifiedName()); aoqi@0: if (pkgSet == null) { aoqi@0: pkgSet = new TreeSet(); aoqi@0: classToPackage.put(cd.qualifiedName(), pkgSet); aoqi@0: } aoqi@0: return pkgSet; aoqi@0: } aoqi@0: aoqi@0: private Set classSet(ClassDoc cd) { aoqi@0: Set clsSet = classToClass.get(cd.qualifiedName()); aoqi@0: if (clsSet == null) { aoqi@0: Set s = new TreeSet(); aoqi@0: clsSet = s; aoqi@0: classToClass.put(cd.qualifiedName(), clsSet); aoqi@0: } aoqi@0: return clsSet; aoqi@0: } aoqi@0: aoqi@0: private void add(Map> map, ClassDoc cd, T ref) { aoqi@0: // add to specified map aoqi@0: refList(map, cd).add(ref); aoqi@0: aoqi@0: // add ref's package to package map and class map aoqi@0: packageSet(cd).add(ref.containingPackage()); aoqi@0: aoqi@0: classSet(cd).add(ref instanceof MemberDoc? aoqi@0: ((MemberDoc)ref).containingClass() : aoqi@0: (ClassDoc)ref); aoqi@0: } aoqi@0: aoqi@0: private void addAll(Map> map, ClassDoc cd, Collection refs) { aoqi@0: if (refs == null) { aoqi@0: return; aoqi@0: } aoqi@0: // add to specified map aoqi@0: refList(map, cd).addAll(refs); aoqi@0: aoqi@0: Set pkgSet = packageSet(cd); aoqi@0: Set clsSet = classSet(cd); aoqi@0: // add ref's package to package map and class map aoqi@0: for (Iterator it = refs.iterator(); it.hasNext();) { aoqi@0: ClassDoc cls = it.next(); aoqi@0: pkgSet.add(cls.containingPackage()); aoqi@0: clsSet.add(cls); aoqi@0: aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Map the ClassDocs to the ProgramElementDocs that use them as aoqi@0: * type parameters. aoqi@0: * aoqi@0: * @param map the map the insert the information into. aoqi@0: * @param doc the doc whose type parameters are being checked. aoqi@0: * @param holder the holder that owns the type parameters. aoqi@0: */ aoqi@0: private void mapTypeParameters(Map> map, Object doc, aoqi@0: T holder) { aoqi@0: TypeVariable[] typeVariables; aoqi@0: if (doc instanceof ClassDoc) { aoqi@0: typeVariables = ((ClassDoc) doc).typeParameters(); aoqi@0: } else if (doc instanceof WildcardType) { aoqi@0: Type[] extendsBounds = ((WildcardType) doc).extendsBounds(); aoqi@0: for (int k = 0; k < extendsBounds.length; k++) { aoqi@0: addTypeParameterToMap(map, extendsBounds[k], holder); aoqi@0: } aoqi@0: Type[] superBounds = ((WildcardType) doc).superBounds(); aoqi@0: for (int k = 0; k < superBounds.length; k++) { aoqi@0: addTypeParameterToMap(map, superBounds[k], holder); aoqi@0: } aoqi@0: return; aoqi@0: } else if (doc instanceof ParameterizedType) { aoqi@0: Type[] typeArguments = ((ParameterizedType) doc).typeArguments(); aoqi@0: for (int k = 0; k < typeArguments.length; k++) { aoqi@0: addTypeParameterToMap(map, typeArguments[k], holder); aoqi@0: } aoqi@0: return; aoqi@0: } else if (doc instanceof ExecutableMemberDoc) { aoqi@0: typeVariables = ((ExecutableMemberDoc) doc).typeParameters(); aoqi@0: } else if (doc instanceof FieldDoc) { aoqi@0: Type fieldType = ((FieldDoc) doc).type(); aoqi@0: mapTypeParameters(map, fieldType, holder); aoqi@0: return; aoqi@0: } else { aoqi@0: return; aoqi@0: } aoqi@0: for (int i = 0; i < typeVariables.length; i++) { aoqi@0: Type[] bounds = typeVariables[i].bounds(); aoqi@0: for (int j = 0; j < bounds.length; j++) { aoqi@0: addTypeParameterToMap(map, bounds[j], holder); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Map the AnnotationType to the ProgramElementDocs that use them as aoqi@0: * type parameters. aoqi@0: * aoqi@0: * @param map the map the insert the information into. aoqi@0: * @param doc the doc whose type parameters are being checked. aoqi@0: * @param holder the holder that owns the type parameters. aoqi@0: */ aoqi@0: private void mapAnnotations(Map> map, Object doc, aoqi@0: T holder) { aoqi@0: AnnotationDesc[] annotations; aoqi@0: boolean isPackage = false; aoqi@0: if (doc instanceof ProgramElementDoc) { aoqi@0: annotations = ((ProgramElementDoc) doc).annotations(); aoqi@0: } else if (doc instanceof PackageDoc) { aoqi@0: annotations = ((PackageDoc) doc).annotations(); aoqi@0: isPackage = true; aoqi@0: } else if (doc instanceof Parameter) { aoqi@0: annotations = ((Parameter) doc).annotations(); aoqi@0: } else { aoqi@0: throw new DocletAbortException("should not happen"); aoqi@0: } aoqi@0: for (int i = 0; i < annotations.length; i++) { aoqi@0: AnnotationTypeDoc annotationDoc = annotations[i].annotationType(); aoqi@0: if (isPackage) aoqi@0: refList(map, annotationDoc).add(holder); aoqi@0: else aoqi@0: add(map, annotationDoc, holder); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Map the AnnotationType to the ProgramElementDocs that use them as aoqi@0: * type parameters. aoqi@0: * aoqi@0: * @param map the map the insert the information into. aoqi@0: * @param doc the doc whose type parameters are being checked. aoqi@0: * @param holder the holder that owns the type parameters. aoqi@0: */ aoqi@0: private void mapAnnotations(Map> map, PackageDoc doc, aoqi@0: T holder) { aoqi@0: AnnotationDesc[] annotations; aoqi@0: annotations = doc.annotations(); aoqi@0: for (int i = 0; i < annotations.length; i++) { aoqi@0: AnnotationTypeDoc annotationDoc = annotations[i].annotationType(); aoqi@0: refList(map, annotationDoc).add(holder); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private void addTypeParameterToMap(Map> map, Type type, aoqi@0: T holder) { aoqi@0: if (type instanceof ClassDoc) { aoqi@0: add(map, (ClassDoc) type, holder); aoqi@0: } else if (type instanceof ParameterizedType) { aoqi@0: add(map, ((ParameterizedType) type).asClassDoc(), holder); aoqi@0: } aoqi@0: mapTypeParameters(map, type, holder); aoqi@0: } aoqi@0: }