KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.api.retouche.source;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import javax.swing.event.EventListenerList JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.platform.JavaPlatformManager;
30 import org.netbeans.api.retouche.source.ClassIndex;
31 import org.netbeans.modules.retouche.source.CacheClassPath;
32 import org.netbeans.modules.retouche.source.usages.ClasspathInfoAccessor;
33 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.WeakListeners;
38
39 /**
40  * This file is originally from Retouche, the Java Support
41  * infrastructure in NetBeans. I have modified the file as little
42  * as possible to make merging Retouche fixes back as simple as
43  * possible.
44  *
45  * Class which contains info about classpath
46  *
47  * @author Tomas Zezula, Petr Hrebejk
48  */

49 public final class ClasspathInfo {
50     
51     private static final ClassPath EMPTY_PATH = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
52     
53     static {
54         ClasspathInfoAccessor.INSTANCE = new ClasspathInfoAccessorImpl ();
55         try {
56             Class.forName(ClassIndex.class.getName(), true, CompilationInfo.class.getClassLoader());
57         } catch (ClassNotFoundException JavaDoc ex) {
58             ErrorManager.getDefault().notify (ex);
59         }
60     }
61     
62 // private final CachingArchiveProvider archiveProvider;
63

64     private final ClassPath srcClassPath;
65     private final ClassPath bootClassPath;
66     private final ClassPath compileClassPath;
67     private ClassPath outputClassPath;
68     
69     private final ClassPathListener cpListener;
70     private final boolean backgroundCompilation;
71     private EventListenerList JavaDoc listenerList = null;
72     private ClassIndex usagesQuery;
73     
74     /** Creates a new instance of ClasspathInfo (private use the fatctory methods) */
75     private ClasspathInfo(/*CachingArchiveProvider archiveProvider,*/ ClassPath bootCp, ClassPath compileCp, ClassPath srcCp,
76         Object JavaDoc /*JavaFileFilterImplementation*/ filter, boolean backgroundCompilation) {
77         assert /*archiveProvider != null && */ bootCp != null && compileCp != null;
78         this.cpListener = new ClassPathListener ();
79         //this.archiveProvider = archiveProvider;
80
this.bootClassPath = CacheClassPath.forBootPath(bootCp);
81         this.compileClassPath = CacheClassPath.forClassPath(compileCp);
82     this.bootClassPath.addPropertyChangeListener(WeakListeners.propertyChange(this.cpListener,this.bootClassPath));
83     this.compileClassPath.addPropertyChangeListener(WeakListeners.propertyChange(this.cpListener,this.compileClassPath));
84     if ( srcCp != null ) {
85             this.srcClassPath = srcCp;
86             this.outputClassPath = CacheClassPath.forSourcePath (this.srcClassPath);
87         this.srcClassPath.addPropertyChangeListener(WeakListeners.propertyChange(this.cpListener,this.srcClassPath));
88     }
89         else {
90             this.srcClassPath = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
91             this.outputClassPath = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
92         }
93         this.backgroundCompilation = backgroundCompilation;
94         //this.filter = filter;
95
}
96     
97     public String JavaDoc toString() {
98         return "ClasspathInfo boot:[" + bootClassPath + "],compile:[" + compileClassPath + "],src:[" + srcClassPath + "]"; //NOI18N
99
}
100     
101     // Factory methods ---------------------------------------------------------
102

103     
104     /** Creates new interface to the compiler
105      * @param file for which the CompilerInterface should be created
106      * @return ClasspathInfo or null if the file does not exist on the
107      * local file system or it has no classpath associated
108      */

109     public static ClasspathInfo create (final File JavaDoc file) {
110         if (file == null) {
111             throw new IllegalArgumentException JavaDoc ("Cannot pass null as parameter of ClasspathInfo.create(java.io.File)"); //NOI18N
112
}
113         final FileObject fo = FileUtil.toFileObject(file);
114         if (fo == null) {
115             return null;
116         }
117         else {
118             return create (fo);
119         }
120     }
121     
122     
123     private static ClasspathInfo create (FileObject fo, Object JavaDoc/*JavaFileFilterImplementation*/ filter, boolean backgroundCompilation) {
124         ClassPath bootPath = ClassPath.getClassPath(fo, ClassPath.BOOT);
125         if (bootPath == null) {
126             //javac requires at least java.lang
127
bootPath = JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries();
128         }
129         ClassPath compilePath = ClassPath.getClassPath(fo, ClassPath.COMPILE);
130         if (compilePath == null) {
131             compilePath = EMPTY_PATH;
132         }
133         ClassPath srcPath = ClassPath.getClassPath(fo, ClassPath.SOURCE);
134         if (srcPath == null) {
135             srcPath = EMPTY_PATH;
136         }
137         return create (bootPath, compilePath, srcPath, filter, backgroundCompilation);
138     }
139     
140     /** Creates new interface to the compiler
141      * @param fo for which the CompilerInterface should be created
142      */

143     public static ClasspathInfo create(FileObject fo) {
144         return create (fo, null, false);
145     }
146     
147     private static ClasspathInfo create(ClassPath bootPath, ClassPath classPath, ClassPath sourcePath, Object JavaDoc/*JavaFileFilterImplementation*/ filter, boolean backgroundCompilation) {
148         return new ClasspathInfo(/*CachingArchiveProvider.getDefault(),*/ bootPath, classPath, sourcePath, filter, backgroundCompilation);
149     }
150     
151     public static ClasspathInfo create(ClassPath bootPath, ClassPath classPath, ClassPath sourcePath) {
152         return new ClasspathInfo(/*CachingArchiveProvider.getDefault(),*/ bootPath, classPath, sourcePath, null, false);
153     }
154        
155     // Public methods ----------------------------------------------------------
156

157     /** Registers ChangeListener which will be notified about the changes in the classpath.
158      * @param listener The listener to register.
159      */

160     public synchronized void addChangeListener(ChangeListener JavaDoc listener) {
161         if (listenerList == null ) {
162             listenerList = new EventListenerList JavaDoc();
163         }
164         listenerList.add (ChangeListener JavaDoc.class, listener);
165     }
166
167     /**Removes ChangeListener from the list of listeners.
168      * @param listener The listener to remove.
169      */

170     public synchronized void removeChangeListener(ChangeListener JavaDoc listener) {
171         listenerList.remove (ChangeListener JavaDoc.class, listener);
172     }
173
174     public ClassPath getClassPath (PathKind pathKind) {
175     switch( pathKind ) {
176         case BOOT:
177         return this.bootClassPath;
178         case COMPILE:
179         return this.compileClassPath;
180         case SOURCE:
181         return this.srcClassPath;
182         case OUTPUT:
183         return this.outputClassPath;
184         default:
185         assert false : "Unknown path type"; //NOI18N
186
return null;
187     }
188     }
189     
190     
191     public synchronized ClassIndex getClassIndex () {
192         if ( usagesQuery == null ) {
193             usagesQuery = new ClassIndex (
194                     this.bootClassPath,
195                     this.compileClassPath,
196                     this.srcClassPath);
197         }
198         return usagesQuery;
199     }
200     
201     // Package private methods -------------------------------------------------
202
//
203
// synchronized JavaFileManager getFileManager() {
204
// if (this.fileManager == null) {
205
// boolean hasSources = this.srcClassPath != null;
206
// this.fileManager = new ProxyFileManager (
207
// new CachingFileManager (this.archiveProvider, this.bootClassPath, true),
208
// new CachingFileManager (this.archiveProvider, this.compileClassPath, false),
209
// hasSources ? (backgroundCompilation ? new CachingFileManager (this.archiveProvider, this.srcClassPath, filter, false)
210
// : new SourceFileManager (this.srcClassPath.getRoots())) : null,
211
// hasSources ? new OutputFileManager (this.archiveProvider, this.outputClassPath, this.srcClassPath) : null
212
// );
213
// }
214
// return this.fileManager;
215
// }
216

217     // Private methods ---------------------------------------------------------
218

219     private void fireChangeListenerStateChanged() {
220         ChangeEvent JavaDoc e = null;
221         if (listenerList == null) return;
222         Object JavaDoc[] listeners = listenerList.getListenerList ();
223         for (int i = listeners.length - 2; i >= 0; i -= 2) {
224             if (listeners[i]==ChangeListener JavaDoc.class) {
225                 if (e == null)
226                     e = new ChangeEvent JavaDoc(this);
227                 ((ChangeListener JavaDoc)listeners[i+1]).stateChanged (e);
228            }
229         }
230     }
231
232
233     // Innerclasses ------------------------------------------------------------
234

235     public static enum PathKind {
236     BOOT,
237     COMPILE,
238     SOURCE,
239     OUTPUT,
240     
241     }
242     
243     private class ClassPathListener implements PropertyChangeListener JavaDoc {
244         
245         public void propertyChange (PropertyChangeEvent JavaDoc event) {
246             if (ClassPath.PROP_ROOTS.equals(event.getPropertyName())) {
247                 synchronized (this) {
248                     // Kill FileManager
249
//fileManager = null;
250
// Kill indexes
251
usagesQuery = null;
252                 }
253                 fireChangeListenerStateChanged();
254             }
255         }
256     }
257     
258     private static class ClasspathInfoAccessorImpl extends ClasspathInfoAccessor {
259         
260 // @Override
261
// public JavaFileManager getFileManager(ClasspathInfo cpInfo) {
262
// return cpInfo.getFileManager();
263
// }
264

265         @Override JavaDoc
266         public ClasspathInfo create (ClassPath bootPath, ClassPath classPath, ClassPath sourcePath, Object JavaDoc/*JavaFileFilterImplementation*/ filter, boolean backgroundCompilation) {
267             return ClasspathInfo.create(bootPath, classPath, sourcePath, filter, backgroundCompilation);
268         }
269         
270         @Override JavaDoc
271         public ClasspathInfo create (FileObject fo, Object JavaDoc /*JavaFileFilterImplementation*/ filter, boolean backgroundCompilation) {
272             return ClasspathInfo.create(fo, filter, backgroundCompilation);
273         }
274     }
275 }
276
Popular Tags