Re: finding all Java classes in a project
Svatopluk Dedic <[email protected]> Wed, 25 Jun 2003 07:54:54 +0200
| Newsgroups | gmane.comp.java.netbeans.modules.projects.devel |
|---|---|
| Message-ID | <[email protected]> |
Dave Weatherford wrote:
>
> I tried JavaProject.getSources(), but that just returned two ProjectMembers,
> both empty, named Collections/java-src.jsrc and Collections/src.jsrc .
>
[not checked] I think the javadoc says that this method returns project
representants for the sources, not the sources (folders) themselves.
> I saw that Svata recommended using JavaProject.getClassesDescriptor(),
> but I
> have not been successful; it always returns null for me.
>
This is surely a bug, if applied on the CU's build target.
JavaProject.getClassesDescriptor(someCU.getBuildTarget()) ought to work.
Please also see #34364 for convenience methods request.
> 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();
At this point, you may retrieve getBuildTarget() and query the target's
output FileSet (which saves you from walking the java.io.File subtrees
manually).
> 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, '.');
If you used JavaDescriptor interface, you could also obtain the
ClassPath object, which is capable to convert filename to a "resource
name" - strip the classpath root prefix from the file (and some other
things, too).
Folks, this is the second request to obtain class _names_ rather than
files; do you think it deserves some helper to be implemented in APIs ?
-Svata