KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > ClassIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.retouche.source;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import org.netbeans.api.gsf.Index;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.netbeans.modules.retouche.source.GlobalSourcePath;
32 import org.netbeans.modules.retouche.source.usages.ClassIndexFactory;
33 import org.netbeans.modules.retouche.source.usages.ClassIndexImpl;
34 import org.netbeans.modules.retouche.source.usages.ClassIndexManager;
35 import org.openide.util.Exceptions;
36
37 /**
38  * This file is originally from Retouche, the Java Support
39  * infrastructure in NetBeans. I have modified the file as little
40  * as possible to make merging Retouche fixes back as simple as
41  * possible.
42  *
43  * The ClassIndex provides access to information stored in the
44  * persistent index. It can be used to obtain list of packages
45  * or declared types. It can be also used to obtain a list of
46  * source files referencing given type (usages of given type).
47  *
48  * @author Petr Hrebejk, Tomas Zezula
49  */

50 public final class ClassIndex extends Index {
51     
52     private static final Logger JavaDoc LOGGER = Logger.getLogger(ClassIndex.class.getName());
53     
54     private final ClassPath bootPath;
55     private final ClassPath classPath;
56     private final ClassPath sourcePath;
57     
58     private Set JavaDoc<ClassIndexImpl> sourceIndeces; // XXX Indices
59
private Set JavaDoc<ClassIndexImpl> depsIndeces;
60     
61     // Constants moved up to Index
62

63     static {
64     ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl();
65     }
66     
67     ClassIndex(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) {
68         //assert bootPath != null;
69
//assert classPath != null;
70
//assert sourcePath != null;
71
this.bootPath = bootPath;
72         this.classPath = classPath;
73         this.sourcePath = sourcePath;
74     }
75     
76     public void gsfSearch(final String JavaDoc primaryField, final String JavaDoc name, final NameKind kind,
77             final Set JavaDoc<SearchScope> scope, /*final ResultConvertor<T> convertor,*/
78             final Set JavaDoc<SearchResult> result) throws IOException JavaDoc {
79         assert primaryField != null;
80         assert name != null;
81         assert kind != null;
82         final Iterable JavaDoc<? extends ClassIndexImpl> queries = this.getQueries(scope);
83         //final ResultConvertor<ElementHandle<TypeElement>> thConvertor = ResultConvertor.elementHandleConvertor();
84
for (ClassIndexImpl query : queries) {
85             query.gsfSearch(primaryField, name, kind, scope, result);
86         }
87         LOGGER.fine(String.format("ClassIndex.gsfSearch returned %d elements\n", result.size()));
88     }
89
90     public void gsfStore(Set JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> fieldToData, Set JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> noIndexFields, Map JavaDoc<String JavaDoc,String JavaDoc> toDelete) throws IOException JavaDoc {
91         // XXX Ugh
92
throw new RuntimeException JavaDoc("Not yet implemented");
93     }
94     
95 // /**
96
// * Returns a set of {@link ElementHandle}s containing reference(s) to given element.
97
// * @param element for which usages should be found
98
// * @param searchKind type of reference, {@see SearchKind}
99
// * @param scope to search in {@see SearchScope}
100
// * @return set of {@link ElementHandle}s containing the reference(s)
101
// *
102
// */
103
// public Set<ElementHandle<TypeElement>> getElements (final ElementHandle<TypeElement> element, final Set<SearchKind> searchKind, final Set<SearchScope> scope) {
104
// assert element != null;
105
// assert element.getSignature()[0] != null;
106
// assert searchKind != null;
107
// final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>> ();
108
// final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope);
109
// final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind);
110
// final String binaryName = element.getSignature()[0];
111
// final ResultConvertor<ElementHandle<TypeElement>> thConvertor = ResultConvertor.elementHandleConvertor();
112
// if (!ut.isEmpty()) {
113
// for (ClassIndexImpl query : queries) {
114
// query.search(binaryName, ut, thConvertor, result);
115
// }
116
// }
117
// return Collections.unmodifiableSet(result);
118
// }
119
//
120
// /**
121
// * Returns a set of source files containing reference(s) to given element.
122
// * @param element for which usages should be found
123
// * @param searchKind type of reference, {@see SearchKind}
124
// * @param scope to search in {@see SearchScope}
125
// * @return set of {@link FileObject}s containing the reference(s)
126
// *
127
// */
128
// public Set<FileObject> getResources (final ElementHandle<TypeElement> element, final Set<SearchKind> searchKind, final Set<SearchScope> scope) {
129
// assert element != null;
130
// assert element.getSignature()[0] != null;
131
// assert searchKind != null;
132
// final Set<FileObject> result = new HashSet<FileObject> ();
133
// final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope);
134
// final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind);
135
// final String binaryName = element.getSignature()[0];
136
// if (!ut.isEmpty()) {
137
// for (ClassIndexImpl query : queries) {
138
// final ResultConvertor<FileObject> foConvertor = ResultConvertor.fileObjectConvertor (query.getSourceRoots());
139
// query.search (binaryName, ut, foConvertor, result);
140
// }
141
// }
142
// return Collections.unmodifiableSet(result);
143
// }
144
//
145
//
146
// /**
147
// * Returns {@link ElementHandle}s for all declared types in given classpath corresponding to the name.
148
// * @param case sensitive prefix, case insensitive prefix, exact simple name,
149
// * camel case or regular expression depending on the kind parameter.
150
// * @param kind of the name {@see NameKind}
151
// * @param scope to search in {@see SearchScope}
152
// * @return set of all matched declared types
153
// */
154
// public Set<ElementHandle<TypeElement>> getDeclaredTypes (final String name, final NameKind kind, final Set<SearchScope> scope) {
155
// assert name != null;
156
// assert kind != null;
157
// final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>();
158
// final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope);
159
// final ResultConvertor<ElementHandle<TypeElement>> thConvertor = ResultConvertor.elementHandleConvertor();
160
// for (ClassIndexImpl query : queries) {
161
// query.getDeclaredTypes (name, kind, thConvertor, result);
162
// }
163
// LOGGER.fine(String.format("ClassIndex.getDeclaredTypes returned %d elements\n", result.size()));
164
// return Collections.unmodifiableSet(result);
165
// }
166
//
167
// /**
168
// * Returns names af all packages in given classpath starting with prefix.
169
// * @param prefix of the package name
170
// * @param directOnly if true treats the packages as folders and returns only
171
// * the nearest component of the package.
172
// * @param scope to search in {@see SearchScope}
173
// * @return set of all matched package names
174
// */
175
// public Set<String> getPackageNames (final String prefix, boolean directOnly, final Set<SearchScope> scope) {
176
// assert prefix != null;
177
// final Set<String> result = new HashSet<String> ();
178
// final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope);
179
// for (ClassIndexImpl query : queries) {
180
// query.getPackageNames (prefix, directOnly, result);
181
// }
182
// return Collections.unmodifiableSet(result);
183
// }
184
//
185
// // Private innerclasses ----------------------------------------------------
186
//
187
private static class ClassIndexFactoryImpl implements ClassIndexFactory {
188         
189     public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) {
190         return new ClassIndex(bootPath, classPath, sourcePath);
191         }
192     
193     }
194     
195     //Private methods
196

