Skip to main content



      Home
Home » Modeling » MoDisco » From .jar to any model(How to get a representation of a .jar file with modisco?)
From .jar to any model [message #1052873] Tue, 30 April 2013 16:38 Go to next message
Eclipse UserFriend
Hi all,

I want to use Modisco to translate a .jar file into a model, and later query it with OCL.

Is possible to create a model from a jar file?

Thanks in advance.
Re: From .jar to any model [message #1053035 is a reply to message #1052873] Thu, 02 May 2013 03:28 Go to previous messageGo to next message
Eclipse UserFriend
Hi Edson,

there is one Modisco Discoverer which takes a .jar as input for creating some "Java" model.
The discoverer is available for direct use from the workspace : right clibk on ".jar" file -> "Discovery->Discoverers->Discover Java Model From Library..."
The discoverer is available from public java API (org.eclipse.modisco.java.discoverer plugin).

Regards,
Fabien.
Re: From .jar to any model [message #1053047 is a reply to message #1053035] Thu, 02 May 2013 04:06 Go to previous messageGo to next message
Eclipse UserFriend
Hello,

In addition to Fabien's answer, and concerning more the querying part once you have already gotten your Java model, note that you can also use ATL (which is an OCL-based model transformation language).

Hugo
Re: From .jar to any model [message #1053285 is a reply to message #1053047] Fri, 03 May 2013 10:49 Go to previous messageGo to next message
Eclipse UserFriend

Thank you for your prompt reply, Smile

Based on the documentation I wrote this little example.

But I have another question, How can I "set" the jar path to the FragmentRoot?

Thanks in advance.

  DiscoverJavaModelFromLibrary discover = new DiscoverJavaModelFromLibrary();
    String path = "/tmp/test.jar";
    IFile jar = ... ;

    IPackageFragmentRoot root = JavaCore.createJarPackageFragmentRootFrom(jar);
    IProgressMonitor monitor = new NullProgressMonitor();

    /* create model */
    discover.discoverElement(root,  monitor);
    Resource resource = discover.getTargetModel();
    Model model = (Model) resource.getContents().get(0);

    /* list methods */
    EList<ClassFile> classFiles = model.getClassFiles();
    for (ClassFile classFile : classFiles) {
      System.out.println(classFile.getName());
    }

Re: From .jar to any model [message #1059738 is a reply to message #1053285] Tue, 21 May 2013 09:12 Go to previous message
Eclipse UserFriend
Thank you Fabien and Hugo,

This is the method to extract the model from a given jar file. The method saves the model into a .xmi file. It's working, but need a refactoring.

Problem solved. Smile

public void getModel(String jar) throws RemoteException {

		if (jar == null)
			return;

		try {
			/* Open eclipse project */
			IWorkspace ws = ResourcesPlugin.getWorkspace();
			IProject project = ws.getRoot().getProject("FeatureExtractor");
			IProgressMonitor monitor = new NullProgressMonitor();

			/* Create a new project, in case there isn't one */
			if (!project.exists()) {
				IProjectDescription description = project.getDescription();
				description.setNatureIds(new String[] {"org.eclipse.jdt.core.javanature"});
				project.setDescription(description, monitor);
				project.create(monitor);
				if (!project.isOpen())
					project.open(monitor);	
			}

			/* Create a link to the jar file, which is outside the project */
			IPath location = new Path(jar);
			IFile file = project.getFile(location.lastSegment());
			file.createLink(location, IResource.REPLACE, monitor);

			/* Create Java project, and 
			 * add location (jar file) to the build path */
			IJavaProject javaProject = JavaCore.create(project);
			IPath filePath = project.getWorkspace().getRoot().getLocation().append(file.getFullPath());

			/* Set project fragment roots */
			Set<IClasspathEntry> e = new HashSet<IClasspathEntry>();
			e.addAll(Arrays.asList(javaProject.getRawClasspath()));			
			boolean entryExist = false;
			for (IClasspathEntry c : javaProject.getRawClasspath()) {
				if ( filePath.toOSString().matches(c.getPath().toOSString()) ) {
					entryExist = true;
					break;
				}
			}			
			IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
			LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall);
			for (LibraryLocation element : locations) {
				e.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
			}
			for (IPackageFragmentRoot r : javaProject.getPackageFragmentRoots()) {
				if ( filePath.toOSString().matches(r.getPath().toOSString()) ) {
					entryExist = true;
					break;
				}
			}	
			if (!entryExist) {
				e.add(JavaCore.newLibraryEntry(filePath, null, null));
			}
			javaProject.setRawClasspath(e.toArray(new IClasspathEntry[e.size()]), monitor);

			/* Create discover for jar file */
			DiscoverJavaModelFromLibrary discover = new DiscoverJavaModelFromLibrary();
			IPackageFragmentRoot root = JavaCore.createJarPackageFragmentRootFrom(file);
			if (root == null) {
				System.out.println("Error: Fragment root is null. Aborting...");
				return null;		
			}

			/* Create model from jar file */
			discover.discoverElement(root, monitor);
			Resource resource = discover.getTargetModel();
			// Model model = (Model) resource.getContents().get(0);

			/* Save Model XMI */
			exportModelToXMI(resource, filePath.toOSString().replace(".jar", ".xmi"));

		} catch (CoreException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (DiscoveryException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (ParserException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
Previous Topic:A simpler ecore java model?
Next Topic:How to extract the complete source code, not only the signatures
Goto Forum:
  


Current Time: Thu May 22 19:33:14 EDT 2025

Powered by FUDForum. Page generated in 0.03524 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top