|
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 */ |
|
25 |
|
26 package com.sun.tools.doclets.internal.toolkit.util; |
|
27 |
|
28 import java.util.*; |
|
29 |
|
30 import com.sun.javadoc.*; |
|
31 import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
32 |
|
33 /** |
|
34 * For a given class method, build an array of interface methods which it |
|
35 * implements. |
|
36 * |
|
37 * <p><b>This is NOT part of any supported API. |
|
38 * If you write code that depends on this, you do so at your own risk. |
|
39 * This code and its internal interfaces are subject to change or |
|
40 * deletion without notice.</b> |
|
41 * |
|
42 * @author Atul M Dambalkar |
|
43 */ |
|
44 public class ImplementedMethods { |
|
45 |
|
46 private Map<MethodDoc,Type> interfaces = new HashMap<MethodDoc,Type>(); |
|
47 private List<MethodDoc> methlist = new ArrayList<MethodDoc>(); |
|
48 private Configuration configuration; |
|
49 private final ClassDoc classdoc; |
|
50 private final MethodDoc method; |
|
51 |
|
52 public ImplementedMethods(MethodDoc method, Configuration configuration) { |
|
53 this.method = method; |
|
54 this.configuration = configuration; |
|
55 classdoc = method.containingClass(); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Return the array of interface methods which the method passed in the |
|
60 * constructor is implementing. The search/build order is as follows: |
|
61 * <pre> |
|
62 * 1. Search in all the immediate interfaces which this method's class is |
|
63 * implementing. Do it recursively for the superinterfaces as well. |
|
64 * 2. Traverse all the superclasses and search recursively in the |
|
65 * interfaces which those superclasses implement. |
|
66 *</pre> |
|
67 * |
|
68 * @return MethodDoc[] Array of implemented methods. |
|
69 */ |
|
70 public MethodDoc[] build(boolean sort) { |
|
71 buildImplementedMethodList(sort); |
|
72 return methlist.toArray(new MethodDoc[methlist.size()]); |
|
73 } |
|
74 |
|
75 public MethodDoc[] build() { |
|
76 return build(true); |
|
77 } |
|
78 |
|
79 public Type getMethodHolder(MethodDoc methodDoc) { |
|
80 return interfaces.get(methodDoc); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Search for the method in the array of interfaces. If found check if it is |
|
85 * overridden by any other subinterface method which this class |
|
86 * implements. If it is not overidden, add it in the method list. |
|
87 * Do this recursively for all the extended interfaces for each interface |
|
88 * from the array passed. |
|
89 */ |
|
90 private void buildImplementedMethodList(boolean sort) { |
|
91 List<Type> intfacs = Util.getAllInterfaces(classdoc, configuration, sort); |
|
92 for (Iterator<Type> iter = intfacs.iterator(); iter.hasNext(); ) { |
|
93 Type interfaceType = iter.next(); |
|
94 MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method); |
|
95 if (found != null) { |
|
96 removeOverriddenMethod(found); |
|
97 if (!overridingMethodFound(found)) { |
|
98 methlist.add(found); |
|
99 interfaces.put(found, interfaceType); |
|
100 } |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 /** |
|
106 * Search in the method list and check if it contains a method which |
|
107 * is overridden by the method as parameter. If found, remove the |
|
108 * overridden method from the method list. |
|
109 * |
|
110 * @param method Is this method overriding a method in the method list. |
|
111 */ |
|
112 private void removeOverriddenMethod(MethodDoc method) { |
|
113 ClassDoc overriddenClass = method.overriddenClass(); |
|
114 if (overriddenClass != null) { |
|
115 for (int i = 0; i < methlist.size(); i++) { |
|
116 ClassDoc cd = methlist.get(i).containingClass(); |
|
117 if (cd == overriddenClass || overriddenClass.subclassOf(cd)) { |
|
118 methlist.remove(i); // remove overridden method |
|
119 return; |
|
120 } |
|
121 } |
|
122 } |
|
123 } |
|
124 |
|
125 /** |
|
126 * Search in the already found methods' list and check if it contains |
|
127 * a method which is overriding the method parameter or is the method |
|
128 * parameter itself. |
|
129 * |
|
130 * @param method MethodDoc Method to be searched in the Method List for |
|
131 * an overriding method. |
|
132 */ |
|
133 private boolean overridingMethodFound(MethodDoc method) { |
|
134 ClassDoc containingClass = method.containingClass(); |
|
135 for (int i = 0; i < methlist.size(); i++) { |
|
136 MethodDoc listmethod = methlist.get(i); |
|
137 if (containingClass == listmethod.containingClass()) { |
|
138 // it's the same method. |
|
139 return true; |
|
140 } |
|
141 ClassDoc cd = listmethod.overriddenClass(); |
|
142 if (cd == null) { |
|
143 continue; |
|
144 } |
|
145 if (cd == containingClass || cd.subclassOf(containingClass)) { |
|
146 return true; |
|
147 } |
|
148 } |
|
149 return false; |
|
150 } |
|
151 } |