finding all Java classes in a project
Dave Weatherford <[email protected]> Tue, 24 Jun 2003 17:26:14 -0700
| Newsgroups | gmane.comp.java.netbeans.modules.projects.devel |
|---|---|
| Organization | Sun Microsystems |
| Message-ID | <[email protected]> |
I need to create a list of all the classes which are in the current project.
What I wound up doing is going through the CompilationUnit(s) to get the output
folder(s) and deriving the classnames from the names of the .class files; not
very pretty. :-( See code below. There must be a better way!
I tried JavaProject.getSources(), but that just returned two ProjectMembers,
both empty, named Collections/java-src.jsrc and Collections/src.jsrc .
I saw that Svata recommended using JavaProject.getClassesDescriptor(), but I
have not been successful; it always returns null for me.
Surely I'm missing something. Any and all suggestions appreciated!
Dave W.
---------------------------------------------------------------------------------
[...]
int suffixLen = ".class".length();
Project project = debugger.getProject();
JavaProject jproj = JavaProject.getJavaProject(project);
Collection cus = jproj.getCompilationUnits();
Iterator cuit = cus.iterator();
while ( cuit.hasNext() ) {
CompilationUnit cu = (CompilationUnit)cuit.next();
FileSet outFolders = cu.getOutputLocations(null);
Iterator fit = outFolders.iterator();
while ( fit.hasNext() ) {
String fileName = ((java.net.URL)fit.next()).getFile();
System.out.println("Output Folder: " + fileName);
File file = new File(fileName);
ArrayList list = new ArrayList();
walk(file, list);
String baseDir = file.getPath();
int prefixLen = baseDir.length() + 1;
Iterator lit = list.iterator();
while ( lit.hasNext() ) {
String path = ((File)lit.next()).getPath();
if ( path.endsWith(".class") ) {
int len = path.length();
String className =
path.substring(prefixLen, len - suffixLen);
className = className.replace(File.separatorChar, '.');
System.out.println(className);
classNames.add(className);
} else {
System.out.println("UNRECOGNIZED: " + path);
}
}
}
}
}
private void walk( File file, List list ) {
if ( file.isDirectory() ) {
String[] kids = file.list();
for ( int i = 0; i < kids.length; i++ ) {
walk(new File(file.toString()+ File.separator + kids[i]), list);
}
} else {
list.add(file);
}
}
--
Dave Weatherford Sun ONE Developer Products
[email protected] Sun Microsystems, Inc.