First, add the following plug-in dependencies to your project ( Require-Bundle in your Manifest.MF):
There are several Java discoverer classes that do the same discovery but on different inputs:
You can easily discover a Java model programmatically. For example, to discover a Java model from a Java project:
DiscoverJavaModelFromJavaProject discoverer = new DiscoverJavaModelFromJavaProject(); javaDiscoverer.discoverElement(javaProject, monitor); Resource javaResource = javaDiscoverer.getTargetModel();
To have a monitor to pass to the discoverElement
method, you can either call the discoverer in an Eclipse Job, or pass a new NullProgressMonitor if you don't need progress reporting.
Once you have the Java Resource, you can use the standard EMF API to read model elements. For example, to get the root:
Model javaModel = (Model) javaResource.getContents().get(0);
To print the list of Java classes in the model:
EList<ClassFile> classFiles = javaModel.getClassFiles(); for (ClassFile classFile : classFiles) { System.out.println(classFile.getName()); }