langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ImplementedMethods.java
author ksrini
Sun, 15 Jun 2014 08:41:57 -0700
changeset 25454 376a52c9540c
parent 22163 3651128c74eb
permissions -rw-r--r--
8039028: [javadoc] refactor the usage of Util.java Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
     2
 * Copyright (c) 1999, 2014, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
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
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.Configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * For a given class method, build an array of interface methods which it
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * implements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    38
 *  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
    39
 *  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
    40
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
public class ImplementedMethods {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    46
    private final Map<MethodDoc,Type> interfaces = new HashMap<>();
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    47
    private final List<MethodDoc> methlist = new ArrayList<>();
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    48
    private final Configuration configuration;
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    49
    private final Utils utils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final ClassDoc classdoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private final MethodDoc method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public ImplementedMethods(MethodDoc method, Configuration configuration) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        this.method = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        this.configuration = configuration;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    56
        this.utils = configuration.utils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        classdoc = method.containingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * Return the array of interface methods which the method passed in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * constructor is implementing. The search/build order is as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * 1. Search in all the immediate interfaces which this method's class is
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     *    implementing. Do it recursively for the superinterfaces as well.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * 2. Traverse all the superclasses and search recursively in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     *    interfaces which those superclasses implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     *</pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @return MethodDoc[] Array of implemented methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public MethodDoc[] build(boolean sort) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        buildImplementedMethodList(sort);
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    74
        return methlist.toArray(new MethodDoc[methlist.size()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public MethodDoc[] build() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return build(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public Type getMethodHolder(MethodDoc methodDoc) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    82
        return interfaces.get(methodDoc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Search for the method in the array of interfaces. If found check if it is
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * overridden by any other subinterface method which this class
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * implements. If it is not overidden, add it in the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * Do this recursively for all the extended interfaces for each interface
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * from the array passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    private void buildImplementedMethodList(boolean sort) {
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    93
        List<Type> intfacs = utils.getAllInterfaces(classdoc, configuration, sort);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14260
diff changeset
    94
        for (Type interfaceType : intfacs) {
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22163
diff changeset
    95
            MethodDoc found = utils.findMethod(interfaceType.asClassDoc(), method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            if (found != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                removeOverriddenMethod(found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                if (!overridingMethodFound(found)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                    methlist.add(found);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                    interfaces.put(found, interfaceType);
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
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Search in the method list and check if it contains a method which
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * is overridden by the method as parameter.  If found, remove the
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * overridden method from the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * @param method Is this method overriding a method in the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    private void removeOverriddenMethod(MethodDoc method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        ClassDoc overriddenClass = method.overriddenClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (overriddenClass != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            for (int i = 0; i < methlist.size(); i++) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   117
                ClassDoc cd = methlist.get(i).containingClass();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                if (cd == overriddenClass || overriddenClass.subclassOf(cd)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                    methlist.remove(i);  // remove overridden method
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                    return;
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
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * Search in the already found methods' list and check if it contains
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * a method which is overriding the method parameter or is the method
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * parameter itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * @param method MethodDoc Method to be searched in the Method List for
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * an overriding method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private boolean overridingMethodFound(MethodDoc method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        ClassDoc containingClass = method.containingClass();
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14260
diff changeset
   136
        for (MethodDoc listmethod : methlist) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            if (containingClass == listmethod.containingClass()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                // it's the same method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            ClassDoc cd = listmethod.overriddenClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            if (cd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            if (cd == containingClass || cd.subclassOf(containingClass)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
}