Book a Demo

Author Topic: Code Generation: Get tags of realized interface  (Read 9440 times)

aljosha

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Code Generation: Get tags of realized interface
« on: October 30, 2021, 12:30:50 am »
Hi everyone,

I use Enterprise Architect for code generation and I would like to automatically retrieve all tags (in my case Java annotations) of the interfaces that a class realizes. Consider the following example:



From this model, I want to generate a class that looks like this:

Code: [Select]
@AnnotationOfMyInterface
public class MyClass {
    ...
}

So I want to add annotations as tags to MyInterface that should be applied to MyClass during code generation. In the UI, tags of implemented interfaces are shown so I was hoping there is a way to get these tags during code generation.

I tried to edit the code generation templates and found macros to get
  • All interfaces that a class implements: %list="ClassInterface" @separator=", "%
  • All tags with a given name (of the class that code is being generated for): %classTag:"annotations"%

But unfortunately, I cannot combine these macros, i.e., I cannot pass one interface to the classTag macro so that I can retrieve the tags of that particular interface (and not the one I'm generating code for). Is there a way to get classTags of a specific class / interface? I also tried to create a separate code generation template and "call" it from the main class code generation template. But inside my new template, the classTag macro still only gets the tags of the class.

I asked this question on StackOverflow (https://stackoverflow.com/questions/69737966/enterprise-architect-code-generation-get-tags-of-interface) but was referred here.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Generation: Get tags of realized interface
« Reply #1 on: November 01, 2021, 08:58:57 am »
I think you'll need to use EXEC_ADD_IN.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Code Generation: Get tags of realized interface
« Reply #2 on: November 01, 2021, 06:15:28 pm »
Well, there you are. As I told you: code generation is best be done by using the API rather than that crude macro language... (Of course the best option is still to not try to generate code at all.)

q.

aljosha

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Code Generation: Get tags of realized interface
« Reply #3 on: November 19, 2021, 08:44:43 pm »
Thank you @Eve for pointing me to EXEC_ADD_IN. I managed to setup a basic AddIn following this tutorial: https://bellekens.com/2011/01/29/tutorial-create-your-first-c-enterprise-architect-add-in-in-10-minutes/ and afterwards changed it to fit my needs. This is how I get the tagged values (annotations) of the interfaces a class realizes:

First step:
Inside the code generation template, I get all the interfaces of a class and pass them to my AddIn
Code: [Select]
$interfaces=%list="ClassInterface" @separator=", "%
%EXEC_ADD_IN("MyAddin","getInterfaceTags", $interfaces)%

Second step:
As documented at https://sparxsystems.com/enterprise_architect_user_guide/15.2/model_domains/functionmacros.html the repository objects gets passed along with the EXEC_ADD_IN call. I use the repository object and query for all interfaces using the names contained in $interfaces. I can then get the tagged values of each interface element. Simple prototype that achieves this for a single interface:
Code: [Select]
public Object getInterfaceTags(EA.Repository repo, Object args)
{
  String[] interfaceNames = args as String[];
  String firstInterfaceName = interfaceNames[0];
  EA.Element interfaceElement = repo.GetElementsByQuery("Simple", firstInterfaceName).GetAt(0);
  String tag = interfaceElement.TaggedValues.GetAt(0);
  return interfaceElement.Name + " has tag value" + tag.Value;
}

I know, there are a couple of shortcomings but this is just a simple proof of concept for an idea that will most likely never be production code. So @qwerty, I will follow your advice and keep my hands off of code generation in the future ;).