src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/ClassUseMapper.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 20,484 **** * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ - package com.sun.tools.doclets.internal.toolkit.util; - import java.util.*; ! import com.sun.javadoc.*; ! import com.sun.tools.doclets.formats.html.ConfigurationImpl; /** * Map all class uses for a given class. * ! * <p><b>This is NOT part of any supported API. ! * If you write code that depends on this, you do so at your own risk. ! * This code and its internal interfaces are subject to change or ! * deletion without notice.</b> * * @since 1.2 * @author Robert G. Field */ public class ClassUseMapper { private final ClassTree classtree; /** ! * Mapping of ClassDocs to set of PackageDoc used by that class. * Entries may be null. */ ! public Map<String,Set<PackageDoc>> classToPackage = new HashMap<>(); /** ! * Mapping of Annotations to set of PackageDoc that use the annotation. */ ! public Map<String,List<PackageDoc>> classToPackageAnnotations = new HashMap<>(); /** ! * Mapping of ClassDocs to set of ClassDoc used by that class. * Entries may be null. */ ! public Map<String,Set<ClassDoc>> classToClass = new HashMap<>(); /** ! * Mapping of ClassDocs to list of ClassDoc which are direct or ! * indirect subclasses of that class. ! * Entries may be null. */ ! public Map<String,List<ClassDoc>> classToSubclass = new HashMap<>(); /** ! * Mapping of ClassDocs to list of ClassDoc which are direct or ! * indirect subinterfaces of that interface. ! * Entries may be null. */ ! public Map<String,List<ClassDoc>> classToSubinterface = new HashMap<>(); /** ! * Mapping of ClassDocs to list of ClassDoc which implement ! * this interface. * Entries may be null. */ ! public Map<String,List<ClassDoc>> classToImplementingClass = new HashMap<>(); /** ! * Mapping of ClassDocs to list of FieldDoc declared as that class. * Entries may be null. */ ! public Map<String,List<FieldDoc>> classToField = new HashMap<>(); /** ! * Mapping of ClassDocs to list of MethodDoc returning that class. * Entries may be null. */ ! public Map<String,List<MethodDoc>> classToMethodReturn = new HashMap<>(); /** ! * Mapping of ClassDocs to list of MethodDoc having that class ! * as an arg. * Entries may be null. */ ! public Map<String,List<ExecutableMemberDoc>> classToMethodArgs = new HashMap<>(); /** ! * Mapping of ClassDocs to list of MethodDoc which throws that class. * Entries may be null. */ ! public Map<String,List<ExecutableMemberDoc>> classToMethodThrows = new HashMap<>(); /** ! * Mapping of ClassDocs to list of ConstructorDoc having that class ! * as an arg. ! * Entries may be null. */ ! public Map<String,List<ExecutableMemberDoc>> classToConstructorArgs = new HashMap<>(); /** ! * Mapping of ClassDocs to list of ConstructorDoc which throws that class. ! * Entries may be null. */ ! public Map<String,List<ExecutableMemberDoc>> classToConstructorThrows = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to constructors that use them. */ ! public Map<String,List<ConstructorDoc>> classToConstructorAnnotations = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to Constructor parameters that use them. */ ! public Map<String,List<ExecutableMemberDoc>> classToConstructorParamAnnotation = new HashMap<>(); /** ! * The mapping of ClassDocs to Constructor arguments that use them as type parameters. */ ! public Map<String,List<ExecutableMemberDoc>> classToConstructorDocArgTypeParam = new HashMap<>(); /** ! * The mapping of ClassDocs to ClassDocs that use them as type parameters. */ ! public Map<String,List<ClassDoc>> classToClassTypeParam = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to ClassDocs that use them. */ ! public Map<String,List<ClassDoc>> classToClassAnnotations = new HashMap<>(); /** ! * The mapping of ClassDocs to ExecutableMemberDocs that use them as type parameters. */ ! public Map<String,List<MethodDoc>> classToExecMemberDocTypeParam = new HashMap<>(); /** ! * The mapping of ClassDocs to ExecutableMemberDocs arguments that use them as type parameters. */ ! public Map<String,List<ExecutableMemberDoc>> classToExecMemberDocArgTypeParam = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to ExecutableMemberDocs that use them. */ ! public Map<String,List<MethodDoc>> classToExecMemberDocAnnotations = new HashMap<>(); /** ! * The mapping of ClassDocs to ExecutableMemberDocs that have return type ! * with type parameters of that class. */ ! public Map<String,List<MethodDoc>> classToExecMemberDocReturnTypeParam = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to MethodDoc parameters that use them. */ ! public Map<String,List<ExecutableMemberDoc>> classToExecMemberDocParamAnnotation = new HashMap<>(); /** ! * The mapping of ClassDocs to FieldDocs that use them as type parameters. */ ! public Map<String,List<FieldDoc>> classToFieldDocTypeParam = new HashMap<>(); /** ! * The mapping of AnnotationTypeDocs to FieldDocs that use them. */ ! public Map<String,List<FieldDoc>> annotationToFieldDoc = new HashMap<>(); private final Utils utils; public ClassUseMapper(ConfigurationImpl configuration, ClassTree classtree) { ! RootDoc root = configuration.root; ! this.classtree = classtree; utils = configuration.utils; // Map subclassing, subinterfacing implementing, ... ! for (ClassDoc doc : classtree.baseclasses()) { ! subclasses(doc); } ! for (ClassDoc doc : classtree.baseinterfaces()) { // does subinterfacing as side-effect ! implementingClasses(doc); } // Map methods, fields, constructors using a class. ! ClassDoc[] classes = root.classes(); ! for (ClassDoc aClass : classes) { ! PackageDoc pkg = aClass.containingPackage(); mapAnnotations(classToPackageAnnotations, pkg, pkg); ! ClassDoc cd = aClass; ! mapTypeParameters(classToClassTypeParam, cd, cd); ! mapAnnotations(classToClassAnnotations, cd, cd); ! FieldDoc[] fields = cd.fields(); ! for (FieldDoc fd : fields) { ! mapTypeParameters(classToFieldDocTypeParam, fd, fd); ! mapAnnotations(annotationToFieldDoc, fd, fd); ! if (!fd.type().isPrimitive()) { ! add(classToField, fd.type().asClassDoc(), fd); } } ! ConstructorDoc[] cons = cd.constructors(); ! for (ConstructorDoc con : cons) { ! mapAnnotations(classToConstructorAnnotations, con, con); ! mapExecutable(con); } ! MethodDoc[] meths = cd.methods(); ! for (MethodDoc md : meths) { mapExecutable(md); ! mapTypeParameters(classToExecMemberDocTypeParam, md, md); ! mapAnnotations(classToExecMemberDocAnnotations, md, md); ! if (!(md.returnType().isPrimitive() || md.returnType() instanceof TypeVariable)) { ! mapTypeParameters(classToExecMemberDocReturnTypeParam, ! md.returnType(), md); ! add(classToMethodReturn, md.returnType().asClassDoc(), md); } } } } /** ! * Return all subclasses of a class AND fill-in classToSubclass map. */ ! private Collection<ClassDoc> subclasses(ClassDoc cd) { ! Collection<ClassDoc> ret = classToSubclass.get(cd.qualifiedName()); if (ret == null) { ! ret = new TreeSet<>(utils.makeComparatorForClassUse()); ! SortedSet<ClassDoc> subs = classtree.subclasses(cd); if (subs != null) { ret.addAll(subs); ! for (ClassDoc sub : subs) { ret.addAll(subclasses(sub)); } } ! addAll(classToSubclass, cd, ret); } return ret; } /** ! * Return all subinterfaces of an interface AND fill-in classToSubinterface map. */ ! private Collection<ClassDoc> subinterfaces(ClassDoc cd) { ! Collection<ClassDoc> ret = classToSubinterface.get(cd.qualifiedName()); if (ret == null) { ! ret = new TreeSet<>(utils.makeComparatorForClassUse()); ! SortedSet<ClassDoc> subs = classtree.subinterfaces(cd); if (subs != null) { ret.addAll(subs); ! for (ClassDoc sub : subs) { ret.addAll(subinterfaces(sub)); } } ! addAll(classToSubinterface, cd, ret); } return ret; } /** ! * Return all implementing classes of an interface (including ! * all subclasses of implementing classes and all classes ! * implementing subinterfaces) AND fill-in both classToImplementingClass * and classToSubinterface maps. */ ! private Collection<ClassDoc> implementingClasses(ClassDoc cd) { ! Collection<ClassDoc> ret = classToImplementingClass.get(cd.qualifiedName()); if (ret == null) { ! ret = new TreeSet<>(utils.makeComparatorForClassUse()); ! SortedSet<ClassDoc> impl = classtree.implementingclasses(cd); if (impl != null) { ret.addAll(impl); ! for (ClassDoc anImpl : impl) { ret.addAll(subclasses(anImpl)); } } ! for (ClassDoc doc : subinterfaces(cd)) { ! ret.addAll(implementingClasses(doc)); } ! addAll(classToImplementingClass, cd, ret); } return ret; } /** ! * Determine classes used by a method or constructor, so they can be ! * inverse mapped. */ ! private void mapExecutable(ExecutableMemberDoc em) { ! boolean isConstructor = em.isConstructor(); ! Set<Type> classArgs = new TreeSet<>(utils.makeTypeComparator()); ! for (Parameter param : em.parameters()) { ! Type pcd = param.type(); ! // ignore primitives and typevars, typevars are handled elsewhere ! if ((!param.type().isPrimitive()) && !(pcd instanceof TypeVariable)) { ! // avoid dups ! if (classArgs.add(pcd)) { ! add(isConstructor ? classToConstructorArgs : classToMethodArgs, ! pcd.asClassDoc(), em); mapTypeParameters(isConstructor ! ? classToConstructorDocArgTypeParam ! : classToExecMemberDocArgTypeParam, ! pcd, em); } } mapAnnotations(isConstructor ? classToConstructorParamAnnotation ! : classToExecMemberDocParamAnnotation, ! param, em); } ! for (ClassDoc anException : em.thrownExceptions()) { add(isConstructor ? classToConstructorThrows : classToMethodThrows, ! anException, em); } } ! private <T> List<T> refList(Map<String,List<T>> map, ClassDoc cd) { ! List<T> list = map.get(cd.qualifiedName()); if (list == null) { list = new ArrayList<>(); ! map.put(cd.qualifiedName(), list); } return list; } ! private Set<PackageDoc> packageSet(ClassDoc cd) { ! Set<PackageDoc> pkgSet = classToPackage.get(cd.qualifiedName()); if (pkgSet == null) { ! pkgSet = new TreeSet<>(); ! classToPackage.put(cd.qualifiedName(), pkgSet); } return pkgSet; } ! private Set<ClassDoc> classSet(ClassDoc cd) { ! Set<ClassDoc> clsSet = classToClass.get(cd.qualifiedName()); if (clsSet == null) { ! clsSet = new TreeSet<>(); ! classToClass.put(cd.qualifiedName(), clsSet); } return clsSet; } ! private <T extends ProgramElementDoc> void add(Map<String,List<T>> map, ClassDoc cd, T ref) { // add to specified map ! refList(map, cd).add(ref); ! // add ref's package to package map and class map ! packageSet(cd).add(ref.containingPackage()); ! ! classSet(cd).add(ref instanceof MemberDoc? ! ((MemberDoc)ref).containingClass() : ! (ClassDoc)ref); } ! private void addAll(Map<String,List<ClassDoc>> map, ClassDoc cd, Collection<ClassDoc> refs) { if (refs == null) { return; } // add to specified map ! refList(map, cd).addAll(refs); ! Set<PackageDoc> pkgSet = packageSet(cd); ! Set<ClassDoc> clsSet = classSet(cd); // add ref's package to package map and class map ! for (ClassDoc cls : refs) { ! pkgSet.add(cls.containingPackage()); clsSet.add(cls); - } } /** ! * Map the ClassDocs to the ProgramElementDocs that use them as ! * type parameters. * * @param map the map the insert the information into. ! * @param doc the doc whose type parameters are being checked. * @param holder the holder that owns the type parameters. */ ! private <T extends ProgramElementDoc> void mapTypeParameters(Map<String,List<T>> map, Object doc, ! T holder) { ! TypeVariable[] typeVariables; ! if (doc instanceof ClassDoc) { ! typeVariables = ((ClassDoc) doc).typeParameters(); ! } else if (doc instanceof WildcardType) { ! for (Type extendsBound : ((WildcardType) doc).extendsBounds()) { ! addTypeParameterToMap(map, extendsBound, holder); } - for (Type superBound : ((WildcardType) doc).superBounds()) { - addTypeParameterToMap(map, superBound, holder); } ! return; ! } else if (doc instanceof ParameterizedType) { ! for (Type typeArgument : ((ParameterizedType) doc).typeArguments()) { ! addTypeParameterToMap(map, typeArgument, holder); } ! return; ! } else if (doc instanceof ExecutableMemberDoc) { ! typeVariables = ((ExecutableMemberDoc) doc).typeParameters(); ! } else if (doc instanceof FieldDoc) { ! Type fieldType = ((FieldDoc) doc).type(); ! mapTypeParameters(map, fieldType, holder); ! return; ! } else { ! return; } ! for (TypeVariable typeVariable : typeVariables) { ! for (Type bound : typeVariable.bounds()) { addTypeParameterToMap(map, bound, holder); } } } ! /** ! * Map the AnnotationType to the ProgramElementDocs that use them as ! * type parameters. ! * ! * @param map the map the insert the information into. ! * @param doc the doc whose type parameters are being checked. ! * @param holder the holder that owns the type parameters. ! */ ! private <T extends ProgramElementDoc> void mapAnnotations(Map<String,List<T>> map, Object doc, ! T holder) { ! AnnotationDesc[] annotations; ! boolean isPackage = false; ! if (doc instanceof ProgramElementDoc) { ! annotations = ((ProgramElementDoc) doc).annotations(); ! } else if (doc instanceof PackageDoc) { ! annotations = ((PackageDoc) doc).annotations(); ! isPackage = true; ! } else if (doc instanceof Parameter) { ! annotations = ((Parameter) doc).annotations(); ! } else { ! throw new DocletAbortException("should not happen"); } ! for (AnnotationDesc annotation : annotations) { ! AnnotationTypeDoc annotationDoc = annotation.annotationType(); ! if (isPackage) ! refList(map, annotationDoc).add(holder); ! else ! add(map, annotationDoc, holder); } } - /** ! * Map the AnnotationType to the ProgramElementDocs that use them as ! * type parameters. * * @param map the map the insert the information into. ! * @param doc the doc whose type parameters are being checked. * @param holder the holder that owns the type parameters. */ ! private <T extends PackageDoc> void mapAnnotations(Map<String,List<T>> map, PackageDoc doc, ! T holder) { ! for (AnnotationDesc annotation : doc.annotations()) { ! AnnotationTypeDoc annotationDoc = annotation.annotationType(); ! refList(map, annotationDoc).add(holder); } } ! private <T extends ProgramElementDoc> void addTypeParameterToMap(Map<String,List<T>> map, Type type, ! T holder) { ! if (type instanceof ClassDoc) { ! add(map, (ClassDoc) type, holder); ! } else if (type instanceof ParameterizedType) { ! add(map, ((ParameterizedType) type).asClassDoc(), holder); } mapTypeParameters(map, type, holder); } } --- 20,611 ---- * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ + package jdk.javadoc.internal.doclets.toolkit.util; import java.util.*; ! import javax.lang.model.element.AnnotationMirror; ! import javax.lang.model.element.Element; ! import javax.lang.model.element.ExecutableElement; ! import javax.lang.model.element.PackageElement; ! import javax.lang.model.element.TypeElement; ! import javax.lang.model.element.TypeParameterElement; ! import javax.lang.model.element.VariableElement; ! import javax.lang.model.type.ArrayType; ! import javax.lang.model.type.DeclaredType; ! import javax.lang.model.type.ErrorType; ! import javax.lang.model.type.TypeMirror; ! import javax.lang.model.type.TypeVariable; ! import javax.lang.model.type.WildcardType; ! import javax.lang.model.util.Elements; ! import javax.lang.model.util.SimpleElementVisitor9; ! import javax.lang.model.util.SimpleTypeVisitor9; ! import javax.lang.model.util.Types; + import com.sun.tools.javac.util.DefinedBy; + import com.sun.tools.javac.util.DefinedBy.Api; + import jdk.javadoc.doclet.DocletEnvironment; + import jdk.javadoc.internal.doclets.formats.html.ConfigurationImpl; + /** * Map all class uses for a given class. * ! * <p> ! * <b>This is NOT part of any supported API. If you write code that depends on this, you do so at ! * your own risk. This code and its internal interfaces are subject to change or deletion without ! * notice.</b> * * @since 1.2 * @author Robert G. Field */ public class ClassUseMapper { private final ClassTree classtree; /** ! * Mapping of TypeElements to set of PackageElements used by that class. * Entries may be null. */ ! public final Map<TypeElement, Set<PackageElement>> classToPackage; /** ! * Mapping of TypeElements representing annotations to a set of PackageElements that use the annotation. */ ! public final Map<TypeElement, List<PackageElement>> classToPackageAnnotations = new HashMap<>(); /** ! * Mapping of TypeElements to a set of TypeElements used by that class. * Entries may be null. */ ! public final Map<TypeElement, Set<TypeElement>> classToClass = new HashMap<>(); /** ! * Mapping of TypeElements to a list of TypeElements which are direct or indirect subClasses of ! * that class. Entries may be null. */ ! public final Map<TypeElement, List<TypeElement>> classToSubclass = new HashMap<>(); /** ! * Mapping of TypeElements to list of TypeElements which are direct or indirect subInterfaces of ! * that interface. Entries may be null. */ ! public final Map<TypeElement, List<TypeElement>> classToSubinterface = new HashMap<>(); /** ! * Mapping of TypeElements to list of TypeElements which implement this interface. * Entries may be null. */ ! public Map<TypeElement, List<TypeElement>> classToImplementingClass = new HashMap<>(); /** ! * Mapping of TypeElements to list of VariableElements declared as that class. * Entries may be null. */ ! public final Map<TypeElement, List<VariableElement>> classToField = new HashMap<>(); /** ! * Mapping of TypeElements to list of ExecutableElements returning that class. * Entries may be null. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodReturn = new HashMap<>(); /** ! * Mapping of TypeElements to list of ExecutableElements having that class as an arg. * Entries may be null. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodArgs = new HashMap<>(); /** ! * Mapping of TypeElements to list of ExecutableElements which throws that class. * Entries may be null. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodThrows = new HashMap<>(); /** ! * Mapping of TypeElements to list of ExecutableElements (constructors) having that ! * class as an arg. Entries may be null. */ ! public final Map<TypeElement, List<ExecutableElement>> classToConstructorArgs = new HashMap<>(); /** ! * Mapping of TypeElements to list of constructors which throws that class. Entries may be null. */ ! public final Map<TypeElement, List<ExecutableElement>> classToConstructorThrows = new HashMap<>(); /** ! * The mapping of TypeElements representing annotations to constructors that use them. */ ! public final Map<TypeElement, List<ExecutableElement>> classToConstructorAnnotations = new HashMap<>(); /** ! * The mapping of TypeElement representing annotations to constructor parameters that use them. */ ! public final Map<TypeElement, List<ExecutableElement>> classToConstructorParamAnnotation = new HashMap<>(); /** ! * The mapping of TypeElements to constructor arguments that use them as type parameters. */ ! public final Map<TypeElement, List<ExecutableElement>> classToConstructorArgTypeParam = new HashMap<>(); /** ! * The mapping of TypeElement to TypeElement that use them as type parameters. */ ! public final Map<TypeElement, List<TypeElement>> classToClassTypeParam = new HashMap<>(); /** ! * The mapping of TypeElement representing annotation to TypeElements that use them. */ ! public final Map<TypeElement, List<TypeElement>> classToClassAnnotations = new HashMap<>(); /** ! * The mapping of TypeElement to methods that use them as type parameters. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodTypeParam = new HashMap<>(); /** ! * The mapping of TypeElement to method arguments that use them as type parameters. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodArgTypeParam = new HashMap<>(); /** ! * The mapping of TypeElement representing annotation to methods that use them. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodAnnotations = new HashMap<>(); /** ! * The mapping of TypeElements to methods that have return type with type parameters ! * of that class. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodReturnTypeParam = new HashMap<>(); /** ! * The mapping of TypeElements representing annotations to method parameters that use them. */ ! public final Map<TypeElement, List<ExecutableElement>> classToMethodParamAnnotation = new HashMap<>(); /** ! * The mapping of TypeElements to fields that use them as type parameters. */ ! public final Map<TypeElement, List<VariableElement>> classToFieldTypeParam = new HashMap<>(); /** ! * The mapping of TypeElements representing annotation to fields that use them. */ ! public final Map<TypeElement, List<VariableElement>> annotationToField = new HashMap<>(); + private final DocletEnvironment root; + private final Elements elements; + private final Types typeutils; private final Utils utils; + public ClassUseMapper(ConfigurationImpl configuration, ClassTree classtree) { ! root = configuration.root; ! elements = root.getElementUtils(); ! typeutils = root.getTypeUtils(); utils = configuration.utils; + this.classtree = classtree; + classToPackage = new TreeMap<>(utils.makeClassUseComparator()); // Map subclassing, subinterfacing implementing, ... ! for (TypeElement te : classtree.baseClasses()) { ! subclasses(te); } ! for (TypeElement intfc : classtree.baseInterfaces()) { // does subinterfacing as side-effect ! implementingClasses(intfc); } // Map methods, fields, constructors using a class. ! Set<TypeElement> classes = root.getIncludedClasses(); ! for (TypeElement aClass : classes) { ! PackageElement pkg = elements.getPackageOf(aClass); mapAnnotations(classToPackageAnnotations, pkg, pkg); ! mapTypeParameters(classToClassTypeParam, aClass, aClass); ! mapAnnotations(classToClassAnnotations, aClass, aClass); ! List<VariableElement> fields = utils.getFields(aClass); ! for (VariableElement fd : fields) { ! mapTypeParameters(classToFieldTypeParam, fd, fd); ! mapAnnotations(annotationToField, fd, fd); ! SimpleTypeVisitor9<Void, VariableElement> stv = new SimpleTypeVisitor9<Void, VariableElement>() { ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitArray(ArrayType t, VariableElement p) { ! return visit(t.getComponentType(), p); } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Void visitDeclared(DeclaredType t, VariableElement p) { + add(classToField, (TypeElement) t.asElement(), p); + return null; } ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitTypeVariable(TypeVariable t, VariableElement p) { ! return visit(typeutils.erasure(t), p); } ! }; ! stv.visit(fd.asType(), fd); ! } ! ! List<ExecutableElement> ctors = utils.getConstructors(aClass); ! for (ExecutableElement ctor : ctors) { ! mapAnnotations(classToConstructorAnnotations, ctor, ctor); ! mapExecutable(ctor); ! } ! ! List<ExecutableElement> meths = utils.getMethods(aClass); ! for (ExecutableElement md : meths) { mapExecutable(md); ! mapTypeParameters(classToMethodTypeParam, md, md); ! mapAnnotations(classToMethodAnnotations, md, md); ! SimpleTypeVisitor9<Void, ExecutableElement> stv = new SimpleTypeVisitor9<Void, ExecutableElement>() { ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitArray(ArrayType t, ExecutableElement p) { ! TypeMirror componentType = t.getComponentType(); ! return visit(utils.isTypeVariable(componentType) ! ? typeutils.erasure(componentType) ! : componentType, p); } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Void visitDeclared(DeclaredType t, ExecutableElement p) { + mapTypeParameters(classToMethodReturnTypeParam, t, p); + add(classToMethodReturn, (TypeElement) t.asElement(), p); + return null; } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + protected Void defaultAction(TypeMirror e, ExecutableElement p) { + return null; } + }; + stv.visit(md.getReturnType(), md); } + } + } /** ! * Return all subClasses of a class AND fill-in classToSubclass map. */ ! private Collection<TypeElement> subclasses(TypeElement te) { ! Collection<TypeElement> ret = classToSubclass.get(te); if (ret == null) { ! ret = new TreeSet<>(utils.makeClassUseComparator()); ! Set<TypeElement> subs = classtree.subClasses(te); if (subs != null) { ret.addAll(subs); ! for (TypeElement sub : subs) { ret.addAll(subclasses(sub)); } } ! addAll(classToSubclass, te, ret); } return ret; } /** ! * Return all subInterfaces of an interface AND fill-in classToSubinterface map. */ ! private Collection<TypeElement> subinterfaces(TypeElement te) { ! Collection<TypeElement> ret = classToSubinterface.get(te); if (ret == null) { ! ret = new TreeSet<>(utils.makeClassUseComparator()); ! Set<TypeElement> subs = classtree.subInterfaces(te); if (subs != null) { ret.addAll(subs); ! for (TypeElement sub : subs) { ret.addAll(subinterfaces(sub)); } } ! addAll(classToSubinterface, te, ret); } return ret; } /** ! * Return all implementing classes of an interface (including all subClasses of implementing ! * classes and all classes implementing subInterfaces) AND fill-in both classToImplementingClass * and classToSubinterface maps. */ ! private Collection<TypeElement> implementingClasses(TypeElement te) { ! Collection<TypeElement> ret = classToImplementingClass.get(te); if (ret == null) { ! ret = new TreeSet<>(utils.makeClassUseComparator()); ! Set<TypeElement> impl = classtree.implementingclasses(te); if (impl != null) { ret.addAll(impl); ! for (TypeElement anImpl : impl) { ret.addAll(subclasses(anImpl)); } } ! for (TypeElement intfc : subinterfaces(te)) { ! ret.addAll(implementingClasses(intfc)); } ! addAll(classToImplementingClass, te, ret); } return ret; } /** ! * Determine classes used by a method or constructor, so they can be inverse mapped. */ ! private void mapExecutable(ExecutableElement ee) { ! final boolean isConstructor = utils.isConstructor(ee); ! Set<TypeMirror> classArgs = new TreeSet<>(utils.makeTypeMirrorClassUseComparator()); ! for (VariableElement param : ee.getParameters()) { ! TypeMirror pType = param.asType(); ! // primitives don't get mapped and type variables are mapped elsewhere ! if (!pType.getKind().isPrimitive() && !utils.isTypeVariable(pType)) { ! // no duplicates please ! if (classArgs.add(pType)) { ! new SimpleTypeVisitor9<Void, ExecutableElement>() { ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitArray(ArrayType t, ExecutableElement p) { ! return visit(t.getComponentType(), p); ! } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitDeclared(DeclaredType t, ExecutableElement p) { ! add(isConstructor ! ? classToConstructorArgs ! : classToMethodArgs, ! (TypeElement) t.asElement(), p); ! return null; ! } ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitTypeVariable(TypeVariable t, ExecutableElement p) { ! visit(typeutils.erasure(t), p); ! return null; ! } ! }.visit(pType, ee); mapTypeParameters(isConstructor ! ? classToConstructorArgTypeParam ! : classToMethodArgTypeParam, ! pType, ee); } } mapAnnotations(isConstructor ? classToConstructorParamAnnotation ! : classToMethodParamAnnotation, ! param, ee); } ! for (TypeMirror anException : ee.getThrownTypes()) { ! SimpleTypeVisitor9<Void, ExecutableElement> stv = new SimpleTypeVisitor9<Void, ExecutableElement>() { ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitArray(ArrayType t, ExecutableElement p) { ! super.visit(t.getComponentType(), p); ! return null; ! } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitDeclared(DeclaredType t, ExecutableElement p) { add(isConstructor ? classToConstructorThrows : classToMethodThrows, ! (TypeElement) t.asElement(), p); ! return null; } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Void visitError(ErrorType t, ExecutableElement p) { + add(isConstructor ? classToConstructorThrows : classToMethodThrows, + (TypeElement) t.asElement(), p); + return null; } ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! protected Void defaultAction(TypeMirror e, ExecutableElement p) { ! throw new AssertionError("this should not happen"); ! } ! }; ! ! stv.visit(typeutils.erasure(anException), ee); ! } ! } ! ! private <T> List<T> refList(Map<TypeElement, List<T>> map, Element element) { ! List<T> list = map.get(element); if (list == null) { list = new ArrayList<>(); ! map.put((TypeElement) element, list); } return list; } ! private Set<PackageElement> packageSet(TypeElement te) { ! Set<PackageElement> pkgSet = classToPackage.get(te); if (pkgSet == null) { ! pkgSet = new TreeSet<>(utils.makeClassUseComparator()); ! classToPackage.put(te, pkgSet); } return pkgSet; } ! private Set<TypeElement> classSet(TypeElement te) { ! Set<TypeElement> clsSet = classToClass.get(te); if (clsSet == null) { ! clsSet = new TreeSet<>(utils.makeClassUseComparator()); ! classToClass.put(te, clsSet); } return clsSet; } ! private <T extends Element> void add(Map<TypeElement, List<T>> map, TypeElement te, T ref) { // add to specified map ! refList(map, te).add(ref); // add ref's package to package map and class map ! packageSet(te).add(elements.getPackageOf(ref)); ! TypeElement entry = (utils.isField((Element) ref) ! || utils.isConstructor((Element) ref) ! || utils.isMethod((Element) ref)) ! ? (TypeElement) ref.getEnclosingElement() ! : (TypeElement) ref; ! classSet(te).add(entry); } ! private void addAll(Map<TypeElement, List<TypeElement>> map, TypeElement te, Collection<TypeElement> refs) { if (refs == null) { return; } // add to specified map ! refList(map, te).addAll(refs); ! Set<PackageElement> pkgSet = packageSet(te); ! Set<TypeElement> clsSet = classSet(te); // add ref's package to package map and class map ! for (TypeElement cls : refs) { ! pkgSet.add(utils.containingPackage(cls)); clsSet.add(cls); } } /** ! * Map the TypeElements to the members that use them as type parameters. * * @param map the map the insert the information into. ! * @param element the te whose type parameters are being checked. * @param holder the holder that owns the type parameters. */ ! private <T extends Element> void mapTypeParameters(final Map<TypeElement, List<T>> map, ! Element element, final T holder) { ! ! SimpleElementVisitor9<Void, Void> elementVisitor ! = new SimpleElementVisitor9<Void, Void>() { ! ! private void addParameters(TypeParameterElement e) { ! for (TypeMirror type : utils.getBounds(e)) { ! addTypeParameterToMap(map, type, holder); } } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitType(TypeElement e, Void p) { ! for (TypeParameterElement param : e.getTypeParameters()) { ! addParameters(param); } ! return null; } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitExecutable(ExecutableElement e, Void p) { ! for (TypeParameterElement param : e.getTypeParameters()) { ! addParameters(param); ! } ! return null; ! } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! protected Void defaultAction(Element e, Void p) { ! mapTypeParameters(map, e.asType(), holder); ! return null; ! } ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitTypeParameter(TypeParameterElement e, Void p) { ! addParameters(e); ! return null; ! } ! }; ! elementVisitor.visit(element); ! } ! ! private <T extends Element> void mapTypeParameters(final Map<TypeElement, List<T>> map, ! TypeMirror aType, final T holder) { ! ! SimpleTypeVisitor9<Void, Void> tv = new SimpleTypeVisitor9<Void, Void>() { ! ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitWildcard(WildcardType t, Void p) { ! TypeMirror bound = t.getExtendsBound(); ! if (bound != null) { addTypeParameterToMap(map, bound, holder); } + bound = t.getSuperBound(); + if (bound != null) { + addTypeParameterToMap(map, bound, holder); } + return null; } ! // ParameterizedType ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitDeclared(DeclaredType t, Void p) { ! for (TypeMirror targ : t.getTypeArguments()) { ! addTypeParameterToMap(map, targ, holder); } ! return null; } + }; + tv.visit(aType); } /** ! * Map the AnnotationType to the members that use them as type parameters. * * @param map the map the insert the information into. ! * @param element whose type parameters are being checked. * @param holder the holder that owns the type parameters. */ ! private <T extends Element> void mapAnnotations(final Map<TypeElement, List<T>> map, ! Element e, final T holder) { ! new SimpleElementVisitor9<Void, Void>() { ! ! void addAnnotations(Element e) { ! for (AnnotationMirror a : e.getAnnotationMirrors()) { ! add(map, (TypeElement) a.getAnnotationType().asElement(), holder); } } ! @Override @DefinedBy(Api.LANGUAGE_MODEL) ! public Void visitPackage(PackageElement e, Void p) { ! for (AnnotationMirror a : e.getAnnotationMirrors()) { ! refList(map, a.getAnnotationType().asElement()).add(holder); } + return null; + } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + protected Void defaultAction(Element e, Void p) { + addAnnotations(e); + return null; + } + }.visit(e); + } + + private <T extends Element> void addTypeParameterToMap(final Map<TypeElement, List<T>> map, + TypeMirror type, final T holder) { + new SimpleTypeVisitor9<Void, Void>() { + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + protected Void defaultAction(TypeMirror e, Void p) { + return super.defaultAction(e, p); + } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Void visitDeclared(DeclaredType t, Void p) { + add(map, (TypeElement) t.asElement(), holder); + return null; + } + + }.visit(type); mapTypeParameters(map, type, holder); } }