duke@1: /* jjg@1357: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.util; duke@1: jjg@1357: import java.util.*; jjg@1357: duke@1: import com.sun.javadoc.*; duke@1: import com.sun.tools.doclets.internal.toolkit.Configuration; duke@1: duke@1: /** duke@1: * For a given class method, build an array of interface methods which it duke@1: * implements. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. duke@1: * duke@1: * @author Atul M Dambalkar duke@1: */ duke@1: public class ImplementedMethods { duke@1: jjg@74: private Map interfaces = new HashMap(); jjg@74: private List methlist = new ArrayList(); duke@1: private Configuration configuration; duke@1: private final ClassDoc classdoc; duke@1: private final MethodDoc method; duke@1: duke@1: public ImplementedMethods(MethodDoc method, Configuration configuration) { duke@1: this.method = method; duke@1: this.configuration = configuration; duke@1: classdoc = method.containingClass(); duke@1: } duke@1: duke@1: /** duke@1: * Return the array of interface methods which the method passed in the duke@1: * constructor is implementing. The search/build order is as follows: duke@1: *

duke@1:      * 1. Search in all the immediate interfaces which this method's class is
duke@1:      *    implementing. Do it recursively for the superinterfaces as well.
duke@1:      * 2. Traverse all the superclasses and search recursively in the
duke@1:      *    interfaces which those superclasses implement.
duke@1:      *
duke@1: * duke@1: * @return MethodDoc[] Array of implemented methods. duke@1: */ duke@1: public MethodDoc[] build(boolean sort) { duke@1: buildImplementedMethodList(sort); jjg@74: return methlist.toArray(new MethodDoc[methlist.size()]); duke@1: } duke@1: duke@1: public MethodDoc[] build() { duke@1: return build(true); duke@1: } duke@1: duke@1: public Type getMethodHolder(MethodDoc methodDoc) { jjg@74: return interfaces.get(methodDoc); duke@1: } duke@1: duke@1: /** duke@1: * Search for the method in the array of interfaces. If found check if it is duke@1: * overridden by any other subinterface method which this class duke@1: * implements. If it is not overidden, add it in the method list. duke@1: * Do this recursively for all the extended interfaces for each interface duke@1: * from the array passed. duke@1: */ duke@1: private void buildImplementedMethodList(boolean sort) { mcimadamore@184: List intfacs = Util.getAllInterfaces(classdoc, configuration, sort); mcimadamore@184: for (Iterator iter = intfacs.iterator(); iter.hasNext(); ) { mcimadamore@184: Type interfaceType = iter.next(); duke@1: MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method); duke@1: if (found != null) { duke@1: removeOverriddenMethod(found); duke@1: if (!overridingMethodFound(found)) { duke@1: methlist.add(found); duke@1: interfaces.put(found, interfaceType); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Search in the method list and check if it contains a method which duke@1: * is overridden by the method as parameter. If found, remove the duke@1: * overridden method from the method list. duke@1: * duke@1: * @param method Is this method overriding a method in the method list. duke@1: */ duke@1: private void removeOverriddenMethod(MethodDoc method) { duke@1: ClassDoc overriddenClass = method.overriddenClass(); duke@1: if (overriddenClass != null) { duke@1: for (int i = 0; i < methlist.size(); i++) { jjg@74: ClassDoc cd = methlist.get(i).containingClass(); duke@1: if (cd == overriddenClass || overriddenClass.subclassOf(cd)) { duke@1: methlist.remove(i); // remove overridden method duke@1: return; duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Search in the already found methods' list and check if it contains duke@1: * a method which is overriding the method parameter or is the method duke@1: * parameter itself. duke@1: * duke@1: * @param method MethodDoc Method to be searched in the Method List for duke@1: * an overriding method. duke@1: */ duke@1: private boolean overridingMethodFound(MethodDoc method) { duke@1: ClassDoc containingClass = method.containingClass(); duke@1: for (int i = 0; i < methlist.size(); i++) { jjg@74: MethodDoc listmethod = methlist.get(i); duke@1: if (containingClass == listmethod.containingClass()) { duke@1: // it's the same method. duke@1: return true; duke@1: } duke@1: ClassDoc cd = listmethod.overriddenClass(); duke@1: if (cd == null) { duke@1: continue; duke@1: } duke@1: if (cd == containingClass || cd.subclassOf(containingClass)) { duke@1: return true; duke@1: } duke@1: } duke@1: return false; duke@1: } duke@1: }