langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ClassUseMapper.java
changeset 22159 682da512ec17
parent 19667 fdfce85627a9
child 22163 3651128c74eb
equal deleted inserted replaced
22158:dca1b80ed62c 22159:682da512ec17
   184 
   184 
   185     public ClassUseMapper(RootDoc root, ClassTree classtree) {
   185     public ClassUseMapper(RootDoc root, ClassTree classtree) {
   186         this.classtree = classtree;
   186         this.classtree = classtree;
   187 
   187 
   188         // Map subclassing, subinterfacing implementing, ...
   188         // Map subclassing, subinterfacing implementing, ...
   189         for (Iterator<ClassDoc> it = classtree.baseclasses().iterator(); it.hasNext();) {
   189         for (ClassDoc doc : classtree.baseclasses()) {
   190             subclasses(it.next());
   190             subclasses(doc);
   191         }
   191         }
   192         for (Iterator<ClassDoc> it = classtree.baseinterfaces().iterator(); it.hasNext();) {
   192         for (ClassDoc doc : classtree.baseinterfaces()) {
   193             // does subinterfacing as side-effect
   193             // does subinterfacing as side-effect
   194             implementingClasses(it.next());
   194             implementingClasses(doc);
   195         }
   195         }
   196         // Map methods, fields, constructors using a class.
   196         // Map methods, fields, constructors using a class.
   197         ClassDoc[] classes = root.classes();
   197         ClassDoc[] classes = root.classes();
   198         for (int i = 0; i < classes.length; i++) {
   198         for (ClassDoc aClass : classes) {
   199             PackageDoc pkg = classes[i].containingPackage();
   199             PackageDoc pkg = aClass.containingPackage();
   200             mapAnnotations(classToPackageAnnotations, pkg, pkg);
   200             mapAnnotations(classToPackageAnnotations, pkg, pkg);
   201             ClassDoc cd = classes[i];
   201             ClassDoc cd = aClass;
   202             mapTypeParameters(classToClassTypeParam, cd, cd);
   202             mapTypeParameters(classToClassTypeParam, cd, cd);
   203             mapAnnotations(classToClassAnnotations, cd, cd);
   203             mapAnnotations(classToClassAnnotations, cd, cd);
   204             FieldDoc[] fields = cd.fields();
   204             FieldDoc[] fields = cd.fields();
   205             for (int j = 0; j < fields.length; j++) {
   205             for (FieldDoc fd : fields) {
   206                 FieldDoc fd = fields[j];
       
   207                 mapTypeParameters(classToFieldDocTypeParam, fd, fd);
   206                 mapTypeParameters(classToFieldDocTypeParam, fd, fd);
   208                 mapAnnotations(annotationToFieldDoc, fd, fd);
   207                 mapAnnotations(annotationToFieldDoc, fd, fd);
   209                 if (! fd.type().isPrimitive()) {
   208                 if (!fd.type().isPrimitive()) {
   210                     add(classToField, fd.type().asClassDoc(), fd);
   209                     add(classToField, fd.type().asClassDoc(), fd);
   211                 }
   210                 }
   212             }
   211             }
   213             ConstructorDoc[] cons = cd.constructors();
   212             ConstructorDoc[] cons = cd.constructors();
   214             for (int j = 0; j < cons.length; j++) {
   213             for (ConstructorDoc con : cons) {
   215                 mapAnnotations(classToConstructorAnnotations, cons[j], cons[j]);
   214                 mapAnnotations(classToConstructorAnnotations, con, con);
   216                 mapExecutable(cons[j]);
   215                 mapExecutable(con);
   217             }
   216             }
   218             MethodDoc[] meths = cd.methods();
   217             MethodDoc[] meths = cd.methods();
   219             for (int j = 0; j < meths.length; j++) {
   218             for (MethodDoc md : meths) {
   220                 MethodDoc md = meths[j];
       
   221                 mapExecutable(md);
   219                 mapExecutable(md);
   222                 mapTypeParameters(classToExecMemberDocTypeParam, md, md);
   220                 mapTypeParameters(classToExecMemberDocTypeParam, md, md);
   223                 mapAnnotations(classToExecMemberDocAnnotations, md, md);
   221                 mapAnnotations(classToExecMemberDocAnnotations, md, md);
   224                 if (! (md.returnType().isPrimitive() || md.returnType() instanceof TypeVariable)) {
   222                 if (!(md.returnType().isPrimitive() || md.returnType() instanceof TypeVariable)) {
   225                     mapTypeParameters(classToExecMemberDocReturnTypeParam,
   223                     mapTypeParameters(classToExecMemberDocReturnTypeParam,
   226                         md.returnType(), md);
   224                                       md.returnType(), md);
   227                     add(classToMethodReturn, md.returnType().asClassDoc(), md);
   225                     add(classToMethodReturn, md.returnType().asClassDoc(), md);
   228                 }
   226                 }
   229             }
   227             }
   230         }
   228         }
   231     }
   229     }
   238         if (ret == null) {
   236         if (ret == null) {
   239             ret = new TreeSet<ClassDoc>();
   237             ret = new TreeSet<ClassDoc>();
   240             List<ClassDoc> subs = classtree.subclasses(cd);
   238             List<ClassDoc> subs = classtree.subclasses(cd);
   241             if (subs != null) {
   239             if (subs != null) {
   242                 ret.addAll(subs);
   240                 ret.addAll(subs);
   243                 for (Iterator<ClassDoc> it = subs.iterator(); it.hasNext();) {
   241                 for (ClassDoc sub : subs) {
   244                     ret.addAll(subclasses(it.next()));
   242                     ret.addAll(subclasses(sub));
   245                 }
   243                 }
   246             }
   244             }
   247             addAll(classToSubclass, cd, ret);
   245             addAll(classToSubclass, cd, ret);
   248         }
   246         }
   249         return ret;
   247         return ret;
   257         if (ret == null) {
   255         if (ret == null) {
   258             ret = new TreeSet<ClassDoc>();
   256             ret = new TreeSet<ClassDoc>();
   259             List<ClassDoc> subs = classtree.subinterfaces(cd);
   257             List<ClassDoc> subs = classtree.subinterfaces(cd);
   260             if (subs != null) {
   258             if (subs != null) {
   261                 ret.addAll(subs);
   259                 ret.addAll(subs);
   262                 for (Iterator<ClassDoc> it = subs.iterator(); it.hasNext();) {
   260                 for (ClassDoc sub : subs) {
   263                     ret.addAll(subinterfaces(it.next()));
   261                     ret.addAll(subinterfaces(sub));
   264                 }
   262                 }
   265             }
   263             }
   266             addAll(classToSubinterface, cd, ret);
   264             addAll(classToSubinterface, cd, ret);
   267         }
   265         }
   268         return ret;
   266         return ret;
   279         if (ret == null) {
   277         if (ret == null) {
   280             ret = new TreeSet<ClassDoc>();
   278             ret = new TreeSet<ClassDoc>();
   281             List<ClassDoc> impl = classtree.implementingclasses(cd);
   279             List<ClassDoc> impl = classtree.implementingclasses(cd);
   282             if (impl != null) {
   280             if (impl != null) {
   283                 ret.addAll(impl);
   281                 ret.addAll(impl);
   284                 for (Iterator<ClassDoc> it = impl.iterator(); it.hasNext();) {
   282                 for (ClassDoc anImpl : impl) {
   285                     ret.addAll(subclasses(it.next()));
   283                     ret.addAll(subclasses(anImpl));
   286                 }
   284                 }
   287             }
   285             }
   288             for (Iterator<ClassDoc> it = subinterfaces(cd).iterator(); it.hasNext();) {
   286             for (ClassDoc doc : subinterfaces(cd)) {
   289                 ret.addAll(implementingClasses(it.next()));
   287                 ret.addAll(implementingClasses(doc));
   290             }
   288             }
   291             addAll(classToImplementingClass, cd, ret);
   289             addAll(classToImplementingClass, cd, ret);
   292         }
   290         }
   293         return ret;
   291         return ret;
   294     }
   292     }
   296     /**
   294     /**
   297      * Determine classes used by a method or constructor, so they can be
   295      * Determine classes used by a method or constructor, so they can be
   298      * inverse mapped.
   296      * inverse mapped.
   299      */
   297      */
   300     private void mapExecutable(ExecutableMemberDoc em) {
   298     private void mapExecutable(ExecutableMemberDoc em) {
   301         Parameter[] params = em.parameters();
       
   302         boolean isConstructor = em.isConstructor();
   299         boolean isConstructor = em.isConstructor();
   303         List<Type> classArgs = new ArrayList<Type>();
   300         List<Type> classArgs = new ArrayList<Type>();
   304         for (int k = 0; k < params.length; k++) {
   301         for (Parameter param : em.parameters()) {
   305             Type pcd = params[k].type();
   302             Type pcd = param.type();
   306             // primitives don't get mapped, also avoid dups
   303             // primitives don't get mapped, also avoid dups
   307             if ((! params[k].type().isPrimitive()) &&
   304             if ((!param.type().isPrimitive()) &&
   308                  ! classArgs.contains(pcd) &&
   305                 !classArgs.contains(pcd) &&
   309                  ! (pcd instanceof TypeVariable)) {
   306                 !(pcd instanceof TypeVariable)) {
   310                 add(isConstructor? classToConstructorArgs :classToMethodArgs,
   307                 add(isConstructor ? classToConstructorArgs : classToMethodArgs,
   311                         pcd.asClassDoc(), em);
   308                     pcd.asClassDoc(), em);
   312                 classArgs.add(pcd);
   309                 classArgs.add(pcd);
   313                 mapTypeParameters(isConstructor?
   310                 mapTypeParameters(isConstructor ?
   314                    classToConstructorDocArgTypeParam : classToExecMemberDocArgTypeParam,
   311                                   classToConstructorDocArgTypeParam : classToExecMemberDocArgTypeParam,
   315                    pcd, em);
   312                                   pcd, em);
   316             }
   313             }
   317             mapAnnotations(
   314             mapAnnotations(
   318                 isConstructor ?
   315                     isConstructor ?
   319                     classToConstructorParamAnnotation :
   316                     classToConstructorParamAnnotation :
   320                     classToExecMemberDocParamAnnotation,
   317                     classToExecMemberDocParamAnnotation,
   321                 params[k], em);
   318                     param, em);
   322         }
   319         }
   323         ClassDoc[] thr = em.thrownExceptions();
   320         for (ClassDoc anException : em.thrownExceptions()) {
   324         for (int k = 0; k < thr.length; k++) {
   321             add(isConstructor ? classToConstructorThrows : classToMethodThrows,
   325             add(isConstructor? classToConstructorThrows : classToMethodThrows,
   322                 anException, em);
   326                     thr[k], em);
       
   327         }
   323         }
   328     }
   324     }
   329 
   325 
   330     private <T> List<T> refList(Map<String,List<T>> map, ClassDoc cd) {
   326     private <T> List<T> refList(Map<String,List<T>> map, ClassDoc cd) {
   331         List<T> list = map.get(cd.qualifiedName());
   327         List<T> list = map.get(cd.qualifiedName());
   376         refList(map, cd).addAll(refs);
   372         refList(map, cd).addAll(refs);
   377 
   373 
   378         Set<PackageDoc> pkgSet = packageSet(cd);
   374         Set<PackageDoc> pkgSet = packageSet(cd);
   379         Set<ClassDoc> clsSet = classSet(cd);
   375         Set<ClassDoc> clsSet = classSet(cd);
   380         // add ref's package to package map and class map
   376         // add ref's package to package map and class map
   381         for (Iterator<ClassDoc> it = refs.iterator(); it.hasNext();) {
   377         for (ClassDoc cls : refs) {
   382             ClassDoc cls = it.next();
       
   383             pkgSet.add(cls.containingPackage());
   378             pkgSet.add(cls.containingPackage());
   384             clsSet.add(cls);
   379             clsSet.add(cls);
   385 
   380 
   386         }
   381         }
   387     }
   382     }
   398             T holder) {
   393             T holder) {
   399         TypeVariable[] typeVariables;
   394         TypeVariable[] typeVariables;
   400         if (doc instanceof ClassDoc) {
   395         if (doc instanceof ClassDoc) {
   401             typeVariables = ((ClassDoc) doc).typeParameters();
   396             typeVariables = ((ClassDoc) doc).typeParameters();
   402         } else if (doc instanceof WildcardType) {
   397         } else if (doc instanceof WildcardType) {
   403             Type[] extendsBounds = ((WildcardType) doc).extendsBounds();
   398             for (Type extendsBound : ((WildcardType) doc).extendsBounds()) {
   404             for (int k = 0; k < extendsBounds.length; k++) {
   399                 addTypeParameterToMap(map, extendsBound, holder);
   405                 addTypeParameterToMap(map, extendsBounds[k], holder);
   400             }
   406             }
   401             for (Type superBound : ((WildcardType) doc).superBounds()) {
   407             Type[] superBounds = ((WildcardType) doc).superBounds();
   402                 addTypeParameterToMap(map, superBound, holder);
   408             for (int k = 0; k < superBounds.length; k++) {
       
   409                 addTypeParameterToMap(map, superBounds[k], holder);
       
   410             }
   403             }
   411             return;
   404             return;
   412         } else if (doc instanceof ParameterizedType) {
   405         } else if (doc instanceof ParameterizedType) {
   413             Type[] typeArguments = ((ParameterizedType) doc).typeArguments();
   406             for (Type typeArgument : ((ParameterizedType) doc).typeArguments()) {
   414             for (int k = 0; k < typeArguments.length; k++) {
   407                 addTypeParameterToMap(map, typeArgument, holder);
   415                 addTypeParameterToMap(map, typeArguments[k], holder);
       
   416             }
   408             }
   417             return;
   409             return;
   418         } else if (doc instanceof ExecutableMemberDoc) {
   410         } else if (doc instanceof ExecutableMemberDoc) {
   419             typeVariables = ((ExecutableMemberDoc) doc).typeParameters();
   411             typeVariables = ((ExecutableMemberDoc) doc).typeParameters();
   420         } else if (doc instanceof FieldDoc) {
   412         } else if (doc instanceof FieldDoc) {
   422             mapTypeParameters(map, fieldType, holder);
   414             mapTypeParameters(map, fieldType, holder);
   423             return;
   415             return;
   424         } else {
   416         } else {
   425             return;
   417             return;
   426         }
   418         }
   427         for (int i = 0; i < typeVariables.length; i++) {
   419         for (TypeVariable typeVariable : typeVariables) {
   428             Type[] bounds = typeVariables[i].bounds();
   420             for (Type bound : typeVariable.bounds()) {
   429             for (int j = 0; j < bounds.length; j++) {
   421                 addTypeParameterToMap(map, bound, holder);
   430                 addTypeParameterToMap(map, bounds[j], holder);
       
   431             }
   422             }
   432         }
   423         }
   433     }
   424     }
   434 
   425 
   435     /**
   426     /**
   452         } else if (doc instanceof Parameter) {
   443         } else if (doc instanceof Parameter) {
   453             annotations = ((Parameter) doc).annotations();
   444             annotations = ((Parameter) doc).annotations();
   454         } else {
   445         } else {
   455             throw new DocletAbortException("should not happen");
   446             throw new DocletAbortException("should not happen");
   456         }
   447         }
   457         for (int i = 0; i < annotations.length; i++) {
   448         for (AnnotationDesc annotation : annotations) {
   458             AnnotationTypeDoc annotationDoc = annotations[i].annotationType();
   449             AnnotationTypeDoc annotationDoc = annotation.annotationType();
   459             if (isPackage)
   450             if (isPackage)
   460                 refList(map, annotationDoc).add(holder);
   451                 refList(map, annotationDoc).add(holder);
   461             else
   452             else
   462                 add(map, annotationDoc, holder);
   453                 add(map, annotationDoc, holder);
   463         }
   454         }
   472      * @param doc the doc whose type parameters are being checked.
   463      * @param doc the doc whose type parameters are being checked.
   473      * @param holder the holder that owns the type parameters.
   464      * @param holder the holder that owns the type parameters.
   474      */
   465      */
   475     private <T extends PackageDoc> void mapAnnotations(Map<String,List<T>> map, PackageDoc doc,
   466     private <T extends PackageDoc> void mapAnnotations(Map<String,List<T>> map, PackageDoc doc,
   476             T holder) {
   467             T holder) {
   477         AnnotationDesc[] annotations;
   468         for (AnnotationDesc annotation : doc.annotations()) {
   478         annotations = doc.annotations();
   469             AnnotationTypeDoc annotationDoc = annotation.annotationType();
   479         for (int i = 0; i < annotations.length; i++) {
       
   480             AnnotationTypeDoc annotationDoc = annotations[i].annotationType();
       
   481             refList(map, annotationDoc).add(holder);
   470             refList(map, annotationDoc).add(holder);
   482         }
   471         }
   483     }
   472     }
   484 
   473 
   485     private <T extends ProgramElementDoc> void addTypeParameterToMap(Map<String,List<T>> map, Type type,
   474     private <T extends ProgramElementDoc> void addTypeParameterToMap(Map<String,List<T>> map, Type type,