langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/ImplementedMethods.java
author prr
Thu, 11 Aug 2016 10:38:13 -0700
changeset 40489 4572e3dce7ba
parent 35426 374342e56a56
child 45417 f7479ee8de69
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit.util;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    30
import javax.lang.model.element.ExecutableElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    31
import javax.lang.model.element.TypeElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    32
import javax.lang.model.type.TypeMirror;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    33
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    34
import jdk.javadoc.internal.doclets.toolkit.Configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * For a given class method, build an array of interface methods which it
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * implements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    40
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    41
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    42
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    43
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class ImplementedMethods {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    49
    private final Map<ExecutableElement, TypeMirror> interfaces = new HashMap<>();
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    50
    private final List<ExecutableElement> methlist = new ArrayList<>();
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    51
    private final Utils utils;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    52
    private final TypeElement typeElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    53
    private final ExecutableElement method;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    55
    public ImplementedMethods(ExecutableElement method, Configuration configuration) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        this.method = method;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    57
        this.utils = configuration.utils;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    58
        typeElement = utils.getEnclosingTypeElement(method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Return the array of interface methods which the method passed in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * constructor is implementing. The search/build order is as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * 1. Search in all the immediate interfaces which this method's class is
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     *    implementing. Do it recursively for the superinterfaces as well.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * 2. Traverse all the superclasses and search recursively in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     *    interfaces which those superclasses implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *</pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    71
     * @return SortedSet<ExecutableElement> of implemented methods.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    73
    public List<ExecutableElement> build() {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    74
        buildImplementedMethodList();
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    75
        return methlist;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    78
    public TypeMirror getMethodHolder(ExecutableElement ee) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    79
        return interfaces.get(ee);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * Search for the method in the array of interfaces. If found check if it is
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * overridden by any other subinterface method which this class
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * implements. If it is not overidden, add it in the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Do this recursively for all the extended interfaces for each interface
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * from the array passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    89
    private void buildImplementedMethodList() {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    90
        Set<TypeMirror> intfacs = utils.getAllInterfaces(typeElement);
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    91
        for (TypeMirror interfaceType : intfacs) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    92
            ExecutableElement found = utils.findMethod(utils.asTypeElement(interfaceType), method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            if (found != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                removeOverriddenMethod(found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
                if (!overridingMethodFound(found)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                    methlist.add(found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                    interfaces.put(found, interfaceType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * Search in the method list and check if it contains a method which
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * is overridden by the method as parameter.  If found, remove the
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * overridden method from the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * @param method Is this method overriding a method in the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   110
    private void removeOverriddenMethod(ExecutableElement method) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   111
        TypeElement overriddenClass = utils.overriddenClass(method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        if (overriddenClass != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            for (int i = 0; i < methlist.size(); i++) {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   114
                TypeElement te = utils.getEnclosingTypeElement(methlist.get(i));
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   115
                if (te == overriddenClass || utils.isSubclassOf(overriddenClass, te)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                    methlist.remove(i);  // remove overridden method
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * Search in the already found methods' list and check if it contains
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * a method which is overriding the method parameter or is the method
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * parameter itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * @param method MethodDoc Method to be searched in the Method List for
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * an overriding method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   131
    private boolean overridingMethodFound(ExecutableElement method) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   132
        TypeElement containingClass = utils.getEnclosingTypeElement(method);
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   133
        for (ExecutableElement listmethod : methlist) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   134
            if (containingClass == utils.getEnclosingTypeElement(listmethod)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                // it's the same method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   138
            TypeElement te = utils.overriddenClass(listmethod);
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   139
            if (te == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   142
            if (te == containingClass || utils.isSubclassOf(te, containingClass)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
}