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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial