Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all classes from the current workspace in Eclipse

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?

like image 849
sparrow Avatar asked Jul 17 '10 19:07

sparrow


1 Answers

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!

like image 143
Woody Zenfell III Avatar answered Nov 15 '22 03:11

Woody Zenfell III