Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: h2oskier on January 29, 2007, 09:11:13 am
-
When generating code is it possible to group methods and attributes by their visibility? In other words put all of the public methods first, followed by the protected, and then the private. It would also be nice to group the constructors together.
Thanks,
Michael
-
Yes it is. Have a look at the class body template for C++ or Delphi.
%list="Operation" @separator="\n" @indent="\t" opName==className and opScope=="Public"%
%list="Operation" @separator="\n" @indent="\t" opName==className and opScope=="Protected"%
%list="Operation" @separator="\n" @indent="\t" opName==className and opScope=="Private"%
%list="Operation" @separator="\n" @indent="\t" opName!=className and opScope=="Public"%
%list="Operation" @separator="\n" @indent="\t" opName!=className and opScope=="Protected"%
%list="Operation" @separator="\n" @indent=\t" opName!=className and opScope=="Private"%
-
Thanks Simon. That works great. To extend it a little further I would like to group properties separate from other methods. What should I use to identify a property from a method?
Thanks,
Michael
-
I'm guessing here, but perhaps you could use stereotypes or tags - perhaps EA is doing this already, so look at an XMI rendering to see. You'd have to tweak the script a bit, but it might work.
-
Properties will be identified by stereotype as Midnight suggested. You'll need "property", "property set", "property get" or "property let" depending on the language you're using.
-
I found another way that is working fine. I am using the presence (or absence) of the attribute_name tag to identify if an operation is a property or a method as follows:
$COMMENT="output properties in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"!=null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"!=null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"!=null%
$COMMENT="output operations in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"==null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"==null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"==null%
For those who have been following the full thread. Here is the C# class body template that I am using to group constants, instance variables, constructors, properties and methods by visibility: I hope this is helpful.
%if elemType == "Interface"%
{\n
%list="Operation" @separator="\n\n" @indent="\t"%
%PI=""%
} // %className%
%endTemplate%
\n{\n
%PI="\n\n"%
%list="InnerClass" @separator="\n\n" @indent="\t"%
$COMMENT="output attribute region only if there are attributea"
$attribute=%list="Attribute"%
%if $attribute!=null%
#region Attributes
$COMMENT="output constants in public/protected/private order"
%list="Attribute" @separator="\n" @indent="\t" attScope=="Public" and attConst=="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Protected" and attConst=="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Private" and attConst=="T"%
$COMMENT="output variables in public/protected/private order"
%list="Attribute" @separator="\n" @indent="\t" attScope=="Public" and attConst!="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Protected" and attConst!="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Private" and attConst!="T"%
#endregion // Attributes
%endIf%
$COMMENT="output constructor region only if there are constructors"
$constructors=%list="Operation" opName==className%
%if $constructors!=null%
#region Constructors
$ops=""
%if genOptGenConstructor == "T" and classHasConstructor != "T"%
$ops+="\tpublic "+%className%+"()\n{\n\n\t}"
%endIf%
$COMMENT="output constructors in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Public"%
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Protected"%
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Private"%
#endregion // Constructors
%endIf%
%if genOptGenDestructor == "T" and classHasDestructor != "T" and classHasFinalizer != "T"%
$ops+="\n\n\t~"+%className%+"()\n{\n\n\t}"
%endIf%
%if genOptCSGenDispose == "T" and classHasDispose != "T"%
$ops+="\n\n\tpublic "
$ops+=%classHasParent=="T" ? "override " : "virtual "%
$ops+="void Dispose(){\n\n\t}"
%endIf%
%if genOptCSGenFinalizer == "T" and genOptGenDestructor != "T" and classHasFinalizer != "T" and classHasDestructor != "T"%
$ops+="\n\n\tprotected "
$ops+=%classHasParent=="T" ? "override " : "virtual "%
$ops+="void Finalize()\n{\n"
$ops+=%classHasParent=="T" ? "\t\tbase.Finalize();" : ""% + "\n\t}"
%endIf%
%if genOptGenCopyConstructor == "T" and classHasCopyConstructor != "T"%
$ops+="\n\n\tpublic "
$ops+=%className%+"("+%className%+" the"+%className%+")\n{\n\n\t}"
%endIf%
$ops
$COMMENT="output property region only if there are properties"
$propertys=%list="Operation" opName!=className and opScope=="Public" and opTag:"attribute_name"!=null%
%if $propertys!=null%
#region Properties
$COMMENT="output properties in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"!=null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"!=null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"!=null%
#endregion // Properties
%endIf%
$COMMENT="output methods region only if there are methods"
$methods=%list="Operation" opName!=className and opScope=="Public" and opTag:"attribute_name"==null%
%if $methods!=null%
#region Methods
$COMMENT="output operations in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"==null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"==null%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"==null%
#endregion // Methods
} // %className%
-
I just have to let you know.
There is no null keyword in the template language.
Your code just happens to work it's treating it like any other word it doesn't know about and using an empty string.
It's unlikely that anything will change to break your templates, but I just thought that it was worth mentioning. I'd normally compare with an empty string literal.
-
Thanks Simon. I appreciate you pointing this out. Below is the updated C# class body template with the null keyword replaced with an empty string.
%if elemType == "Interface"%
{\n
%list="Operation" @separator="\n\n" @indent="\t"%
%PI=""%
} // %className%
%endTemplate%
\n{\n
%PI="\n\n"%
%list="InnerClass" @separator="\n\n" @indent="\t"%
$COMMENT="output attribute region only if there are attributea"
$attribute=%list="Attribute"%
%if $attribute!=""%
#region Attributes
$COMMENT="output constants in public/protected/private order"
%list="Attribute" @separator="\n" @indent="\t" attScope=="Public" and attConst=="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Protected" and attConst=="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Private" and attConst=="T"%
$COMMENT="output variables in public/protected/private order"
%list="Attribute" @separator="\n" @indent="\t" attScope=="Public" and attConst!="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Protected" and attConst!="T"%
%list="Attribute" @separator="\n" @indent="\t" attScope=="Private" and attConst!="T"%
#endregion // Attributes
%endIf%
$COMMENT="output constructor region only if there are constructors"
$constructors=%list="Operation" opName==className%
%if $constructors!=""%
#region Constructors
$ops=""
%if genOptGenConstructor == "T" and classHasConstructor != "T"%
$ops+="\tpublic "+%className%+"()\n{\n\n\t}"
%endIf%
$COMMENT="output constructors in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Public"%
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Protected"%
%list="Operation" @separator="\n\n" @indent="\t" opName==className and opScope=="Private"%
#endregion // Constructors
%endIf%
%if genOptGenDestructor == "T" and classHasDestructor != "T" and classHasFinalizer != "T"%
$ops+="\n\n\t~"+%className%+"()\n{\n\n\t}"
%endIf%
%if genOptCSGenDispose == "T" and classHasDispose != "T"%
$ops+="\n\n\tpublic "
$ops+=%classHasParent=="T" ? "override " : "virtual "%
$ops+="void Dispose(){\n\n\t}"
%endIf%
%if genOptCSGenFinalizer == "T" and genOptGenDestructor != "T" and classHasFinalizer != "T" and classHasDestructor != "T"%
$ops+="\n\n\tprotected "
$ops+=%classHasParent=="T" ? "override " : "virtual "%
$ops+="void Finalize()\n{\n"
$ops+=%classHasParent=="T" ? "\t\tbase.Finalize();" : ""% + "\n\t}"
%endIf%
%if genOptGenCopyConstructor == "T" and classHasCopyConstructor != "T"%
$ops+="\n\n\tpublic "
$ops+=%className%+"("+%className%+" the"+%className%+")\n{\n\n\t}"
%endIf%
$ops
$COMMENT="output property region only if there are properties"
$propertys=%list="Operation" opName!=className and opTag:"attribute_name"!=""%
%if $propertys!=""%
#region Properties
$COMMENT="output properties in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"!=""%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"!=""%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"!=""%
#endregion // Properties
%endIf%
$COMMENT="output methods region only if there are methods"
$methods=%list="Operation" opName!=className and opTag:"attribute_name"==""%
%if $methods!=""%
#region Methods
$COMMENT="output operations in public/protected/private order"
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Public" and opTag:"attribute_name"==""%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Protected" and opTag:"attribute_name"==""%
%list="Operation" @separator="\n\n" @indent="\t" opName!=className and opScope=="Private" and opTag:"attribute_name"==""%
#endregion // Methods
%endIf%
} // %className%