Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » MoDisco » Creating a Java Class Programmatically by means of Java Model
Creating a Java Class Programmatically by means of Java Model [message #1100258] Mon, 02 September 2013 22:53 Go to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Hello guys.


Now I am having another issue. Now I don't know how to instantiate the class Type programmatically. I'am trying to create a Java class by means of Java Model. Thus, firstly, I created an instance of ClassDeclaration. Then, I set all information of this class, such visibility, also instantiated the Class Modifier to set the ClassDeclaration with the keywords Abstract and Public. But I'm facing an issue and I need to create an attribute to this Class. I know that I have to instantiate an FieldDeclaration, this is done. THe real problem is to instantiate the classes TypeAccess and Type.

See the source-code bellow:


Modifier modifierClass = JavaFactory.eINSTANCE.createModifier();
				
				modifierClass.setInheritance(InheritanceKind.ABSTRACT);
				modifierClass.setVisibility(VisibilityKind.PUBLIC);
				modifierClass.setStatic(false);
				modifierClass.setTransient(false);
				modifierClass.setVolatile(false);	
				modifierClass.setNative(false);
				modifierClass.setStrictfp(false);
				modifierClass.setSynchronized(false);
				
				
				Modifier modifierAtt = JavaFactory.eINSTANCE.createModifier();
				
				modifierAtt.setVisibility(VisibilityKind.PRIVATE);
				modifierAtt.setInheritance(InheritanceKind.NONE);
				modifierAtt.setStatic(false);
				modifierAtt.setTransient(false);
				modifierAtt.setVolatile(false);	
				modifierAtt.setNative(false);
				modifierAtt.setStrictfp(false);
				modifierAtt.setSynchronized(false);

FieldDeclaration attibute1 = JavaFactory.eINSTANCE.createFieldDeclaration();
				
				attibute1.setModifier(modifierAtt);
				attibute1.setName("kind");
				attibute1.setType(typeAcessFinal);

ClassDeclaration classDecl = JavaFactory.eINSTANCE.createClassDeclaration();
				classDecl.setName("Animal");
				classDecl.setModifier(modifierClass);
				classDecl.getBodyDeclarations().add(attibute1);
	



Thus, how to instantiate the the class Type programmatically?
Re: Creating a Java Class Programmatically by means of Java Model [message #1100520 is a reply to message #1100258] Tue, 03 September 2013 08:01 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
As I stated earlier I need create a Class programmatically by means of Java Model.

public class Test {

private String problem; \\ I'm having problem to know how to instantiate an attribute and its type by using the Java Model. 


}



Can someone helps me?
Re: Creating a Java Class Programmatically by means of Java Model [message #1100575 is a reply to message #1100520] Tue, 03 September 2013 09:31 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
Create a sample of what you want to generate, inject it in a JavaModel. Study this JavaModel to know what you need to do.
Re: Creating a Java Class Programmatically by means of Java Model [message #1100624 is a reply to message #1100575] Tue, 03 September 2013 11:02 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Sylvain EVEILLARD can you please show me an example of how to inject into the Java Model?


I want to create this simple class programmatically..


public class Test {

private String name;


}

Re: Creating a Java Class Programmatically by means of Java Model [message #1100636 is a reply to message #1100258] Tue, 03 September 2013 11:21 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
I have been trying but i didn't have succeed..

I got the following error :

The object 'org.eclipse.gmt.modisco.java.emf.impl.ClassDeclarationImpl@2c8376b (name: null, proxy: true)' is not contained in a resource.
Re: Creating a Java Class Programmatically by means of Java Model [message #1100686 is a reply to message #1100636] Tue, 03 September 2013 12:50 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo BruneliereFriend
Messages: 674
Registered: July 2009
Senior Member
Hello,

If you have MoDisco installed in your Eclipse (as I think it is), you can quickly get a Java model out of your Java project by right-clicking on it and then select "Discovery -> Discoverers -> Discover Java project...".
For more information on the Java discoverer, I encourage you to take a look to the corresponding section of the MoDisco documentation.

Once done, you can navigate this model using the MoDisco Model Browser and then find out about its structure.

Best regards,


--------------------------------------------------------
Hugo Bruneliere, PhD
NaoMod team (IMT Atlantique & LS2N-CNRS)
Nantes - France
--------------------------------------------------------
Re: Creating a Java Class Programmatically by means of Java Model [message #1100708 is a reply to message #1100258] Tue, 03 September 2013 13:28 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
I have already done that but I could figured out how to create a TypeAccess and Type (Attribute) in Java.

Can you show me an exempla ? I just need it?
Re: Creating a Java Class Programmatically by means of Java Model [message #1100742 is a reply to message #1100708] Tue, 03 September 2013 14:35 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Any help?
Re: Creating a Java Class Programmatically by means of Java Model [message #1101255 is a reply to message #1100258] Wed, 04 September 2013 08:01 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Hello again.

My only problem is that I don't know how to create the Type, I can create the TypeAccess but not the Type class. For instance:

TypeAccess typeAcess = JavaFactory.eINSTANCE.createTypeAccess();

typeAcessFinal.setType(Type) //here is my problem, how to create a Type ?



Thanks in advance for your help.
Re: Creating a Java Class Programmatically by means of Java Model [message #1101265 is a reply to message #1100742] Wed, 04 September 2013 08:17 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
I tried to make the following:


TypeAccess typeAcessFinal = JavaFactory.eINSTANCE.createTypeAccess();

TypeParameter  testeParameter = JavaFactory.eINSTANCE.createTypeParameter();
				
testeParameter.setName("teste");

typeAcessFinal.setType(testeParameter);

FieldDeclaration attibute1 = JavaFactory.eINSTANCE.createFieldDeclaration();
				
attibute1.setModifier(modifierAtt);
attibute1.setName("valor");
attibute1.setType(typeAcessFinal);



After trying to salve in XML i I got the following error: The object 'org.eclipse.gmt.modisco.java.emf.impl.TypeParameterImpl@455fc40c (name: Valor, proxy: false)' is not contained in a resource.

Why? Thanks in advance.
Re: Creating a Java Class Programmatically by means of Java Model [message #1101280 is a reply to message #1101265] Wed, 04 September 2013 08:49 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo BruneliereFriend
Messages: 674
Registered: July 2009
Senior Member
Hello Rafael,

If you look to the structure of the Java metamodel (e.g. on the left panel when opening a Java model in the MoDisco Model Browser) and to the Java model itself, you will see that Type is an abstract concept.
ClassDeclaration, InterfaceDeclaration, EnumDeclaration or PrimitiveTypeXXX are types that you can actually instantiate and then set as the type of a FieldDeclaration.
Again, I really advice you to take a deep look to a discovered Java model in the MoDisco Model Browser to have a better idea of how it has to be structured.

Best regards,


--------------------------------------------------------
Hugo Bruneliere, PhD
NaoMod team (IMT Atlantique & LS2N-CNRS)
Nantes - France
--------------------------------------------------------
Re: Creating a Java Class Programmatically by means of Java Model [message #1101382 is a reply to message #1101280] Wed, 04 September 2013 11:40 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Hello Hugo,

I tried to do what you told me. I followed the Java Model as you said. Then I created an instance of PrimitiveTypeInt and set into a TypeAccess, see the source-code below. However, when I will save the JavaModel I always get the same error. Please me let know how to fix it, i.e., how to put the PrimitiveTypeInt into a resource.

THanks in advance.

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: The object 'org.eclipse.gmt.modisco.java.emf.impl.PrimitiveTypeIntImpl@53343ed0 (name: int, proxy: false)' is not contained in a resource.
	at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.endSave(XMLSaveImpl.java:301)


TypeAccess type = JavaFactory.eINSTANCE.createTypeAccess();

PrimitiveTypeInt inteiro = JavaFactory.eINSTANCE.createPrimitiveTypeInt();
		
inteiro.setName("int");
		
inteiro.getUsagesInTypeAccess().add(type);

inteiro.setProxy(false);

type.setType(inteiro);
Re: Creating a Java Class Programmatically by means of Java Model [message #1101524 is a reply to message #1101382] Wed, 04 September 2013 15:12 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo BruneliereFriend
Messages: 674
Registered: July 2009
Senior Member
Browsing the Java model, you can see that the PrimitiveTypeXXX elements are set in the orphanTypes reference of the Model element.
Generally, to see the actual container of a model element, you can check its computed /eContainer reference in the MoDisco Model Browser.

index.php/fa/16105/0/

Best regards,


--------------------------------------------------------
Hugo Bruneliere, PhD
NaoMod team (IMT Atlantique & LS2N-CNRS)
Nantes - France
--------------------------------------------------------
Re: Creating a Java Class Programmatically by means of Java Model [message #1101646 is a reply to message #1101524] Wed, 04 September 2013 19:13 Go to previous messageGo to next message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Hugo, thanks one more time for you patience and help. However, I made what you told me but I didn't have succeed.


...more code here

Model model = JavaFactory.eINSTANCE.createModel();

TypeAccess type = JavaFactory.eINSTANCE.createTypeAccess();

PrimitiveTypeInt inteiro = JavaFactory.eINSTANCE.createPrimitiveTypeInt();
		
inteiro.setName("int");
		
inteiro.getUsagesInTypeAccess().add(type);

inteiro.setProxy(false);

type.setType(inteiro);

model.getOrphanTypes().add(inteiro); \\ by doing this I through that it would work...but..didn't

System.out.println(" Verify container" + inteiro.eContainer()); \\ when I can the method eContainer() of the inteiro it shows org.eclipse.gmt.modisco.java.emf.impl.ModelImpl@18b1aebf. In other words, it is supposed to be correct..




After running this chunk of source-code I still get the same error "The object 'org.eclipse.gmt.modisco.java.emf.impl.PrimitiveTypeIntImpl@53343ed0 (name: int, proxy: false)'" and the model is not saved..

Do you know what can be now?
Re: Creating a Java Class Programmatically by means of Java Model [message #1101960 is a reply to message #1101646] Thu, 05 September 2013 06:41 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo BruneliereFriend
Messages: 674
Registered: July 2009
Senior Member
Hello,

You can find an EMF FAQ entry related to this issue.
If the PrimitiveTypeInt element is well contained in the Model element, this probably means that your Model element is not contained in an EMF resource as it should be: could you check this?

Best regards,


--------------------------------------------------------
Hugo Bruneliere, PhD
NaoMod team (IMT Atlantique & LS2N-CNRS)
Nantes - France
--------------------------------------------------------
Re: Creating a Java Class Programmatically by means of Java Model [message #1102942 is a reply to message #1101960] Fri, 06 September 2013 08:13 Go to previous message
Rafael Durelli is currently offline Rafael DurelliFriend
Messages: 72
Registered: September 2012
Member
Hello Hugo,


I checked and it was exactly what you suspected. The Model element was not contained in an EMF resource. Now it is correct.

Thank you for your support and patience.
Previous Topic:Changing KDM programatically
Next Topic:Modisco standalone
Goto Forum:
  


Current Time: Thu Mar 28 18:24:06 GMT 2024

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

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

Back to the top