Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.netbeans.modules.apisupport.project.ui.services; |
42 |
|
43 |
import com.sun.source.tree.AnnotationTree; |
44 |
import com.sun.source.tree.ClassTree; |
45 |
import com.sun.source.tree.CompilationUnitTree; |
46 |
import com.sun.source.tree.Tree; |
47 |
import com.sun.source.tree.TreeVisitor; |
48 |
import com.sun.source.util.SimpleTreeVisitor; |
49 |
import com.sun.source.util.TreePath; |
50 |
import com.sun.source.util.Trees; |
51 |
import org.netbeans.api.java.classpath.ClassPath; |
52 |
import org.netbeans.api.java.source.CancellableTask; |
53 |
import org.netbeans.api.java.source.CompilationController; |
54 |
import org.netbeans.api.java.source.JavaSource; |
55 |
import org.netbeans.api.java.source.JavaSource.Phase; |
56 |
import org.openide.filesystems.FileObject; |
57 |
|
58 |
import javax.lang.model.type.TypeMirror; |
59 |
import javax.lang.model.util.Types; |
60 |
import java.awt.*; |
61 |
import java.io.IOException; |
62 |
import java.util.List; |
63 |
import java.util.logging.Level; |
64 |
import java.util.logging.Logger; |
65 |
import org.openide.util.Parameters; |
66 |
|
67 |
/** |
68 |
* Scans a classpath and finds all subtypes of a given type. |
69 |
* |
70 |
* @author Tim Boudreau |
71 |
*/ |
72 |
public class TypeFinder implements CancellableTask<CompilationController> { |
73 |
private volatile boolean cancelled; |
74 |
private final ClassPath classpath; |
75 |
private final String fqn; |
76 |
private Callback callback; |
77 |
|
78 |
public TypeFinder(String fqn, ClassPath path) { |
79 |
this.classpath = path; |
80 |
this.fqn = fqn; |
81 |
Parameters.notNull ("Classpath", path); //NOI18N |
82 |
Parameters.notNull ("AnnotationName", fqn); //NOI18N |
83 |
} |
84 |
|
85 |
public interface Callback { |
86 |
public void foundFileObject(FileObject fo, String className); |
87 |
} |
88 |
|
89 |
public void findTypes(Callback callback) { |
90 |
//Don't ever do this kind of IO-heavy work on the event thread |
91 |
assert !EventQueue.isDispatchThread(); |
92 |
this.callback = callback; |
93 |
//recursively iterate all children |
94 |
for (FileObject fo : classpath.getRoots()) { |
95 |
analyze(fo); |
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
private void analyze(FileObject fo) { |
101 |
if (fo.isFolder()) { |
102 |
for (FileObject child : fo.getChildren()) { |
103 |
analyze(child); |
104 |
} |
105 |
} else if ("java".equals(fo.getExt())) { //NOI18N |
106 |
JavaSource src = JavaSource.forFileObject(fo); |
107 |
try { |
108 |
src.runUserActionTask(this, true); |
109 |
} catch (IOException ex) { |
110 |
Logger.getLogger(TypeFinder.class.getName()).log(Level.INFO, |
111 |
"Problem scanning " + fo.getPath(), ex); //NOI18N |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
public void reset() { |
117 |
cancelled = false; |
118 |
} |
119 |
|
120 |
public void cancel() { |
121 |
cancelled = true; |
122 |
} |
123 |
|
124 |
public void run(CompilationController compiler) throws Exception { |
125 |
if (cancelled) { |
126 |
return; |
127 |
} |
128 |
compiler.toPhase(Phase.RESOLVED); |
129 |
if (cancelled) { |
130 |
return; |
131 |
} |
132 |
List <? extends Tree> types = |
133 |
compiler.getCompilationUnit().getTypeDecls(); |
134 |
for (Tree tree: types) { |
135 |
if (cancelled) { |
136 |
return; |
137 |
} |
138 |
TypeMirror mirror = compiler.getTrees().getTypeMirror( |
139 |
TreePath.getPath(compiler.getCompilationUnit(), |
140 |
tree)); |
141 |
if (tree instanceof ClassTree && match(compiler, (ClassTree) tree)) { |
142 |
callback.foundFileObject(compiler.getFileObject(), |
143 |
mirror.toString()); |
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
boolean match(CompilationController compiler, ClassTree t) { |
149 |
// compiler.getTrees(), compiler.getTypes(), compiler.getCompilationUnit()/ |
150 |
// TypeMirror mirror = trees.getTypeMirror(TreePath.getPath(unit, t)); |
151 |
V v = new V(compiler); |
152 |
return t.accept(v, callback); |
153 |
// return match(mirror, types); |
154 |
} |
155 |
|
156 |
/* |
157 |
boolean match(TypeMirror mirror, Types types) { |
158 |
boolean result = false; |
159 |
if (mirror != null && |
160 |
!"java.lang.Object".equals(mirror.toString())) { //NOI18N |
161 |
|
162 |
result = fqn.equals(mirror.toString()); |
163 |
if (!result) { |
164 |
List<? extends TypeMirror> l = |
165 |
types.directSupertypes(mirror); |
166 |
|
167 |
for (TypeMirror tm : l) { |
168 |
result = match(tm, types); |
169 |
if (cancelled) return result; |
170 |
if (result) break; |
171 |
} |
172 |
} |
173 |
} |
174 |
return result; |
175 |
} |
176 |
*/ |
177 |
|
178 |
private class V extends SimpleTreeVisitor <Boolean, Callback> { |
179 |
private final CompilationController compiler; |
180 |
V (CompilationController compiler) { |
181 |
this.compiler = compiler; |
182 |
} |
183 |
|
184 |
@Override |
185 |
public Boolean visitAnnotation(AnnotationTree node, Callback p) { |
186 |
TypeMirror mirror = compiler.getTrees().getTypeMirror(TreePath.getPath(compiler.getCompilationUnit(), node)); |
187 |
return fqn.equals (mirror.toString()); |
188 |
} |
189 |
|
190 |
@Override |
191 |
public Boolean visitClass(ClassTree node, Callback p) { |
192 |
boolean result = false; |
193 |
for (AnnotationTree t : node.getModifiers().getAnnotations()) { |
194 |
result = visitAnnotation(t, p); |
195 |
if (result) { |
196 |
break; |
197 |
} |
198 |
} |
199 |
return result; |
200 |
} |
201 |
|
202 |
} |
203 |
} |