KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > usages > PersistentClassIndex


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.modules.java.source.usages;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import javax.lang.model.element.ElementKind;
31 import org.netbeans.api.java.queries.SourceForBinaryQuery;
32 import org.netbeans.api.java.source.CancellableTask;
33 import org.netbeans.api.java.source.ClassIndex;
34 import org.netbeans.api.java.source.CompilationController;
35 import org.netbeans.api.java.source.CompilationInfo;
36 import org.netbeans.api.java.source.JavaSource;
37 import org.netbeans.api.java.source.JavaSource.Phase;
38 import org.netbeans.modules.java.source.JavaSourceAccessor;
39 import static org.netbeans.modules.java.source.usages.ClassIndexImpl.UsageType.*;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.URLMapper;
42 import org.openide.util.Exceptions;
43 import org.openide.util.Exceptions;
44
45 /**
46  *
47  * @author Petr Hrebejk, Tomas Zezula
48  */

49 public class PersistentClassIndex extends ClassIndexImpl {
50     
51     private final Index index;
52     private final URL JavaDoc root;
53     private final boolean isSource;
54     private WeakReference JavaDoc<JavaSource> dirty;
55     private static final Logger JavaDoc LOGGER = Logger.getLogger(PersistentClassIndex.class.getName());
56     
57     /** Creates a new instance of ClassesAndMembersUQ */
58     private PersistentClassIndex(final URL JavaDoc root, final File JavaDoc cacheRoot, final boolean source)
59         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
60         assert root != null;
61         this.root = root;
62         this.index = LuceneIndex.create (cacheRoot);
63         this.isSource = source;
64     }
65     
66     public BinaryAnalyser getBinaryAnalyser () {
67         return new BinaryAnalyser (this.index);
68     }
69     
70     public SourceAnalyser getSourceAnalyser () {
71         return new SourceAnalyser (this.index);
72     }
73     
74     public FileObject[] getSourceRoots () {
75         FileObject[] rootFos;
76         if (isSource) {
77             FileObject rootFo = URLMapper.findFileObject (this.root);
78             rootFos = rootFo == null ? new FileObject[0] : new FileObject[] {rootFo};
79         }
80         else {
81             rootFos = SourceForBinaryQuery.findSourceRoots(this.root).getRoots();
82         }
83         return rootFos;
84     }
85     
86
87     // Factory method
88

89     public static ClassIndexImpl create(URL JavaDoc root, final File JavaDoc cacheRoot, final boolean indexNow)
90         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
91         return new PersistentClassIndex(root, cacheRoot, indexNow);
92     }
93     
94     // Implementation of UsagesQueryImpl ---------------------------------------
95
public <T> void search (final String JavaDoc binaryName, final Set JavaDoc<UsageType> usageType, final ResultConvertor<T> convertor, final Set JavaDoc<? super T> result) {
96         updateDirty();
97         if (BinaryAnalyser.OBJECT.equals(binaryName)) {
98             this.getDeclaredTypes("", ClassIndex.NameKind.PREFIX, convertor, result);
99             return;
100         }
101         try {
102             ClassIndexManager.getDefault().readLock(new ClassIndexManager.ExceptionAction<Void JavaDoc> () {
103                 public Void JavaDoc run () throws IOException JavaDoc {
104                     usages(binaryName, usageType, convertor, result);
105                     return null;
106                 }
107             });
108         } catch (IOException JavaDoc ioe) {
109             Exceptions.printStackTrace(ioe);
110         }
111     }
112     
113     
114                
115     
116     public <T> void getDeclaredTypes (final String JavaDoc simpleName, final ClassIndex.NameKind kind, final ResultConvertor<T> convertor, final Set JavaDoc<? super T> result) {
117         updateDirty();
118         try {
119             ClassIndexManager.getDefault().readLock(new ClassIndexManager.ExceptionAction<Void JavaDoc> () {
120                 public Void JavaDoc run () throws IOException JavaDoc {
121                     index.getDeclaredTypes (simpleName,kind, convertor, result);
122                     return null;
123                 }
124             });
125         } catch (IOException JavaDoc ioe) {
126             Exceptions.printStackTrace(ioe);
127         }
128     }
129     
130     
131     public void getPackageNames (final String JavaDoc prefix, final boolean directOnly, final Set JavaDoc<String JavaDoc> result) {
132         try {
133             ClassIndexManager.getDefault().readLock(new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
134                 public Void JavaDoc run () throws IOException JavaDoc {
135                     index.getPackageNames(prefix, directOnly, result);
136                     return null;
137                 }
138             });
139         } catch (IOException JavaDoc ioe) {
140             Exceptions.printStackTrace(ioe);
141         }
142     }
143     
144     public synchronized void setDirty (final JavaSource js) {
145         if (js == null) {
146             this.dirty = null;
147         }
148         else if (this.dirty == null || this.dirty.get() != js) {
149             this.dirty = new WeakReference JavaDoc (js);
150         }
151     }
152     
153     public @Override JavaDoc String JavaDoc toString () {
154         return "CompromiseUQ["+this.root.toExternalForm()+"]"; // NOI18N
155
}
156     
157     //Protected methods --------------------------------------------------------
158
protected final void close () throws IOException JavaDoc {
159         this.index.close();
160     }
161     
162     
163     // Private methods ---------------------------------------------------------
164

