Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.java.source.support; |
43 |
|
44 |
import java.io.IOException; |
45 |
import java.net.URL; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collections; |
48 |
import java.util.HashMap; |
49 |
import java.util.Iterator; |
50 |
import java.util.List; |
51 |
import java.util.Map; |
52 |
import javax.lang.model.element.ElementKind; |
53 |
import javax.lang.model.element.PackageElement; |
54 |
import javax.lang.model.element.TypeElement; |
55 |
import org.netbeans.api.annotations.common.NonNull; |
56 |
import org.netbeans.api.java.classpath.ClassPath; |
57 |
import org.netbeans.api.java.source.ClasspathInfo; |
58 |
import org.netbeans.api.java.source.ElementHandle; |
59 |
import org.netbeans.api.java.source.SourceUtils; |
60 |
import org.netbeans.modules.java.source.ElementHandleAccessor; |
61 |
import org.netbeans.modules.java.source.usages.ClassIndexImpl; |
62 |
import org.netbeans.modules.java.source.usages.ClassIndexManager; |
63 |
import org.netbeans.modules.parsing.lucene.support.Convertor; |
64 |
import org.openide.util.Parameters; |
65 |
|
66 |
/** |
67 |
* Provides an information about frequencies of type and package usages |
68 |
* in given source root or source path. |
69 |
* @since 0.97 |
70 |
* @author Tomas Zezula |
71 |
*/ |
72 |
public final class ReferencesCount { |
73 |
|
74 |
private final Object lck = new Object(); |
75 |
private final Iterable<? extends URL> roots; |
76 |
//@GuardedBy("lck") |
77 |
private Map<String,Integer> typeFreqs; |
78 |
//@GuardedBy("lck") |
79 |
private Map<String,Integer> pkgFreqs; |
80 |
|
81 |
private ReferencesCount(@NonNull final Iterable<? extends URL> roots) { |
82 |
this.roots = roots; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Returns a number of classes on given source path (source root) which are |
87 |
* using given type. |
88 |
* @param type the type type to find the usage frequency for. |
89 |
* @return number of classes using the type. |
90 |
*/ |
91 |
public int getTypeReferenceCount(@NonNull final ElementHandle<? extends TypeElement> type) { |
92 |
Parameters.notNull("binaryName", type); //NOI18N |
93 |
if (!type.getKind().isClass() && |
94 |
!type.getKind().isInterface() && |
95 |
type.getKind() != ElementKind.OTHER) { |
96 |
throw new IllegalArgumentException(type.toString()); |
97 |
} |
98 |
try { |
99 |
init(); |
100 |
final Integer count = typeFreqs.get(SourceUtils.getJVMSignature(type)[0]); |
101 |
return count == null ? 0 : count; |
102 |
} catch (InterruptedException ie) { |
103 |
return 0; |
104 |
} |
105 |
} |
106 |
|
107 |
/** |
108 |
* Returns a number of classes on given source path (source root) which are |
109 |
* using given package. |
110 |
* @param pkg the package to find the usage frequency for. |
111 |
* @return number of classes using types from given package. |
112 |
*/ |
113 |
public int getPackageReferenceCount(@NonNull final ElementHandle<? extends PackageElement> pkg) { |
114 |
Parameters.notNull("pkgName", pkg); //NOI18N |
115 |
if (pkg.getKind() != ElementKind.PACKAGE) { |
116 |
throw new IllegalArgumentException(pkg.toString()); |
117 |
} |
118 |
try { |
119 |
init(); |
120 |
final Integer count = pkgFreqs.get(SourceUtils.getJVMSignature(pkg)[0]); |
121 |
return count == null ? 0 : count; |
122 |
} catch (InterruptedException ie) { |
123 |
return 0; |
124 |
} |
125 |
} |
126 |
|
127 |
/** |
128 |
* Returns all types used by classes on given source path (source root). |
129 |
* @return the used classes |
130 |
*/ |
131 |
@NonNull |
132 |
public Iterable<? extends ElementHandle<? extends TypeElement>> getUsedTypes() { |
133 |
try { |
134 |
init(); |
135 |
return new AsHandlesIterable<String, ElementHandle<TypeElement>>( |
136 |
typeFreqs.keySet(), |
137 |
new Convertor<String, ElementHandle<TypeElement>>() { |
138 |
@NonNull |
139 |
@Override |
140 |
public ElementHandle<TypeElement> convert(@NonNull final String p) { |
141 |
return ElementHandleAccessor.INSTANCE.create(ElementKind.OTHER, p); //FIXME |
142 |
} |
143 |
}); |
144 |
} catch (InterruptedException ie) { |
145 |
return Collections.<ElementHandle<TypeElement>>emptySet(); |
146 |
} |
147 |
} |
148 |
|
149 |
/** |
150 |
* Returns all packages used by classes on given source path (source root). |
151 |
* @return the used packages |
152 |
*/ |
153 |
@NonNull |
154 |
public Iterable<? extends ElementHandle<? extends PackageElement>> getUsedPackages() { |
155 |
try { |
156 |
init(); |
157 |
return new AsHandlesIterable<String, ElementHandle<PackageElement>>( |
158 |
pkgFreqs.keySet(), |
159 |
new Convertor<String, ElementHandle<PackageElement>>() { |
160 |
@NonNull |
161 |
@Override |
162 |
public ElementHandle<PackageElement> convert(@NonNull final String p) { |
163 |
return ElementHandleAccessor.INSTANCE.create(ElementKind.PACKAGE, p); |
164 |
} |
165 |
}); |
166 |
} catch (InterruptedException ie) { |
167 |
return Collections.<ElementHandle<PackageElement>>emptySet(); |
168 |
} |
169 |
} |
170 |
|
171 |
private void init() throws InterruptedException { |
172 |
synchronized (lck) { |
173 |
if (typeFreqs == null) { |
174 |
final ClassIndexManager cim = ClassIndexManager.getDefault(); |
175 |
final Map<String,Integer> typef = new HashMap<String, Integer>(); |
176 |
final Map<String,Integer> pkgf = new HashMap<String, Integer>(); |
177 |
try { |
178 |
for (URL root : roots) { |
179 |
final ClassIndexImpl ci = cim.getUsagesQuery(root, true); |
180 |
ci.getReferencesFrequences(typef, pkgf); |
181 |
} |
182 |
typeFreqs = Collections.<String,Integer>unmodifiableMap(typef); |
183 |
pkgFreqs = Collections.<String,Integer>unmodifiableMap(pkgf); |
184 |
} catch (IOException ioe) { |
185 |
typeFreqs = Collections.<String,Integer>emptyMap(); |
186 |
pkgFreqs = Collections.<String,Integer>emptyMap(); |
187 |
} |
188 |
} |
189 |
} |
190 |
assert typeFreqs != null; |
191 |
assert pkgFreqs != null; |
192 |
} |
193 |
|
194 |
private static class AsHandlesIterable<P,R> implements Iterable<R> { |
195 |
|
196 |
private final Iterable<P> from; |
197 |
private final Convertor<P,R> fnc; |
198 |
|
199 |
private AsHandlesIterable( |
200 |
@NonNull Iterable<P> from, |
201 |
@NonNull Convertor<P,R> fnc) { |
202 |
assert from != null; |
203 |
assert fnc != null; |
204 |
this.from = from; |
205 |
this.fnc = fnc; |
206 |
} |
207 |
|
208 |
@Override |
209 |
public Iterator<R> iterator() { |
210 |
return new AsHandlesIterator<P, R>(from.iterator(),fnc); |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
private static class AsHandlesIterator<P,R> implements Iterator<R> { |
216 |
|
217 |
private final Iterator<P> from; |
218 |
private final Convertor<P,R> fnc; |
219 |
|
220 |
private AsHandlesIterator( |
221 |
@NonNull Iterator<P> from, |
222 |
@NonNull Convertor<P,R> fnc) { |
223 |
assert from != null; |
224 |
assert fnc != null; |
225 |
this.from = from; |
226 |
this.fnc = fnc; |
227 |
} |
228 |
|
229 |
@Override |
230 |
public boolean hasNext() { |
231 |
return from.hasNext(); |
232 |
} |
233 |
|
234 |
@Override |
235 |
public R next() { |
236 |
return fnc.convert(from.next()); |
237 |
} |
238 |
|
239 |
@Override |
240 |
public void remove() { |
241 |
throw new UnsupportedOperationException("Read only Collection."); //NOI18N |
242 |
} |
243 |
|
244 |
|
245 |
} |
246 |
|
247 |
/** |
248 |
* Creates a {@link ReferencesCount} for source classpath represented by given |
249 |
* {@link ClasspathInfo}. |
250 |
* @param cpInfo the {@link ClasspathInfo} to create {@link ReferencesCount} for. |
251 |
* @return the {@link ReferencesCount} |
252 |
*/ |
253 |
@NonNull |
254 |
public static ReferencesCount get(@NonNull final ClasspathInfo cpInfo) { |
255 |
Parameters.notNull("cpInfo", cpInfo); //NOI18N |
256 |
final List<? extends ClassPath.Entry> scp = cpInfo.getClassPath(ClasspathInfo.PathKind.SOURCE).entries(); |
257 |
final List<URL> roots = new ArrayList<URL>(scp.size()); |
258 |
for (ClassPath.Entry e : scp) { |
259 |
roots.add(e.getURL()); |
260 |
} |
261 |
return new ReferencesCount(roots); |
262 |
} |
263 |
|
264 |
|
265 |
/** |
266 |
* Creates a {@link ReferencesCount} for source root. |
267 |
* @param root the root to create {@link ReferencesCount} for. |
268 |
* @return the {@link ReferencesCount} |
269 |
*/ |
270 |
@NonNull |
271 |
public static ReferencesCount get(@NonNull final URL root) { |
272 |
Parameters.notNull("cpInfo", root); //NOI18N |
273 |
return new ReferencesCount(Collections.<URL>singleton(root)); |
274 |
} |
275 |
} |