views:

113

answers:

4

Hi, am writing an Eclipse plugin, and I was trying to create a method that returns all the classes in the workspace in an ArrayList<\Class<\?>> (I added the "\" to include the generic brackets since html won't let me do so otherwise).

Here is the code I have:

private ArrayList<Class<?>> getAllClasses() throws JavaModelException {

 ArrayList<Class<?>> classList = new ArrayList<Class<?>>();

 IWorkspace workspace = ResourcesPlugin.getWorkspace();
 IWorkspaceRoot root = workspace.getRoot();
 IProject[] projects = root.getProjects();

 for (IProject project : projects) {
  IJavaProject javaProject = JavaCore.create(project);
  IPackageFragment[] packages = javaProject.getPackageFragments();

  for (IPackageFragment myPackage : packages) {
   IClassFile[] classes = myPackage.getClassFiles();

   for (IClassFile myClass : classes) {
    classList.add(myClass.getClass());
   }
  }
 }

    return classList;
}

This, however, doesn't seem to be working. I had some printlines, and I figured out that it also includes irrelevant classes (ie. classes from Java\jre6\lib\rt.jar). Any suggestions?

A: 

change

from

for (IClassFile myClass : classes) {

to

if (myPackage.getKind() != 2)
for (IClassFile myClass : classes) {
01
Where and how should that be added?Thanks!
sparrow
This doesn't seem to be working. That conditional seems to never be true, since when I execute the program, I have included some printlines in there and see none of them. I tried instead:if (myPackage.getKind() == IPackageFragmentRoot.K_BINARY)But this seems to return the same class over and over (it's "class org.eclipse.jdt.internal.core.ClassFile"). I tried it also with IPackageFragmentRoot.K_SOURCE, but I got no results at all. Isn't there a Java built-in method to get all the classes from all the projects you have opened in Eclipse?
sparrow
Maybe you should take a look at the StandardJavaElementContentProvider, which is the root class for many of those contentproviders within Eclipse. The Package Explorer itself uses the PackageExplorerContentProvider or WorkingSetAwareContentProvider, which probably do exactly what you are looking for.As I see it, you have to check for yourself at IJavaProject-Level, whether to include it or not by evaluating the classpath.
pimpf0r
Shouldn't there be a literal instead of the "2"? Kinda hard to read/understand in this way.
ShiDoiSi
A: 

Maybe you can use IJavaProject.getAllPackageFragmentRoots() method to get all source folder,and then get ICompilationUnits in it.

Safrain
+1  A: 

I'm not sure what you want to do:

  • In a running Eclipse plug-in, show all classes that are running in the JVM with the plug-in (i.e. classes for other editors, views, Eclipse machinery)?
  • In a running Eclipse plug-in, show all classes being built in open Java projects in the workspace? (Since you used the word "workspace", I suspect this is what you're looking for.)

Note in the latter case, you will not be able to get actual Java Class<...> objects, because the projects being edited and compiled are not loaded for execution into the same JVM as your plug-in. Your plug-in's code would be executing alongside the Eclipse IDE and Eclipse JDT tool code; the only time classes in open projects would be loaded for execution (producing Class<...> objects somewhere) would be when you launch or debug one of those projects . . . in which case you're dealing with a brand new JVM, and your plug-in is no longer around. Does that make sense?

If I am reading you right, I think you probably want to find "compilation units", not "class files". "Compilation units" correspond with .java source files, while "class files" correspond with pre-built binary class files (often in JARs). Or maybe you need both. Better yet, it sounds like what you really want are the "types" inside those.

Check out the JDT Core guide for some pretty good information that's remarkably difficult to find. Note that some analysis is possible at the Java Model level, but more detailed analyses (e.g. looking "inside" method definitions) will require parsing chunks of code into ASTs and going from there. The Java Model is pretty convenient to use, but the AST stuff can be a little daunting the first time out.

Also consider the Java search engine (documented near the above) and IType.newTypeHierarchy() for finding and navigating types.

Good luck!

Woody Zenfell III
A: 

You should try :

for (final ICompilationUnit compilationUnit : packageFragment.getCompilationUnits()) {
       // now check if compilationUnit.exits
}

You don't get a CompilationUnit for binary types.

fastcodejava