langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ImplementedMethods.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1264 076a3cde30d5
child 5520 86e4b9a9da40
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 868
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.doclets.internal.toolkit.Configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * For a given class method, build an array of interface methods which it
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * implements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
public class ImplementedMethods {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    44
    private Map<MethodDoc,Type> interfaces = new HashMap<MethodDoc,Type>();
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    45
    private List<MethodDoc> methlist = new ArrayList<MethodDoc>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private Configuration configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private final ClassDoc classdoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private final MethodDoc method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    public ImplementedMethods(MethodDoc method, Configuration configuration) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        this.method = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        classdoc = method.containingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * Return the array of interface methods which the method passed in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * constructor is implementing. The search/build order is as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * 1. Search in all the immediate interfaces which this method's class is
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     *    implementing. Do it recursively for the superinterfaces as well.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * 2. Traverse all the superclasses and search recursively in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     *    interfaces which those superclasses implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     *</pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * @return MethodDoc[] Array of implemented methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public MethodDoc[] build(boolean sort) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        buildImplementedMethodList(sort);
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    70
        return methlist.toArray(new MethodDoc[methlist.size()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public MethodDoc[] build() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        return build(true);
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 Type getMethodHolder(MethodDoc methodDoc) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    78
        return interfaces.get(methodDoc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Search for the method in the array of interfaces. If found check if it is
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * overridden by any other subinterface method which this class
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * implements. If it is not overidden, add it in the method list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * Do this recursively for all the extended interfaces for each interface
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * from the array passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private void buildImplementedMethodList(boolean sort) {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    89
        List<Type> intfacs = Util.getAllInterfaces(classdoc, configuration, sort);
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    90
        for (Iterator<Type> iter = intfacs.iterator(); iter.hasNext(); ) {
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    91
            Type interfaceType = iter.next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method);
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
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    private void removeOverriddenMethod(MethodDoc method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        ClassDoc overriddenClass = method.overriddenClass();
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++) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   114
                ClassDoc cd = methlist.get(i).containingClass();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                if (cd == overriddenClass || overriddenClass.subclassOf(cd)) {
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
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    private boolean overridingMethodFound(MethodDoc method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        ClassDoc containingClass = method.containingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < methlist.size(); i++) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   134
            MethodDoc listmethod = methlist.get(i);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            if (containingClass == listmethod.containingClass()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                // it's the same method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            ClassDoc cd = listmethod.overriddenClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            if (cd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            if (cd == containingClass || cd.subclassOf(containingClass)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
}