开发者

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?


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!


You should try :

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

You don't get a CompilationUnit for binary types.


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


I have much simpler solution for an eclipse project if you're just looking for listing java class names for the current package and add them to a list of classes (please replace PARENT_CLASS by the parent class name of all your classes):

    List<PARENT_CLASS> arrayListOfClasses = new ArrayList<>();

    String currentDir  = new java.io.File("").toURI().toString().split("file:/")[1];
    System.out.println("currentDir=" + currentDir);
    String[] dirStringTab = currentDir.split("/");
    String currentPackageName = dirStringTab[dirStringTab.length-1];
    System.out.println("currentPackageName=" + currentPackageName);

    File dir = new File("./src/" + currentPackageName + "/");
    File[] filesList = dir.listFiles();
    String javaClassNameWithoutExtension = "";

    for (File file : filesList) {
        if (file.isFile()) {
            System.out.println(javaClassNameWithoutExtension);
            javaClassNameWithoutExtension = file.getName().split(".java")[0];

        Class c  = Class.forName(javaClassNameWithoutExtension); 
        Object a = c.newInstance();
        arrayListOfClasses.add(a);

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