Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 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 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.whitelist.support; |
43 |
|
44 |
import com.sun.source.tree.ClassTree; |
45 |
import com.sun.source.tree.CompilationUnitTree; |
46 |
import com.sun.source.tree.IdentifierTree; |
47 |
import com.sun.source.tree.MemberSelectTree; |
48 |
import com.sun.source.tree.MethodInvocationTree; |
49 |
import com.sun.source.tree.MethodTree; |
50 |
import com.sun.source.tree.NewClassTree; |
51 |
import com.sun.source.tree.Tree; |
52 |
import com.sun.source.util.TreePathScanner; |
53 |
import com.sun.source.util.Trees; |
54 |
import java.net.URL; |
55 |
import java.util.ArrayDeque; |
56 |
import java.util.Collection; |
57 |
import java.util.HashMap; |
58 |
import java.util.List; |
59 |
import java.util.Map; |
60 |
import java.util.concurrent.Callable; |
61 |
import java.util.concurrent.CopyOnWriteArrayList; |
62 |
import javax.lang.model.element.Element; |
63 |
import javax.lang.model.element.ElementKind; |
64 |
import org.netbeans.api.annotations.common.CheckForNull; |
65 |
import org.netbeans.api.annotations.common.NonNull; |
66 |
import org.netbeans.api.annotations.common.NullAllowed; |
67 |
import org.netbeans.api.java.source.ElementHandle; |
68 |
import org.netbeans.api.whitelist.WhiteListQuery; |
69 |
import org.netbeans.api.whitelist.WhiteListQuery.Result; |
70 |
import org.netbeans.api.whitelist.WhiteListQuery.WhiteList; |
71 |
import org.netbeans.modules.whitelist.index.WhiteListIndexAccessor; |
72 |
import org.netbeans.modules.whitelist.index.WhiteListIndexerPlugin; |
73 |
import org.openide.filesystems.FileObject; |
74 |
import org.openide.filesystems.FileUtil; |
75 |
import org.openide.util.Exceptions; |
76 |
import org.openide.util.Parameters; |
77 |
|
78 |
/** |
79 |
* A persistent index of white list violations. |
80 |
* @author Tomas Zezula |
81 |
* @since 1.2 |
82 |
*/ |
83 |
public final class WhiteListIndex { |
84 |
|
85 |
static { |
86 |
WhiteListIndexAccessor.setInstance(new WhiteListIndexAccessorImpl()); |
87 |
} |
88 |
|
89 |
private static WhiteListIndex instance; |
90 |
private final List<WhiteListIndexListener> listeners = new CopyOnWriteArrayList<WhiteListIndexListener>(); |
91 |
|
92 |
private WhiteListIndex() {} |
93 |
|
94 |
/** |
95 |
* Returns the white list violations for given root or file. |
96 |
* @param root the root to get white list violations for |
97 |
* @param file the file to restrict the white list violation search or null |
98 |
* @return a {@link Collection} of {@link Problem}s |
99 |
* @throws IllegalArgumentException in case when file is not under the root or it's not a valid file |
100 |
* @throws UnsupportedOperationException when the index is not supported by the IDE |
101 |
*/ |
102 |
@NonNull |
103 |
public Collection<? extends Problem> getWhiteListViolations( |
104 |
@NonNull final FileObject root, |
105 |
@NullAllowed final FileObject file) throws IllegalArgumentException, UnsupportedOperationException { |
106 |
Parameters.notNull("scope", root); //NOI18N |
107 |
if (file != null && !(root.equals(file) || FileUtil.isParentOf(root, file))) { |
108 |
throw new IllegalArgumentException( |
109 |
"The file: " + //NOI18N |
110 |
FileUtil.getFileDisplayName(file) + |
111 |
" has to be inside the root: " + //NOI18N |
112 |
FileUtil.getFileDisplayName(root)); |
113 |
} |
114 |
if (file != null && !file.isData()) { |
115 |
throw new IllegalArgumentException( |
116 |
"The file: " + //NOI18N |
117 |
FileUtil.getFileDisplayName(file) + |
118 |
" has to be file."); //NOI18N |
119 |
} |
120 |
return WhiteListIndexerPlugin.getWhiteListViolations(root,file); |
121 |
} |
122 |
|
123 |
/** |
124 |
* Adds {@link WhiteListIndexListener}. |
125 |
* The listener is notified when the white list index is changed. |
126 |
* @param listener the listener to be added |
127 |
*/ |
128 |
public void addWhiteListIndexListener(@NonNull final WhiteListIndexListener listener) { |
129 |
Parameters.notNull("listener", listener); //NOI18N |
130 |
listeners.add(listener); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Removes {@link WhiteListIndexListener}. |
135 |
* @param listener the listener to be removed |
136 |
*/ |
137 |
public void removeWhiteListIndexListener(@NonNull final WhiteListIndexListener listener) { |
138 |
Parameters.notNull("listener", listener); //NOI18N |
139 |
listeners.remove(listener); |
140 |
} |
141 |
|
142 |
/** |
143 |
* Utility method to check the given {@link CompilationUnitTree} for white list violations. |
144 |
* @param unit the {@link CompilationUnitTree} to be analyzed |
145 |
* @param whitelist the {@link WhiteList} to use to check the violations |
146 |
* @param trees the {@link Trees} service |
147 |
* @param cancel the cancel request. If the {@link Callable} returns true the check is canceled |
148 |
* and null is returned. |
149 |
* @return a {@link Map} of {@link Tree}s with attached white list violations or null when the |
150 |
* scan was canceled. |
151 |
*/ |
152 |
@CheckForNull |
153 |
public static Map<? extends Tree, ? extends WhiteListQuery.Result> getWhiteListViolations( |
154 |
@NonNull final CompilationUnitTree unit, |
155 |
@NonNull final WhiteListQuery.WhiteList whitelist, |
156 |
@NonNull final Trees trees, |
157 |
@NullAllowed final Callable<Boolean> cancel) { |
158 |
Parameters.notNull("tree", unit); //NOI18N |
159 |
Parameters.notNull("whitelist", whitelist); //NOI18N |
160 |
Parameters.notNull("trees", trees); //NOI18N |
161 |
final Map<Tree,WhiteListQuery.Result> result = new HashMap<Tree, Result>(); |
162 |
final WhiteListScanner scanner = new WhiteListScanner(trees, whitelist, cancel); |
163 |
try { |
164 |
scanner.scan(unit, result); |
165 |
return result; |
166 |
} catch (WhiteListScanner.Cancel ce) { |
167 |
return null; |
168 |
} |
169 |
} |
170 |
|
171 |
/** |
172 |
* Returns an instance of {@link WhiteListIndex} |
173 |
* @return the instance of {@link WhiteListIndex} |
174 |
*/ |
175 |
public static synchronized WhiteListIndex getDefault() { |
176 |
if (instance == null) { |
177 |
instance = new WhiteListIndex(); |
178 |
} |
179 |
return instance; |
180 |
} |
181 |
|
182 |
|
183 |
/** |
184 |
* Represent a white list violation |
185 |
*/ |
186 |
public static final class Problem { |
187 |
|
188 |
private final WhiteListQuery.Result result; |
189 |
private final FileObject root; |
190 |
private final String relPath; |
191 |
private final int line; |
192 |
|
193 |
private Problem ( |
194 |
@NonNull final WhiteListQuery.Result result, |
195 |
@NonNull final FileObject root, |
196 |
@NonNull final String relPath, |
197 |
final int line) { |
198 |
Parameters.notNull("result", result); //NOI18N |
199 |
Parameters.notNull("root", root); //NOI18N |
200 |
Parameters.notNull("relPath", relPath); //NOI18N |
201 |
this.result = result; |
202 |
this.root = root; |
203 |
this.relPath = relPath; |
204 |
this.line = line; |
205 |
} |
206 |
|
207 |
/** |
208 |
* Returns {@link WhiteListQuery.Result} describing the |
209 |
* white list violation. |
210 |
* @return the {@link WhiteListQuery.Result} |
211 |
*/ |
212 |
@NonNull |
213 |
public WhiteListQuery.Result getResult() { |
214 |
return result; |
215 |
} |
216 |
|
217 |
/** |
218 |
* Returns the file in which the white list violation occured. |
219 |
* @return the {@link FileObject} |
220 |
*/ |
221 |
@CheckForNull |
222 |
public FileObject getFile() { |
223 |
return root.getFileObject(relPath); |
224 |
} |
225 |
|
226 |
/** |
227 |
* Returns the line number of white list violation. |
228 |
* @return the line number |
229 |
*/ |
230 |
public int getLine() { |
231 |
return line; |
232 |
} |
233 |
} |
234 |
|
235 |
//<editor-fold defaultstate="collapsed" desc="Private implementation"> |
236 |
private void fireIndexChange(final URL root) { |
237 |
final WhiteListIndexEvent event = new WhiteListIndexEvent(this, root); |
238 |
for (WhiteListIndexListener l : listeners) { |
239 |
l.indexChanged(event); |
240 |
} |
241 |
} |
242 |
|
243 |
private static final class WhiteListIndexAccessorImpl extends WhiteListIndexAccessor { |
244 |
@Override |
245 |
public void refresh(@NonNull URL root) { |
246 |
WhiteListIndex.getDefault().fireIndexChange(root); |
247 |
} |
248 |
|
249 |
@Override |
250 |
@NonNull |
251 |
public Problem createProblem( |
252 |
@NonNull final Result result, |
253 |
@NonNull final FileObject root, |
254 |
@NonNull final String key, |
255 |
int line) { |
256 |
return new Problem(result, root, key, line); |
257 |
} |
258 |
} |
259 |
|
260 |
private static class WhiteListScanner extends TreePathScanner<Void, Map<Tree,WhiteListQuery.Result>> { |
261 |
|
262 |
private final Trees trees; |
263 |
private final Callable<Boolean> cancel; |
264 |
private final WhiteList whiteList; |
265 |
private final ArrayDeque<MethodInvocationTree> methodInvocation; |
266 |
|
267 |
WhiteListScanner( |
268 |
final Trees trees, |
269 |
final WhiteList whiteList, |
270 |
final Callable<Boolean> cancel) { |
271 |
this.trees = trees; |
272 |
this.whiteList = whiteList; |
273 |
this.cancel = cancel; |
274 |
methodInvocation = new ArrayDeque<MethodInvocationTree>(); |
275 |
} |
276 |
|
277 |
@Override |
278 |
public Void visitMethod(MethodTree node, Map<Tree,WhiteListQuery.Result> p) { |
279 |
checkCancel(); |
280 |
return super.visitMethod(node, p); |
281 |
} |
282 |
|
283 |
@Override |
284 |
public Void visitClass(ClassTree node, Map<Tree,WhiteListQuery.Result> p) { |
285 |
checkCancel(); |
286 |
return super.visitClass(node, p); |
287 |
} |
288 |
|
289 |
@Override |
290 |
public Void visitIdentifier(IdentifierTree node, Map<Tree,WhiteListQuery.Result> p) { |
291 |
handleNode(node,p); |
292 |
return super.visitIdentifier(node, p); |
293 |
} |
294 |
|
295 |
@Override |
296 |
public Void visitMemberSelect(MemberSelectTree node, Map<Tree,WhiteListQuery.Result> p) { |
297 |
handleNode(node,p); |
298 |
return super.visitMemberSelect(node, p); |
299 |
} |
300 |
|
301 |
@Override |
302 |
public Void visitNewClass(NewClassTree node, Map<Tree,WhiteListQuery.Result> p) { |
303 |
final Element e = trees.getElement(getCurrentPath()); |
304 |
final WhiteListQuery.Result res; |
305 |
if (e != null && !(res=whiteList.check(ElementHandle.create(e),WhiteListQuery.Operation.USAGE)).isAllowed()) { |
306 |
p.put(node,res); |
307 |
} |
308 |
scan(node.getTypeArguments(), p); |
309 |
scan(node.getArguments(), p); |
310 |
scan(node.getClassBody(), p); |
311 |
return null; |
312 |
} |
313 |
|
314 |
@Override |
315 |
public Void visitMethodInvocation(MethodInvocationTree node, Map<Tree,WhiteListQuery.Result> p) { |
316 |
methodInvocation.offerFirst(node); |
317 |
super.visitMethodInvocation(node, p); |
318 |
methodInvocation.removeFirst(); |
319 |
return null; |
320 |
} |
321 |
|
322 |
private void handleNode( |
323 |
final Tree node, |
324 |
final Map<Tree,WhiteListQuery.Result> p) { |
325 |
final Element e = trees.getElement(getCurrentPath()); |
326 |
if (e == null) { |
327 |
return; |
328 |
} |
329 |
final ElementKind k = e.getKind(); |
330 |
Tree toReport = null; |
331 |
if (k.isClass() || k.isInterface()) { |
332 |
toReport=node; |
333 |
} else if ((k == ElementKind.METHOD || k == ElementKind.CONSTRUCTOR) && |
334 |
!methodInvocation.isEmpty()) { |
335 |
toReport=methodInvocation.peekFirst(); |
336 |
} |
337 |
final WhiteListQuery.Result res; |
338 |
if (toReport != null && |
339 |
!(res=whiteList.check(ElementHandle.create(e),WhiteListQuery.Operation.USAGE)).isAllowed()) { |
340 |
p.put(toReport,res); |
341 |
} |
342 |
} |
343 |
|
344 |
private void checkCancel() { |
345 |
try { |
346 |
if (cancel != null && cancel.call() == Boolean.TRUE) { |
347 |
throw new Cancel(); |
348 |
} |
349 |
} catch (Exception ex) { |
350 |
Exceptions.printStackTrace(ex); |
351 |
} |
352 |
} |
353 |
|
354 |
static final class Cancel extends RuntimeException { |
355 |
} |
356 |
} |
357 |
//</editor-fold> |
358 |
} |