Book a Demo

Author Topic: Java collections and generics in associations  (Read 5792 times)

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Java collections and generics in associations
« on: March 01, 2008, 02:04:30 am »
Is it possible and, if yes, how to make EA generate code using Java Generics and Collections API?

Simple example is just to have two classes with association in between navigable and bidirectional, modeling many-to-many relationship.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Java collections and generics in associations
« Reply #1 on: March 03, 2008, 08:30:41 am »

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Java collections and generics in associations
« Reply #2 on: March 04, 2008, 12:55:07 am »
Thank you, it works, but...

User and Role classes are drawn on single diagram, and on that diagram I've first configured in its Details tab, Collection Classes, Default Collection Class to be List<#TYPE#> and generated code. All associations that had multiplicity 0..* or 1..* were well generated.

Then I cleared that setting from User class, deleted previously generated Java file, and set in Tools-->Options-->Source Code Engineering-->Java-->Collection Classes, Default Collection Class to be java.util.List<#TYPE#>. After that I've generated code and to my surprise, associations between User and Role were generated as List<#TYPE#> and other association between User and some other classes drawn on different diagram were generated as java.util.List<#TYPE#> (where #TYPE# was replaced as expected). Problem is I can not make it uniform anymore, regardless of the global or User class setting for Default Collection Class is, it always generates List<#TYPE#>. Is this a bug or am I doing something wrong?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Java collections and generics in associations
« Reply #3 on: March 04, 2008, 01:33:44 am »
This is only a guess, but do you have any package designated as a namespace root? Perhaps your current namespace settings (either way) are confusing EA on some branches of your model.
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Java collections and generics in associations
« Reply #4 on: March 04, 2008, 06:40:40 am »
My guess is that some of your classes are holding on to previous values of the option.  See http://www.sparxsystems.com.au/EAUserGuide/index.html?resetoptions.htm for how to correct this.

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Java collections and generics in associations
« Reply #5 on: March 04, 2008, 08:11:55 pm »
Midnight, Have to admit, I'm not sure what you're talking about. I didn't do anything like that, at least not on purpose. There is a root package, and problematic class, User, is first painted in one sub-package and then dragged from Project Browser tree to the other sub-package to mark another association that class has. In its primary package, User class had its Default Collection Class set to one value, then its source was generated, then Default Collection Class was erased for that class, and finally Default Collection Class is set globally for Java language to a slightly different value (full class path was specified). Regardless of made changes, generating source for that class would result in x-to-Many associations being mapped using second value, except single association drawn between User class and some other class, on User class primary diagram/package. No, problematic association doesn't have Member Type set for neither of the two roles.

I agree with you Simon, something is holding that old value, but why, where, and how to clean/reset it I do not know. Right-clicking root package then selecting Code Engineering-->Reset Options for this Package, then choosing either Java or All as Where, and Java as To, and checking Reset Default Options and checking Process Child Packages - all this didn't help. I've tried deleting association, saving, restarting EA, adding association back - no luck. Same with deleting class itself - no wonder why setting Default Collection Class for that specific class didn't work as well.

All this is rather annoying. I don't understand rationale behind not having things like these having default values for specific languages. E.g. in Java, a java.util.List is most commonly used collection class, and Java 1.5+ is version of choice in most if not all new Java applications. Setting default values would make it easier for all users - ones that will use it as is, and also ones that will want to change it (it is easier to change something than to reinvent the wheel especially if that involves learning something like EA's only expressions like #TYPE#)

Thank you all for your assistance!

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Java collections and generics in associations
« Reply #6 on: March 04, 2008, 09:49:29 pm »
Another issue, which in my opinion is a bug, is manifested in way that when a class attribute, previously checked to be Property and having getter and setter generated, has its type changed, this change doesn't propagate to corresponding getter and setter. Workaround I had to come up with is to first uncheck Property checkbox for attribute whose type has to be changed (getter and setter are deleted), change attribute type, and then check Property again. (Sigh) Yet another annoying EA "feature".

Back to the associations (but related to the accessor methods), class attributes that class gets by (navigable) association is not in list of class attributes and I don't see easy way to mark it additionally as a Property (being navigable in certain direction should be enough). Is there any way to make EA generate getter and setter methods for such attributes? Not to mention that other tools like Sybase PowerDesigner not only offer this, SYB PD can also generate additional handy methods for accessing attributes that are collections (getIterator, removeAll, addAll, ...). Guess this can be done in Code Generation Templates, and as much as I agree these additional methods are advanced and not always needed (so customizing generation templates is reasonable), getters and setters are basic and common, I expect things like these from the start. Where is the best place to start learning this language for customizing code generation templates in EA?

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Java collections and generics in associations
« Reply #7 on: March 05, 2008, 12:48:39 am »
I've found in EAP file a line that has

Code: [Select]
defaultCollectionClassList<#TYPE#>{E75F2822-0F50-4882-89F1-4DEE4623F024}Q++

so definitely old and erased Default Collection Class setting is still present in the project.

sslavic

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Java collections and generics in associations
« Reply #8 on: March 05, 2008, 11:16:07 am »
Another issue/bug found: attribute gained through association will persist even tho either of the classes, which formed association, gets deleted from the diagram where that association was painted. Both classes will remain in Project Browser, they can possibly be painted elsewhere on some other diagrams, but point is that when code is generated, association attribute will still be there, even tho there is no more that association between the classes.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Java collections and generics in associations
« Reply #9 on: March 05, 2008, 02:38:48 pm »
Quote
Another issue/bug found: attribute gained through association will persist even tho either of the classes, which formed association, gets deleted from the diagram where that association was painted. Both classes will remain in Project Browser, they can possibly be painted elsewhere on some other diagrams, but point is that when code is generated, association attribute will still be there, even tho there is no more that association between the classes.
This is the correct behavior.  Removing classes off a diagram does not remove the connector.  It still exists between the classes even if it is not shown on any diagram.  (To see this, try adding the classes back onto another diagram.)

Quote
Back to the associations (but related to the accessor methods), class attributes that class gets by (navigable) association is not in list of class attributes and I don't see easy way to mark it additionally as a Property (being navigable in certain direction should be enough). Is there any way to make EA generate getter and setter methods for such attributes?
No, EA won't do it except as part of a transformation.  See the default C# and Java transformations as an example.

Quote
Where is the best place to start learning this language for customizing code generation templates in EA?
The help file.  Start at http://www.sparxsystems.com.au/EAUserGuide/index.html?codetemplatesyntax.htm
« Last Edit: March 05, 2008, 02:42:59 pm by simonm »