165     private void updateDirty () {
166         WeakReference JavaDoc<JavaSource> jsRef;
167         synchronized (this) {
168             jsRef = this.dirty;
169         }
170         if (jsRef != null) {
171             final JavaSource js = jsRef.get();
172             if (js != null) {
173                 final long startTime = System.currentTimeMillis();
174                 if (JavaSourceAccessor.INSTANCE.isDispatchThread()) {
175                     //Already under javac's lock
176
try {
177                         ClassIndexManager.getDefault().writeLock(
178                             new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
179                                 public Void JavaDoc run () throws IOException JavaDoc {
180                                     CompilationInfo compilationInfo = JavaSourceAccessor.INSTANCE.getCurrentCompilationInfo (js, JavaSource.Phase.RESOLVED);
181                                     if (compilationInfo != null) {
182                                         //Not cancelled
183
final SourceAnalyser sa = getSourceAnalyser();
184                                         long st = System.currentTimeMillis();
185                                         sa.analyseUnitAndStore(compilationInfo.getCompilationUnit(), JavaSourceAccessor.INSTANCE.getJavacTask(compilationInfo));
186                                         long et = System.currentTimeMillis();
187                                     }
188                                     return null;
189                                 }
190                         });
191                     } catch (IOException JavaDoc ioe) {
192                         Exceptions.printStackTrace(ioe);
193                     }
194                 }
195                 else {
196                     try {
197                         js.runUserActionTask(new CancellableTask<CompilationController>() {
198                             public void run (final CompilationController controller) {
199                                 try {
200                                     ClassIndexManager.getDefault().writeLock(
201                                         new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
202                                             public Void JavaDoc run () throws IOException JavaDoc {
203                                                 controller.toPhase(Phase.RESOLVED);
204                                                 final SourceAnalyser sa = getSourceAnalyser();
205                                                 long st = System.currentTimeMillis();
206                                                 sa.analyseUnitAndStore(controller.getCompilationUnit(), JavaSourceAccessor.INSTANCE.getJavacTask(controller));
207                                                 long et = System.currentTimeMillis();
208                                                 return null;
209                                             }
210                                     });
211                                 } catch (IOException JavaDoc ioe) {
212                                     Exceptions.printStackTrace(ioe);
213                                 }
214                             }
215
216                             public void cancel () {}
217                         }, true);
218                     } catch (IOException JavaDoc ioe) {
219                         Exceptions.printStackTrace(ioe);
220                     }
221                 }
222                 synchronized (this) {
223                     this.dirty = null;
224                 }
225                 final long endTime = System.currentTimeMillis();
226                 LOGGER.fine("PersistentClassIndex.updateDirty took: " + (endTime-startTime)+ " ms"); //NOI18N
227
}
228         }
229     }
230     
231     private <T> void usages (final String JavaDoc binaryName, final Set JavaDoc<UsageType> usageType, ResultConvertor<T> convertor, Set JavaDoc<? super T> result) {
232         final List JavaDoc<String JavaDoc> classInternalNames = this.getUsagesFQN(binaryName,usageType, Index.BooleanOperator.OR);
233         for (String JavaDoc classInternalName : classInternalNames) {
234             T value = convertor.convert(ElementKind.OTHER, classInternalName);
235             if (value != null) {
236                 result.add(value);
237             }
238         }
239     }
240     
241     private List JavaDoc<String JavaDoc> getUsagesFQN (final String JavaDoc binaryName, final Set JavaDoc<UsageType> mask, final Index.BooleanOperator operator) {
242         List JavaDoc<String JavaDoc> result = null;
243         try {
244             result = this.index.getUsagesFQN(binaryName, mask, operator);
245         } catch (IOException JavaDoc ioe) {
246             Exceptions.printStackTrace(ioe);
247         }
248         if (result == null) {
249             result = Collections.emptyList();
250         }
251         return result;
252     }
253 }
254
Popular Tags