Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » MoDisco » discover Java/KDM Model programaticaly
discover Java/KDM Model programaticaly [message #575689] Thu, 19 August 2010 07:25 Go to next message
Björn is currently offline Björn
Messages: 29
Registered: August 2010
Junior Member
Hi

i want to write a program that can discover a Javamodel from Java sourcecode.

This is what Fred Madiot pointed out to me already:

Quote:
> Fred Madiot said...
>
> Hi,
>
> Here is an example of code showing how to programmatically create a java model from java source code :
>
>
> // Create a discoverer for a Java project
> DiscoverJavaModelFromJavaProject javaDiscoverer = new DiscoverJavaModelFromJavaProject();
>
> // Parameters of the discoverer
> Map javaDiscoveryParameters = new HashMap();
> javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_SILE NT_MODE,true);
> javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_BROW SE_RESULT,false);
>
> // Execute the discoverer (javaProject is a IJavaProject)
> javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);
>
> // Get the result model
> Resource javaModel=(Resource) javaDiscoveryParameters .get(DefaultDiscoverer.PARAMETER_TARGET_RESOURCE)
>
>
> If you need help, you can post your questions directly on the MoDisco forum :
> http://www.eclipse.org/forums/index.php?t=thread&frm_id= 21&
> August 12, 2010 1:01 AM


that is a very great hint and I got it almost to work that way, apart from a few NullPointerExceptions....

Now my question is how do I construct a IJavaProject from the sourcecode located sommewhere on my disk?


I want to build a function something like:

public IJavaProject getJavaProjectFromSource(String pathToSource);


Does anyone have an idea how to do that? Or does someone know that something like that has been done already?

I will be very pleased for every little hint on the subject.
Thanx very much in advance.

Cheers Björn
Re: discover Java/KDM Model programaticaly [message #575706 is a reply to message #575689] Thu, 19 August 2010 09:13 Go to previous message
Esteban Dugueperoux is currently offline Esteban Dugueperoux
Messages: 289
Registered: July 2009
Senior Member
Hi,

Modisco Java Discoverer uses the JDT DOM Parser to visit a
org.eclipse.jdt.core.dom.CompilationUnit and transforms it to a
org.eclipse.gmt.modisco.java.Model, then you could use direclty the
org.eclipse.gmt.modisco.java.io.java.JDTVisitor and the
org.eclipse.jdt.core.dom.ASTParser to produces your wanted
org.eclipse.gmt.modisco.java.Model

I have writen a simple example below :

package test;

import java.io.File;
import java.io.FileReader;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.gmt.modisco.java.Model;
import org.eclipse.gmt.modisco.java.emf.JavaFactory;
import org.eclipse.gmt.modisco.java.io.java.JDTVisitor;
import org.eclipse.gmt.modisco.java.io.java.binding.BindingManager;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstant s;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

public class ModiscoTests {

public static void main(String[] args) throws Exception {
File file = new File("datas/Comp111Impl.java");
ASTParser parser = ASTParser.newParser(AST.JLS3);
FileReader fileReader = new FileReader(file);
char buffer[] = new char[(int) file.length()];
fileReader.read(buffer);
parser.setSource(buffer);
parser.setResolveBindings(true);

CompilerOptions options = new CompilerOptions();
options.verbose = true;
options.complianceLevel = ClassFileConstants.JDK1_5;
options.sourceLevel = ClassFileConstants.JDK1_5;

parser.setCompilerOptions(options.getMap());
CompilationUnit compilationUnit = (CompilationUnit) parser
.createAST(null);
compilationUnit.recordModifications();

JavaFactory factory = JavaFactory.eINSTANCE;
Model model = factory.createModel();
BindingManager bindingManager = new BindingManager(factory);
String path = file.getCanonicalPath();
String javaContent = String.copyValueOf(buffer);
JDTVisitor visitor = new JDTVisitor(factory, model, bindingManager,
path, javaContent, false, false);
compilationUnit.accept(visitor);

ResourceSet resourceSet = new ResourceSetImpl();
URI javaModelURI =
URI.createFileURI(file.getName().replaceFirst("java", ".javaxmi"));
Resource resource = resourceSet.createResource(javaModelURI);
resource.getContents().add(model);
resource.save(Collections.emptyMap());
}
}


But unfortunately Modisco is tied to OSGI Runtime Model (it uses
BundleActivator to get runtime informations), then this sample isn't
usable standalone. It would be nice to have Modisco usable standalone,
by using the org.eclipse.core.runtime.Platform.isRunning() operation
when accessing BundleActivator.
I had the same problem in the past.

Best Regards.

On 19/08/2010 13:25, bjorn.t@web.de wrote:
> Hi
>
> i want to write a program that can discover a Javamodel from Java
> sourcecode.
>
> This is what Fred Madiot pointed out to me already:
>
> Quote:
>> Fred Madiot said...
>>
>> Hi,
>>
>> Here is an example of code showing how to programmatically create a
>> java model from java source code :
>>
>>
>> // Create a discoverer for a Java project
>> DiscoverJavaModelFromJavaProject javaDiscoverer = new
>> DiscoverJavaModelFromJavaProject();
>>
>> // Parameters of the discoverer
>> Map javaDiscoveryParameters = new HashMap();
>> javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_SILE NT_MODE,true);
>>
>> javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_BROW SE_RESULT,false);
>>
>>
>> // Execute the discoverer (javaProject is a IJavaProject)
>> javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);
>>
>> // Get the result model
>> Resource javaModel=(Resource) javaDiscoveryParameters
>> .get(DefaultDiscoverer.PARAMETER_TARGET_RESOURCE)
>>
>>
>> If you need help, you can post your questions directly on the MoDisco
>> forum :
>> http://www.eclipse.org/forums/index.php?t=thread&frm_id= 21&
>> August 12, 2010 1:01 AM
>
>
> that is a very great hint and I got it almost to work that way, apart
> from a few NullPointerExceptions....
>
> Now my question is how do I construct a IJavaProject from the sourcecode
> located sommewhere on my disk?
>
>
> I want to build a function something like:
>
> public IJavaProject getJavaProjectFromSource(String pathToSource);
>
>
> Does anyone have an idea how to do that? Or does someone know that
> something like that has been done already?
>
> I will be very pleased for every little hint on the subject.
> Thanx very much in advance.
>
> Cheers Björn
>
Previous Topic:Query Manager
Next Topic:Metrics Visualizations Builder
Goto Forum:
  


Current Time: Mon Jul 28 16:36:37 EDT 2014

Powered by FUDForum. Page generated in 0.04110 seconds