src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/ImplementedMethods.java
branchepsilon-gc-branch
changeset 56489 016b77c3734a
parent 56473 63a5ea2cdd0d
parent 49900 770679787db5
child 56490 0d0e6b083a3f
equal deleted inserted replaced
56473:63a5ea2cdd0d 56489:016b77c3734a
     1 /*
       
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.javadoc.internal.doclets.toolkit.util;
       
    27 
       
    28 import java.util.*;
       
    29 
       
    30 import javax.lang.model.element.ExecutableElement;
       
    31 import javax.lang.model.element.TypeElement;
       
    32 import javax.lang.model.type.TypeMirror;
       
    33 
       
    34 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
       
    35 
       
    36 /**
       
    37  * For a given class method, build an array of interface methods which it
       
    38  * implements.
       
    39  *
       
    40  *  <p><b>This is NOT part of any supported API.
       
    41  *  If you write code that depends on this, you do so at your own risk.
       
    42  *  This code and its internal interfaces are subject to change or
       
    43  *  deletion without notice.</b>
       
    44  *
       
    45  * @author Atul M Dambalkar
       
    46  */
       
    47 public class ImplementedMethods {
       
    48 
       
    49     private final Map<ExecutableElement, TypeMirror> interfaces = new HashMap<>();
       
    50     private final List<ExecutableElement> methlist = new ArrayList<>();
       
    51     private final Utils utils;
       
    52     private final TypeElement typeElement;
       
    53     private final ExecutableElement method;
       
    54 
       
    55     public ImplementedMethods(ExecutableElement method, BaseConfiguration configuration) {
       
    56         this.method = method;
       
    57         this.utils = configuration.utils;
       
    58         typeElement = utils.getEnclosingTypeElement(method);
       
    59     }
       
    60 
       
    61     /**
       
    62      * Return the array of interface methods which the method passed in the
       
    63      * constructor is implementing. The search/build order is as follows:
       
    64      * <pre>
       
    65      * 1. Search in all the immediate interfaces which this method's class is
       
    66      *    implementing. Do it recursively for the superinterfaces as well.
       
    67      * 2. Traverse all the superclasses and search recursively in the
       
    68      *    interfaces which those superclasses implement.
       
    69      *</pre>
       
    70      *
       
    71      * @return SortedSet<ExecutableElement> of implemented methods.
       
    72      */
       
    73     public List<ExecutableElement> build() {
       
    74         buildImplementedMethodList();
       
    75         return methlist;
       
    76     }
       
    77 
       
    78     public TypeMirror getMethodHolder(ExecutableElement ee) {
       
    79         return interfaces.get(ee);
       
    80     }
       
    81 
       
    82     /**
       
    83      * Search for the method in the array of interfaces. If found check if it is
       
    84      * overridden by any other subinterface method which this class
       
    85      * implements. If it is not overidden, add it in the method list.
       
    86      * Do this recursively for all the extended interfaces for each interface
       
    87      * from the array passed.
       
    88      */
       
    89     private void buildImplementedMethodList() {
       
    90         Set<TypeMirror> intfacs = utils.getAllInterfaces(typeElement);
       
    91         for (TypeMirror interfaceType : intfacs) {
       
    92             ExecutableElement found = utils.findMethod(utils.asTypeElement(interfaceType), 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     }
       
   102 
       
   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(ExecutableElement method) {
       
   111         TypeElement overriddenClass = utils.overriddenClass(method);
       
   112         if (overriddenClass != null) {
       
   113             for (int i = 0; i < methlist.size(); i++) {
       
   114                 TypeElement te = utils.getEnclosingTypeElement(methlist.get(i));
       
   115                 if (te == overriddenClass || utils.isSubclassOf(overriddenClass, te)) {
       
   116                     methlist.remove(i);  // remove overridden method
       
   117                     return;
       
   118                 }
       
   119             }
       
   120         }
       
   121     }
       
   122 
       
   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(ExecutableElement method) {
       
   132         TypeElement containingClass = utils.getEnclosingTypeElement(method);
       
   133         for (ExecutableElement listmethod : methlist) {
       
   134             if (containingClass == utils.getEnclosingTypeElement(listmethod)) {
       
   135                 // it's the same method.
       
   136                 return true;
       
   137             }
       
   138             TypeElement te = utils.overriddenClass(listmethod);
       
   139             if (te == null) {
       
   140                 continue;
       
   141             }
       
   142             if (te == containingClass || utils.isSubclassOf(te, containingClass)) {
       
   143                 return true;
       
   144             }
       
   145         }
       
   146         return false;
       
   147     }
       
   148 }