Book a Demo

Author Topic: Transformation:How to avoid getters for static att  (Read 5308 times)

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Transformation:How to avoid getters for static att
« on: March 27, 2010, 12:53:46 am »
Hi,

I just did a transformation from PIM to a PSM (Java). It created everything nicely except constants. I used a lot of public static const attributes (like THIS_IS_A_CONSTANT). After the transformation, these attributes are gone and instead there have been getters and setters created (like getTHIS-IS_A_CONSTANT) which is not what I want and it does not even make sense to create a setter for a const attribute.

How can I avoid that? In the manual it say I should look in the Java Specs in the options dialog. But there I can only seet the name of the prefix.

What can I do?

Thanks, Jan

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #1 on: March 29, 2010, 09:05:30 pm »
For anyone who's interested:

I managed to do it myself. I think AFAIK there is no checkbox in the options. I found the "problem" in the transformation templates. I edited the Java templates (Settings->Transformation templates).

The problem is, that in the class template, there is a check if its a Class. If not (as in my case an Interface) it calls the "Attribute__AsProperties" macro. This creates the getters and setters by callin the "Properties" macro.

I inserted a new condition in this template to check whether it's static const. If yes, it will only call the "Attribute" macro and end. see here (Only the biginning of the template):
Code: [Select]
$attName=$parameter1
$type=$parameter2
$propertyName=$attName
$propertyName=%REMOVE_PREFIX($propertyName,genOptPropertyPrefix)%
%if $attConst != 0 and $attStatic != 0%
%Attribute%
%endTemplate%
%endIf%
%if genOptGenCapitalisedProperties=="T"%
After doing this, I found a second thing that bothers me: Public constants are being transformed into private attributes. I changed the "Attribute" template as follows:
Code: [Select]
Attribute
{
  %TRANSFORM_REFERENCE()%
  %TRANSFORM_CURRENT("scope","type","stereotype")%
  %if $attConst != 0 and $attStatic != 0%
  scope=%qt%value%qt%
  %elseIf%
  scope=%qt%%attScope == "Public" ? "Private" : value%%qt%
  %endIf%
  type=%qt%%CONVERT_TYPE("Java",attType)%%qt%
%if classStereotype=="enumeration"%
  stereotype="enum"
%else%
  stereotype=%qt%%attStereotype%%qt%
%endIf%
}

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #2 on: March 30, 2010, 08:16:41 pm »
Actually, I dove a little deeping into the template sea. If found out, that if a check box is checked, the value is "T" otherwise its empty (so. I guess ""). thus, my comparsion !=0 was wrong and thus it was always true.

From my point of view, it should be
Code: [Select]
%if $attConst=="T" and $attStatic=="T"%instead.
But it doesn't work. The conditionseems to be always false even if I replace and by or. But the attributes of the class I transform are definitely static const!

So what's the problem here? What am I doing wrong? Please help!

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #3 on: March 30, 2010, 10:08:54 pm »
Next update:

My fault, it has to be no $ (which is the classifier for variables). So it should work like this:
Code: [Select]
%if attConst=="T" and attStatic=="T"%
For the general branch it works. In the "Properties2 template it sucessfully avoids creating getters and setters and jump directly to the "Attributes" template.

But when I transform one of my classes that includes public static const attributes, the result is a priviate attribute after the transformation.

Actually it is quite strange, because im currently not able to change the scope. the drop down box is disabled. However, it shows "Public" in the disabled box but a lock icon infront of the attribute in the attributes dialog and "-" character infront of the attribute in the diagram. If I create a new attribute, the scope drop down is also disabled and set to public there is no lock icon and the "+" is shown in the diagram view.

Very strange.

I don't see anything in the template that could cause this behaviour. the only things of which I am not sure what they are doing in detail are the TRANSFORM_REFERENCE() and TRANSFORM_CURRENT macros.

Can anyone help?

pocketom

  • EA User
  • **
  • Posts: 97
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #4 on: March 31, 2010, 12:24:48 am »
You can write the executed transformation script to a log file - for debugging. You can activate that in the Transform dialog.

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #5 on: March 31, 2010, 01:56:55 am »
Tom, great suggestion! :)

Using this output, I saw that all entries in the attribute section were actually created TWICE. One time Public, one time private. So, in the drow down it obviously displayed the first one, in the other view the second scope defintion.

Using this information, I found out that I had inserted one empty elseIf statement. This seems to be always true if empty (not so good idea actually), both branches were executed.

Now it works!!

ebeb

  • EA User
  • **
  • Posts: 169
  • Karma: +0/-0
    • View Profile
Re: Transformation:How to avoid getters for static
« Reply #6 on: March 31, 2010, 01:59:20 am »
If anybody is interested in the resulting templates, here they are (I don't know if it is the best way to do it and handle all other cases, but it works for me):

Propertires:
Code: [Select]
%if attConst=="T" and attStatic=="T"%
%Attribute%
%else%
$attName=$parameter1
$type=$parameter2
$propertyName=$attName
$propertyName=%REMOVE_PREFIX($propertyName,genOptPropertyPrefix)%
%if genOptGenCapitalisedProperties=="T"%
$propertyName=%CONVERT_NAME($propertyName, "camel case", "pascal case")%
%endIf%
Operation
{
  %TRANSFORM_REFERENCE()%
  name=%qt%%genOptJavaGetPrefix%$propertyName%qt%
  stereotype="property get"
  scope="Public"
  type=%qt%$type%qt%
  code=%qt%return $attName;%qt%
  Tag{name="attribute_name" value=%qt%$attName%qt%}
}

Operation
{
  %TRANSFORM_REFERENCE()%
  name=%qt%%genOptJavaSetPrefix%$propertyName%qt%
  stereotype="property set"
  scope="Public"
  type="void"
  Parameter
  {
    name="newVal"
    type=%qt%$type%qt%
  }
  code=%qt%$attName = newVal;%qt%
  Tag{name="attribute_name" value=%qt%$attName%qt%}
}
%endIf%

Attribute:
Code: [Select]
Attribute
{
  %TRANSFORM_REFERENCE()%
%if attConst=="T" and attStatic=="T"%
  %TRANSFORM_CURRENT("type","stereotype")%
%else%
  %TRANSFORM_CURRENT("scope","type","stereotype")%
  scope=%qt%%attScope == "Public" ? "Private" : value%%qt%
%endIf%
  type=%qt%%CONVERT_TYPE("Java",attType)%%qt%
%if classStereotype=="enumeration"%
  stereotype="enum"
%else%
  stereotype=%qt%%attStereotype%%qt%
%endIf%
}