src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ImplementedMethods.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 74
5a9172b251dd
child 184
905e151a185a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.doclets.internal.toolkit.util;
    28 import com.sun.javadoc.*;
    29 import com.sun.tools.doclets.internal.toolkit.Configuration;
    30 import java.util.*;
    32 /**
    33  * For a given class method, build an array of interface methods which it
    34  * implements.
    35  *
    36  * This code is not part of an API.
    37  * It is implementation that is subject to change.
    38  * Do not use it as an API
    39  *
    40  * @author Atul M Dambalkar
    41  */
    42 public class ImplementedMethods {
    44     private Map<MethodDoc,Type> interfaces = new HashMap<MethodDoc,Type>();
    45     private List<MethodDoc> methlist = new ArrayList<MethodDoc>();
    46     private Configuration configuration;
    47     private final ClassDoc classdoc;
    48     private final MethodDoc method;
    50     public ImplementedMethods(MethodDoc method, Configuration configuration) {
    51         this.method = method;
    52         this.configuration = configuration;
    53         classdoc = method.containingClass();
    54     }
    56     /**
    57      * Return the array of interface methods which the method passed in the
    58      * constructor is implementing. The search/build order is as follows:
    59      * <pre>
    60      * 1. Search in all the immediate interfaces which this method's class is
    61      *    implementing. Do it recursively for the superinterfaces as well.
    62      * 2. Traverse all the superclasses and search recursively in the
    63      *    interfaces which those superclasses implement.
    64      *</pre>
    65      *
    66      * @return MethodDoc[] Array of implemented methods.
    67      */
    68     public MethodDoc[] build(boolean sort) {
    69         buildImplementedMethodList(sort);
    70         return methlist.toArray(new MethodDoc[methlist.size()]);
    71     }
    73     public MethodDoc[] build() {
    74         return build(true);
    75     }
    77     public Type getMethodHolder(MethodDoc methodDoc) {
    78         return interfaces.get(methodDoc);
    79     }
    81     /**
    82      * Search for the method in the array of interfaces. If found check if it is
    83      * overridden by any other subinterface method which this class
    84      * implements. If it is not overidden, add it in the method list.
    85      * Do this recursively for all the extended interfaces for each interface
    86      * from the array passed.
    87      */
    88     private void buildImplementedMethodList(boolean sort) {
    89         List intfacs = Util.getAllInterfaces(classdoc, configuration, sort);
    90         for (Iterator iter = intfacs.iterator(); iter.hasNext(); ) {
    91             Type interfaceType = (Type) iter.next();
    92             MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method);
    93             if (found != null) {
    94                 removeOverriddenMethod(found);
    95                 if (!overridingMethodFound(found)) {
    96                     methlist.add(found);
    97                     interfaces.put(found, interfaceType);
    98                 }
    99             }
   100         }
   101     }
   103     /**
   104      * Search in the method list and check if it contains a method which
   105      * is overridden by the method as parameter.  If found, remove the
   106      * overridden method from the method list.
   107      *
   108      * @param method Is this method overriding a method in the method list.
   109      */
   110     private void removeOverriddenMethod(MethodDoc method) {
   111         ClassDoc overriddenClass = method.overriddenClass();
   112         if (overriddenClass != null) {
   113             for (int i = 0; i < methlist.size(); i++) {
   114                 ClassDoc cd = methlist.get(i).containingClass();
   115                 if (cd == overriddenClass || overriddenClass.subclassOf(cd)) {
   116                     methlist.remove(i);  // remove overridden method
   117                     return;
   118                 }
   119             }
   120         }
   121     }
   123     /**
   124      * Search in the already found methods' list and check if it contains
   125      * a method which is overriding the method parameter or is the method
   126      * parameter itself.
   127      *
   128      * @param method MethodDoc Method to be searched in the Method List for
   129      * an overriding method.
   130      */
   131     private boolean overridingMethodFound(MethodDoc method) {
   132         ClassDoc containingClass = method.containingClass();
   133         for (int i = 0; i < methlist.size(); i++) {
   134             MethodDoc listmethod = methlist.get(i);
   135             if (containingClass == listmethod.containingClass()) {
   136                 // it's the same method.
   137                 return true;
   138             }
   139             ClassDoc cd = listmethod.overriddenClass();
   140             if (cd == null) {
   141                 continue;
   142             }
   143             if (cd == containingClass || cd.subclassOf(containingClass)) {
   144                 return true;
   145             }
   146         }
   147         return false;
   148     }
   149 }

mercurial