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