197     
198     private synchronized Iterable JavaDoc<? extends ClassIndexImpl> getQueries (final Set JavaDoc<SearchScope> scope) {
199         Set JavaDoc<ClassIndexImpl> result = new HashSet JavaDoc<ClassIndexImpl> ();
200
201         if (scope.contains(SearchScope.SOURCE)) {
202             if (this.sourceIndeces == null) {
203                 Set JavaDoc<ClassIndexImpl> indeces = new HashSet JavaDoc<ClassIndexImpl>();
204                 createQueriesForRoots (this.sourcePath, true, indeces);
205                 this.sourceIndeces = indeces;
206             }
207             result.addAll(this.sourceIndeces);
208         }
209
210         if (scope.contains(SearchScope.DEPENDENCIES)) {
211             if (this.depsIndeces == null) {
212                 Set JavaDoc<ClassIndexImpl> indeces = new HashSet JavaDoc<ClassIndexImpl>();
213                 // BEGIN TOR MODIFICATIONS
214
//createQueriesForRoots (this.bootPath, false, indeces);
215
// The boot class path for Ruby queries is always the same.
216
// I can't rely on this.bootPath.getRoots() because this relies
217
// upon getting the ClassPath for the file, and for files outside
218
// of Ruby projects (such as Ruby files in the libraries themselves,
219
// in case I'm navigating from one super class to another) these
220
// do not get the Ruby bootclass path. They instead have a
221
// fallback Java platform classpath with jars.
222
// I -always- want the Ruby bootclass path here so use it
223
// directly.
224
Set JavaDoc<URL JavaDoc> bootRoots = ClassIndexManager.getDefault().getBootRoots();
225                 for (URL JavaDoc u : bootRoots) {
226                     try {
227                         ClassIndexImpl ci = ClassIndexManager.getDefault().getUsagesQuery(u);
228                         if (ci != null) {
229                             indeces.add(ci);
230                         }
231                     } catch (IOException JavaDoc ioe) {
232                         Exceptions.printStackTrace(ioe);
233                     }
234                 }
235                 // END TOR MODIFICATIONS
236
createQueriesForRoots (this.classPath, false, indeces);
237                 this.depsIndeces = indeces;
238             }
239             result.addAll(this.depsIndeces);
240         }
241         LOGGER.fine(String.format("ClassIndex.queries[Scope=%s, sourcePath=%s, bootPath=%s, classPath=%s] => %s\n",scope,sourcePath,bootPath,classPath,result));
242         return result;
243     }
244     
245     
246     // For lucene browser
247
public static
248     /*private*/ void createQueriesForRoots (final ClassPath cp, final boolean sources, final Set JavaDoc<? super ClassIndexImpl> queries) {
249         final GlobalSourcePath gsp = GlobalSourcePath.getDefault();
250         List JavaDoc<ClassPath.Entry> entries = cp.entries();
251     for (ClassPath.Entry entry : entries) {
252         try {
253                 boolean indexNow = false;
254                 URL JavaDoc[] srcRoots;
255                 if (!sources) {
256                     URL JavaDoc srcRoot = org.netbeans.modules.retouche.source.usages.Index.getSourceRootForClassFolder (entry.getURL());
257                     if (srcRoot != null) {
258                         srcRoots = new URL JavaDoc[] {srcRoot};
259                     }
260                     else {
261                         srcRoots = gsp.getSourceRootForBinaryRoot (entry.getURL(), cp, true);
262                         if (srcRoots == null) {
263                             indexNow = true;
264                             srcRoots = new URL JavaDoc[] {entry.getURL()};
265                         }
266                     }
267                     //End to be removed
268
}
269                 else {
270                     srcRoots = new URL JavaDoc[] {entry.getURL()};
271                 }
272                 for (URL JavaDoc srcRoot : srcRoots) {
273                     ClassIndexImpl ci = ClassIndexManager.getDefault().getUsagesQuery(srcRoot);
274                     if (ci != null) {
275                         queries.add (ci);
276                     }
277                 }
278         } catch (IOException JavaDoc ioe) {
279         Exceptions.printStackTrace(ioe);
280         }
281     }
282     }
283     
284 //
285
// private static Set<ClassIndexImpl.UsageType> encodeSearchKind (final ElementKind elementKind, final Set<ClassIndex.SearchKind> kind) {
286
// assert kind != null;
287
// final Set<ClassIndexImpl.UsageType> result = EnumSet.noneOf(ClassIndexImpl.UsageType.class);
288
// for (ClassIndex.SearchKind sk : kind) {
289
// switch (sk) {
290
// case METHOD_REFERENCES:
291
// result.add(ClassIndexImpl.UsageType.METHOD_REFERENCE);
292
// break;
293
// case FIELD_REFERENCES:
294
// result.add(ClassIndexImpl.UsageType.FIELD_REFERENCE);
295
// break;
296
// case TYPE_REFERENCES:
297
// result.add(ClassIndexImpl.UsageType.TYPE_REFERENCE);
298
// break;
299
// case IMPLEMENTORS:
300
// switch( elementKind) {
301
// case INTERFACE:
302
// case ANNOTATION_TYPE:
303
// result.add(ClassIndexImpl.UsageType.SUPER_INTERFACE);
304
// break;
305
// case CLASS:
306
// result.add(ClassIndexImpl.UsageType.SUPER_CLASS);
307
// break;
308
// case ENUM: //enum is final
309
// break;
310
// case OTHER:
311
// result.add(ClassIndexImpl.UsageType.SUPER_INTERFACE);
312
// result.add(ClassIndexImpl.UsageType.SUPER_CLASS);
313
// break;
314
// default:
315
// throw new IllegalArgumentException ();
316
// }
317
// break;
318
// default:
319
// throw new IllegalArgumentException ();
320
// }
321
// }
322
// return result;
323
// }
324
//
325
}
326
Popular Tags