Getting all classes from the current workspace in Eclipse

I am writing an Eclipse plugin and I was trying to create a method that returns all the classes in the workspace in ArrayList <\ Class <\? → (I added "\" to include common brackets with html will not allow me to do this 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, does not seem to work. I had some print lines, and I realized that it also includes irrelevant classes (i.e. Classes from Java \ jre6 \ lib \ rt.jar). Any suggestions?

+7
java eclipse plugins class
source share
4 answers

I'm not sure what you want to do:

  • In the running Eclipse plug-in, show all the classes running in the JVM with the plug-in (for example, for other editors, views, Eclipse machines)?
  • In the running Eclipse plug-in show all classes created in open Java projects in the workspace? (Since you used the word “workspace,” I suspect that this is what you are looking for.)

Please note that in the latter case, you cannot get the actual Java Class<...> objects, because projects edited and compiled are not loaded for execution in the same JVM as your plugin. Your plug-in code will run along with the Eclipse IDE and Eclipse JDT tool code; only time classes in open projects will be loaded for execution (creating Class<...> objects somewhere) will be when starting or debugging one of these projects., in this case you are dealing with a completely new JVM, and your plugin will no longer exists. It makes sense?

If I read you correctly, I think you will probably want to find “compilation units” rather than “class files”. The "compilation units" correspond to the source .java files, and the "class files" correspond to the pre-built binary class files (often in the JAR). Or maybe you need both. Even better, it looks like what you really want, these are the “types” inside them.

Check out the JDT Core guide for some pretty good information that is very hard to find. Please note that some analysis is possible at the Java model level, but more detailed analyzes (for example, searching “inside” method definitions) will require parsing code fragments in the AST and moving from there. The Java model is pretty usable, but AST can be a little complicated for the first time.

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

Good luck

+1
source share

You must try:

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

You do not get CompilationUnit for binary types.

+1
source share

Perhaps you can use the IJavaProject.getAllPackageFragmentRoots () method to get the entire source folder, and then get ICompilationUnits in it.

0
source share

I have a much simpler solution for the eclipse project if you are just looking for java class names for the current package and add them to the class list (please replace PARENT_CLASS with 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
source share

All Articles