author | mcimadamore |
Tue, 13 Jan 2009 13:27:14 +0000 | |
changeset 1789 | 7ac8c0815000 |
parent 1264 | 076a3cde30d5 |
child 5520 | 86e4b9a9da40 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
1264 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
10 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.doclets.internal.toolkit.util; |
|
27 |
||
28 |
import com.sun.javadoc.*; |
|
29 |
import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
30 |
import java.util.*; |
|
31 |
||
32 |
/** |
|
33 |
* For a given class method, build an array of interface methods which it |
|
34 |
* implements. |
|
35 |
* |
|
36 |
* This code is not part of an API. |
|
37 |
* It is implementation that is subject to change. |
|
38 |
* Do not use it as an API |
|
39 |
* |
|
40 |
* @author Atul M Dambalkar |
|
41 |
*/ |
|
42 |
public class ImplementedMethods { |
|
43 |
||
868 | 44 |
private Map<MethodDoc,Type> interfaces = new HashMap<MethodDoc,Type>(); |
45 |
private List<MethodDoc> methlist = new ArrayList<MethodDoc>(); |
|
10 | 46 |
private Configuration configuration; |
47 |
private final ClassDoc classdoc; |
|
48 |
private final MethodDoc method; |
|
49 |
||
50 |
public ImplementedMethods(MethodDoc method, Configuration configuration) { |
|
51 |
this.method = method; |
|
52 |
this.configuration = configuration; |
|
53 |
classdoc = method.containingClass(); |
|
54 |
} |
|
55 |
||
56 |
/** |
|
57 |
* Return the array of interface methods which the method passed in the |
|
58 |
* constructor is implementing. The search/build order is as follows: |
|
59 |
* <pre> |
|
60 |
* 1. Search in all the immediate interfaces which this method's class is |
|
61 |
* implementing. Do it recursively for the superinterfaces as well. |
|
62 |
* 2. Traverse all the superclasses and search recursively in the |
|
63 |
* interfaces which those superclasses implement. |
|
64 |
*</pre> |
|
65 |
* |
|
66 |
* @return MethodDoc[] Array of implemented methods. |
|
67 |
*/ |
|
68 |
public MethodDoc[] build(boolean sort) { |
|
69 |
buildImplementedMethodList(sort); |
|
868 | 70 |
return methlist.toArray(new MethodDoc[methlist.size()]); |
10 | 71 |
} |
72 |
||
73 |
public MethodDoc[] build() { |
|
74 |
return build(true); |
|
75 |
} |
|
76 |
||
77 |
public Type getMethodHolder(MethodDoc methodDoc) { |
|
868 | 78 |
return interfaces.get(methodDoc); |
10 | 79 |
} |
80 |
||
81 |
/** |
|
82 |
* Search for the method in the array of interfaces. If found check if it is |
|
83 |
* overridden by any other subinterface method which this class |
|
84 |
* implements. If it is not overidden, add it in the method list. |
|
85 |
* Do this recursively for all the extended interfaces for each interface |
|
86 |
* from the array passed. |
|
87 |
*/ |
|
88 |
private void buildImplementedMethodList(boolean sort) { |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
89 |
List<Type> intfacs = Util.getAllInterfaces(classdoc, configuration, sort); |
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
90 |
for (Iterator<Type> iter = intfacs.iterator(); iter.hasNext(); ) { |
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
91 |
Type interfaceType = iter.next(); |
10 | 92 |
MethodDoc found = Util.findMethod(interfaceType.asClassDoc(), method); |
93 |
if (found != null) { |
|
94 |
removeOverriddenMethod(found); |
|
95 |
if (!overridingMethodFound(found)) { |
|
96 |
methlist.add(found); |
|
97 |
interfaces.put(found, interfaceType); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Search in the method list and check if it contains a method which |
|
105 |
* is overridden by the method as parameter. If found, remove the |
|
106 |
* overridden method from the method list. |
|
107 |
* |
|
108 |
* @param method Is this method overriding a method in the method list. |
|
109 |
*/ |
|
110 |
private void removeOverriddenMethod(MethodDoc method) { |
|
111 |
ClassDoc overriddenClass = method.overriddenClass(); |
|
112 |
if (overriddenClass != null) { |
|
113 |
for (int i = 0; i < methlist.size(); i++) { |
|
868 | 114 |
ClassDoc cd = methlist.get(i).containingClass(); |
10 | 115 |
if (cd == overriddenClass || overriddenClass.subclassOf(cd)) { |
116 |
methlist.remove(i); // remove overridden method |
|
117 |
return; |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Search in the already found methods' list and check if it contains |
|
125 |
* a method which is overriding the method parameter or is the method |
|
126 |
* parameter itself. |
|
127 |
* |
|
128 |
* @param method MethodDoc Method to be searched in the Method List for |
|
129 |
* an overriding method. |
|
130 |
*/ |
|
131 |
private boolean overridingMethodFound(MethodDoc method) { |
|
132 |
ClassDoc containingClass = method.containingClass(); |
|
133 |
for (int i = 0; i < methlist.size(); i++) { |
|
868 | 134 |
MethodDoc listmethod = methlist.get(i); |
10 | 135 |
if (containingClass == listmethod.containingClass()) { |
136 |
// it's the same method. |
|
137 |
return true; |
|
138 |
} |
|
139 |
ClassDoc cd = listmethod.overriddenClass(); |
|
140 |
if (cd == null) { |
|
141 |
continue; |
|
142 |
} |
|
143 |
if (cd == containingClass || cd.subclassOf(containingClass)) { |
|
144 |
return true; |
|
145 |
} |
|
146 |
} |
|
147 |
return false; |
|
148 |
} |
|
149 |
} |