Book a Demo

Author Topic: Code Templates: Making Changes  (Read 6586 times)

eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Code Templates: Making Changes
« on: February 04, 2004, 05:53:43 am »
I have tried repeatedly to change the code template that builds properties for C#.

I'm wondering if anyone else has had luck in making changes with the demo?

I've tried making blatant changes to a number of templates by adding things like "smelly" to see if I can get it to come through and it doesn't. I've changed the AttributeDeclaration to no avail.

I thought that changing the default AttributeDefinition template from this:
%if attStereotype == "property"%
{\n\t//read property\n\tget{;}\n\t//write property\n\tset{;}\n}
%endTemplate%

to this:
%if attStereotype == "property"%
\t{get{;}\n\t\tset{;}\n}
%endTemplate%

would do the trick. No change what so ever.
Any help would be greatly appreciated.

I want the code generation to yield this:

CODE >>>>>>>>
public string firstName
{
     get{return firstName;}
     set{firstName = value;}
}

Instead of this:

CODE >>>>>>>>
public string firstName {
                 get{
                       return firstName;
                 }
                 set{
                       firstName = value;
                 }
           }


Thank you
eAndy

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #1 on: February 04, 2004, 06:46:04 am »
There is 2 locations that getter/setters are set in the code templates, you are editing the attribute code templates and not the operation code templates.

You should find similar template code  under the operation templates :D
« Last Edit: February 04, 2004, 06:47:55 am by fluxtah »

eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #2 on: February 04, 2004, 07:08:48 am »
So you've successfully changed the property template?

Can you send me your code and exactly which template to modify?

I've tried changing all the operation code templates and I see NO changes in the properties generated.

I'm hitting save after the changes and I've even gotten out of the product and re-started. No Changes.

I'm going so far as to just put some word (like smelly) as a literal way up top that should ALWAYS show based on the docs but it doesn't.

eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #3 on: February 04, 2004, 07:30:04 am »
I exported the C# code and searched it for get and set and only find it in the Attribute Declaration.

I'm wondering if
a. code generation can't be changed in the demo
b. there is some internal macro that generates that block
c. there is some procuedre other than hitting save and generating the code that I must do to get the changed template to work.

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #4 on: February 04, 2004, 07:31:33 am »
Hiya,

Going into code templates, then clicking on template operation body then stereotype override property gives the following code:

Code: [Select]
$att=%opTag:"attribute_name"=="" ? "<unknown>" : value%
%if opTag:"writeonly" == ""%
$read="\tget{\n\t\treturn " + $att + ";\n\t}"
%endIf%
%if opTag:"readonly" == ""%
$write="\tset{\n\t\t" + $att + " = value;\n\t}"
%endIf%


I change the above to include \n before my get/set keywords to suite my coding style

Code: [Select]
$att=%opTag:"attribute_name"=="" ? "<unknown>" : value%
%if opTag:"writeonly" == ""%
$read="\n\tget{\n\t\treturn " + $att + ";\n\t}"
%endIf%
%if opTag:"readonly" == ""%
$write="\n\tset{\n\t\t" + $att + " = value;\n\t}"
%endIf%


I create a new class Class1 and then add an attribute, in this case i want a property so i call my attribute something like _TestAttribute or testAttribute at this point dont specify a stereotype, click on the checkbox titled 'property' you may need to save the attribute before you can do this.

This will create a property stereotyped operation for you allowing you to specify getter/setter, I called mine TestAttribute.

Here is the code output

Code: [Select]
///////////////////////////////////////////////////////////
//  Class1.cs
//  Implementation of the Class Class1
//  Generated by Enterprise Architect
//  Created on:      04-Feb-2004 15:26:59
///////////////////////////////////////////////////////////




namespace TestView {
     public class Class1 {

           private System.String testProperty;

           public Class1(){

           }

           public System.String TestProperty {

                 get
                 {
                       return testProperty;
                 }

                 set
                 {
                       testProperty = value;
                 }
           }

     }//end Class1
}//end namespace TestView



If you have already generated the source code, made a change to the template and then try forward engineering again EA will not modify existing code, try deleting the file you generated before you forward engineer.

eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #5 on: February 04, 2004, 08:40:33 am »
I can't thank you enough.

I completely missed that the stereotypes were subclasses of templates.

Very cool, although it took me a bit to figure out exactly what I wanted to do.

Its all working now.

Thank you, Thank you, Thank you

;D

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #6 on: February 04, 2004, 09:15:22 am »
Cool :D the stereotype overrides in the code template editor are really useful! :]




eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #7 on: February 04, 2004, 09:18:00 am »
Absolutely. Really allows to fine tune your code without having giant spaghetti structures to dig through.

Very nice.

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #8 on: February 04, 2004, 09:38:10 am »
yep! you can also use the tags to modify code output in the templates, this is really useful, tack on these tags to nearly any UML entity within EA and then modify the templates to customise the code output.

- Fluxtah


eAndy

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #9 on: February 04, 2004, 10:23:55 am »
Have you got a repro/sample that I could use to see it?

fluxtah

  • EA User
  • **
  • Posts: 144
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #10 on: February 04, 2004, 12:48:56 pm »
I haven't got anything like that so far, I am still a UA newbie only had it for about a month.

I realised that even though tagging is a great way to manipulate code output, there might be another way to do what I am trying to do.

You can create a new class then go to the tags tab on the class properties panel then type in a tag ie 'mytag' with a value of something like yes|no, 1 or 0.

Then you can open the code template editor and manipulate them, I copied the following line from a template that uses the tags:

Code: [Select]
%if classTag:"mytag" != ""%
foobar will be printed here
%endIf%


I think for operation and attributes templates instead of classTag, use opTag and attTag
« Last Edit: February 04, 2004, 12:51:21 pm by fluxtah »

sperry

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Code Templates: Making Changes
« Reply #11 on: February 26, 2004, 05:39:35 am »
Is there a complete reference, or White Paper, on modifying and using the Code Templates?

-SP

-SP