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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1359
25e14ad23cef
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial