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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ImplementedMethods.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,151 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.doclets.internal.toolkit.util;
    1.30 +
    1.31 +import java.util.*;
    1.32 +
    1.33 +import com.sun.javadoc.*;
    1.34 +import com.sun.tools.doclets.internal.toolkit.Configuration;
    1.35 +
    1.36 +/**
    1.37 + * For a given class method, build an array of interface methods which it
    1.38 + * implements.
    1.39 + *
    1.40 + *  <p><b>This is NOT part of any supported API.
    1.41 + *  If you write code that depends on this, you do so at your own risk.
    1.42 + *  This code and its internal interfaces are subject to change or
    1.43 + *  deletion without notice.</b>
    1.44 + *
    1.45 + * @author Atul M Dambalkar
    1.46 + */
    1.47 +public class ImplementedMethods {
    1.48 +
    1.49 +    private Map<MethodDoc,Type> interfaces = new HashMap<MethodDoc,Type>();
    1.50 +    private List<MethodDoc> methlist = new ArrayList<MethodDoc>();
    1.51 +    private Configuration configuration;
    1.52 +    private final ClassDoc classdoc;
    1.53 +    private final MethodDoc method;
    1.54 +
    1.55 +    public ImplementedMethods(MethodDoc method, Configuration configuration) {
    1.56 +        this.method = method;
    1.57 +        this.configuration = configuration;
    1.58 +        classdoc = method.containingClass();
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Return the array of interface methods which the method passed in the
    1.63 +     * constructor is implementing. The search/build order is as follows:
    1.64 +     * <pre>
    1.65 +     * 1. Search in all the immediate interfaces which this method's class is
    1.66 +     *    implementing. Do it recursively for the superinterfaces as well.
    1.67 +     * 2. Traverse all the superclasses and search recursively in the
    1.68 +     *    interfaces which those superclasses implement.
    1.69 +     *</pre>
    1.70 +     *
    1.71 +     * @return MethodDoc[] Array of implemented methods.
    1.72 +     */
    1.73 +    public MethodDoc[] build(boolean sort) {
    1.74 +        buildImplementedMethodList(sort);
    1.75 +        return methlist.toArray(new MethodDoc[methlist.size()]);
    1.76 +    }
    1.77 +
    1.78 +    public MethodDoc[] build() {
    1.79 +        return build(true);
    1.80 +    }
    1.81 +
    1.82 +    public Type getMethodHolder(MethodDoc methodDoc) {
    1.83 +        return interfaces.get(methodDoc);
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Search for the method in the array of interfaces. If found check if it is
    1.88 +     * overridden by any other subinterface method which this class
    1.89 +     * implements. If it is not overidden, add it in the method list.
    1.90 +     * Do this recursively for all the extended interfaces for each interface
    1.91 +     * from the array passed.
    1.92 +     */
    1.93 +    private void buildImplementedMethodList(boolean sort) {
    1.94 +        List<Type> intfacs = Util.getAllInterfaces(classdoc, configuration, sort);
    1.95 +        for (Iterator<Type> iter = intfacs.iterator(); iter.hasNext(); ) {
    1.96 +            Type interfaceType = iter.next();
    1.97 +            MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method);
    1.98 +            if (found != null) {
    1.99 +                removeOverriddenMethod(found);
   1.100 +                if (!overridingMethodFound(found)) {
   1.101 +                    methlist.add(found);
   1.102 +                    interfaces.put(found, interfaceType);
   1.103 +                }
   1.104 +            }
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Search in the method list and check if it contains a method which
   1.110 +     * is overridden by the method as parameter.  If found, remove the
   1.111 +     * overridden method from the method list.
   1.112 +     *
   1.113 +     * @param method Is this method overriding a method in the method list.
   1.114 +     */
   1.115 +    private void removeOverriddenMethod(MethodDoc method) {
   1.116 +        ClassDoc overriddenClass = method.overriddenClass();
   1.117 +        if (overriddenClass != null) {
   1.118 +            for (int i = 0; i < methlist.size(); i++) {
   1.119 +                ClassDoc cd = methlist.get(i).containingClass();
   1.120 +                if (cd == overriddenClass || overriddenClass.subclassOf(cd)) {
   1.121 +                    methlist.remove(i);  // remove overridden method
   1.122 +                    return;
   1.123 +                }
   1.124 +            }
   1.125 +        }
   1.126 +    }
   1.127 +
   1.128 +    /**
   1.129 +     * Search in the already found methods' list and check if it contains
   1.130 +     * a method which is overriding the method parameter or is the method
   1.131 +     * parameter itself.
   1.132 +     *
   1.133 +     * @param method MethodDoc Method to be searched in the Method List for
   1.134 +     * an overriding method.
   1.135 +     */
   1.136 +    private boolean overridingMethodFound(MethodDoc method) {
   1.137 +        ClassDoc containingClass = method.containingClass();
   1.138 +        for (int i = 0; i < methlist.size(); i++) {
   1.139 +            MethodDoc listmethod = methlist.get(i);
   1.140 +            if (containingClass == listmethod.containingClass()) {
   1.141 +                // it's the same method.
   1.142 +                return true;
   1.143 +            }
   1.144 +            ClassDoc cd = listmethod.overriddenClass();
   1.145 +            if (cd == null) {
   1.146 +                continue;
   1.147 +            }
   1.148 +            if (cd == containingClass || cd.subclassOf(containingClass)) {
   1.149 +                return true;
   1.150 +            }
   1.151 +        }
   1.152 +        return false;
   1.153 +    }
   1.154 +}

mercurial