Skip to main content



      Home
Home » Modeling » MoDisco » How can I use ATL to modify a java model based on a third party lib
How can I use ATL to modify a java model based on a third party lib [message #789907] Fri, 03 February 2012 10:08 Go to next message
Eclipse UserFriend
Hello Community,

please allow me to show my question with an example:

before transformation:

public class Person {
}


after transformation:

import javax.persistence.Entity

@Entity
public class Person {
}


I have read the source code of the junit3 to junit4 transfromation example and understood how to create and add "@Test" annotation into the java model by using the MoDisco API.

My question is how to do with ATL?

Another example with ATL (http://www.eclipse.org/forums/index.php/m/727468/?srch=New+MoDisco+presentation+and+demo+available+online#msg_727468) only shows us how to modify the java model based on the given source, i.e. change class name, remove deprecated method, and create missing getter method.

With my example, can I "create" an "@Entity" Annotation and AnnotatoinTypeDeclaration by using ATL? The source model does not contain it...

best regards
Jing
Re: How can I use ATL to modify a java model based on a third party lib [message #789933 is a reply to message #789907] Fri, 03 February 2012 10:41 Go to previous messageGo to next message
Eclipse UserFriend
Hello Jing,

As you mentioned it, the provided demo shows a transformation creating new getter methods in a Java model.
What you want to do here is following exactly the same principle: creating new annotations (and corresponding type import) in a Java model.
It means that your transformation is going to be of the same kind, but of course the concerned model elements will be different.
To help a bit more, you can also find from https://bugs.eclipse.org/bugs/show_bug.cgi?id=369827 a contribution (to be committed soon) including a transformation similarly adding new annotations...

Best regards,

Hugo
Re: How can I use ATL to modify a java model based on a third party lib [message #791784 is a reply to message #789933] Mon, 06 February 2012 04:00 Go to previous messageGo to next message
Eclipse UserFriend
Hello Hugo,

thanks for your help and kindly reply Smile.

As I said, I knew how to create new Annotations in a java model by using MoDisco API. I want to know if it is also possible to do it ONLY using ATL. Your example of creating missing getter method shows us that we can, for example,

create method declaration based on the given source java model:

		omd : Java!MethodDeclaration (
			abstractTypeDeclaration <- ifd.abstractTypeDeclaration,
			originalCompilationUnit <- ifd.originalCompilationUnit,
			name <- let fieldName : String = ifd.fragments->first().name 
					in
						'get' + fieldName.substring(1,1).toUpper() + fieldName.substring(2,fieldName.size()),
			modifier <- om,
			body <- ob,
			returnType <- ota
		)


create a modifier:

		om : Java!Modifier (
			visibility <- #public	
		),


Do you mean that we can create any model element like declarations/annotations/imports(even if they are unknown for the source java model) in the target java model ONLY using ATL?


best regards
Jing

[Updated on: Mon, 06 February 2012 05:01] by Moderator

Re: How can I use ATL to modify a java model based on a third party lib [message #791854 is a reply to message #791784] Mon, 06 February 2012 05:34 Go to previous messageGo to next message
Eclipse UserFriend
Hi Jing,

Yes you can create any kind of model element in the target model, as long as the corresponding model element type is declared in the target metamodel of course.
Thus, you can create annotations using the same principle as for all types of elements in the Java metamodel.

This is how model-to-model transformation works. I encourage you to take a look to the ATL Documentation (and to the "Concepts" page) to have a clearer understanding of this.

Actually, the contribution bug I indicated in my previous message already shows how to create new elements in a Java model ONLY using ATL (there is no reference to or direct use of a specific MoDisco API there).

Best regards,

Hugo
Re: How can I use ATL to modify a java model based on a third party lib [message #791990 is a reply to message #791854] Mon, 06 February 2012 08:48 Go to previous messageGo to next message
Eclipse UserFriend
Hello Hugo,

thank you very much. Now I am beginning to understand ATL and the java model Smile.

To share my experience with the community, I post here my simple example:

before transformation:
public class Person {
}


after transformation:
@Deprecated
public class Person {
}


the goal is to add a new annotation element into the target java model. @Decprecated annotation is used in this example to keep it as simple as possible, i.e. no import declaration.

here is the ATL script:

rule addAnnotation { 
	from 
		ifd : Java!ClassDeclaration
	to
		ofd : Java!ClassDeclaration(
			annotations <- Sequence{ano}	
		),
		ano : Java!Annotation (
			originalCompilationUnit <- ifd.originalCompilationUnit,
			type <- tas
		),
		tas : Java!TypeAccess (
			type <- atd	
		), 
		atd : Java!AnnotationTypeDeclaration (
			name <- 'Deprecated',
			proxy <- true
		)
			
}


with this ATL all classes found in the project will be marked as "@Deprecated". The generated code looks great! Very Happy

This script has still a little problem. The transformed java model contains two annotation elements. One is correct and expected. The other one does not have the originalCompilationUnit und should not be created. Have no idea why it happens.

best regards
Jing
Re: How can I use ATL to modify a java model based on a third party lib [message #792109 is a reply to message #791990] Mon, 06 February 2012 11:22 Go to previous messageGo to next message
Eclipse UserFriend
Jing,

When you write this part of the rule
     ofd : Java!ClassDeclaration (
             annotations <- Sequence{ano}	
     ),

you don't indicate that the already existing annotations for this class declaration still have to be considered... so the corresponding "Annotation" model elements remain in the target model (as you are in a "refinement" transformation) but are not attached anymore to any class declaration.
You could rather do something like
     ofd : Java!ClassDeclaration (
             annotations <- ifd.annotations.append(ano)	
     ),


Best regards,

Hugo
Re: How can I use ATL to modify a java model based on a third party lib [message #795253 is a reply to message #792109] Fri, 10 February 2012 03:54 Go to previous message
Eclipse UserFriend
Hi Hugo,

got it, Thanks!

best regards!
Jing
Previous Topic:where can I learn ATL for handling java metamodel
Next Topic:Modisco API
Goto Forum:
  


Current Time: Sat May 17 17:56:39 EDT 2025

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

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

Back to the top