Book a Demo

Author Topic: Programatically create getter and setter methods  (Read 6441 times)

Ross

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Programatically create getter and setter methods
« on: May 28, 2009, 02:31:20 am »
I've created the method below in java to programatically create the class getter and setter methods using the EA automation APIs. (See Method to Create the Getter and Setter section) Both the getter and setter methods are created successfully. However when I generate the code in EA for the resulting class (See Generated Code Output section) , the member variables/properties are not being retrieved. Rather, the value "unknown" is appearing in the list.  I suspect I need to indicate to EA that the methods are property getter/setter methods and are mapped to a particular variable. In the EA interface this is done by toggling the "Property" box for a given property/member variable. How do I set this value via the automation APIs? Is there some sort of element and/or attribute tagged value I must first create?

********* Method to Create the Getter and Setters****************
      public void addAttributeGetterSetters(int elementId,
String  attributeName,
String attributeDataType,
String attributeNotes){
            org.sparx.Element element;
            org.sparx.Method method;
            org.sparx.Parameter param;
            
            
            element = EaRepository.getEaRepository().getRepos().GetElementByID(elementId);
            
            //Create Getter
            method = element.GetMethods().AddNew("get" + attributeName, attributeDataType);
            method.SetStereotype("property get");
            method.SetVisibility("Public");
            method.SetNotes(attributeNotes);
            method.Update();

            
            //Create Setter
            method = element.GetMethods().AddNew("set" + attributeName, attributeDataType);
            method.SetStereotype("property set");
            method.SetVisibility("Public");
            method.SetNotes(attributeNotes);            
            method.Update();
            param = method.GetParameters().AddNew("newVal", attributeDataType);
            param.Update();            
            method.GetParameters().Refresh();
            
            element.GetMethods().Refresh();      
      }

***********Generated Code Output************************

public final class AccountingCode {

      private String accountingCode;


      public AccountingCode(){

      }

      public String getaccountingCode(){
            return <unknown>;
      }

      public void setaccountingCode(final String newVal){
            <unknown> = newVal;
      }

}

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Programatically create getter and setter metho
« Reply #1 on: May 28, 2009, 08:15:36 am »
You also need to add a tagged value "attribute_name".

almacivor

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Programatically create getter and setter metho
« Reply #2 on: June 10, 2009, 01:05:40 am »
Interesting, I tried this with version 7.5 and EA fell over in a heap. :o

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Programatically create getter and setter metho
« Reply #3 on: June 10, 2009, 08:49:35 am »
Please define "fell over in a heap".

Also, if you are using any edition above Corporate and are on build 844.  I would recommend upgrading the build 845.

almacivor

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Programatically create getter and setter metho
« Reply #4 on: June 10, 2009, 08:14:43 pm »
Sure
I'm trying to generate source code for the package using the right click option.
When EA is generating the code for a particular class it pops up a failure dialog box and closes.
The class i'm trying to generate has one attribute of type List<ClassName>, (which I've entered as a literal because I couldn't figure out how to get the collection attributes to generate it as a java list)
I've tried testing this with an attribute type of ClassName, and it seems to work fine.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Programatically create getter and setter metho
« Reply #5 on: June 11, 2009, 08:42:34 am »
So, what was the error?

almacivor

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Programatically create getter and setter metho
« Reply #6 on: June 11, 2009, 07:14:41 pm »
EA has encountered  problem and needs to close.
I'll email the xmi through.
Incidentally, what do I have to do to get the code gen to correctly generate attributes of type List<class>. i've never managed to work it out.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile