Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   package org.sdmlib.models.classes.logic;
   
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Collections;
   
   import org.sdmlib.CGUtil;
   import org.sdmlib.StrUtil;
  
  
  public class GenRole extends Generator<Role>
  {
     private int elistPos;
  
     private void insertCaseInGenericGet(Clazz clazzParser parserRole partnerRoleString rootDir)
     {
        if (clazz.isInterface())
        {
           return;
        }
        
        int pos = parser.indexOf(. + ":getValue(Object,String)");
  
        if (pos < 0)
        {
           // ups, did not find generic get method. 
           ..println("Warning: SDMLib codgen for role" + partnerRole.getName() + " in class " + clazz.getFullName() 
              + ": \nDid not find method get(String). Should have been generated by the clazz. " 
              + "\nCould not add required code fragment there. :( ");
  
           return;
        }
  
        // OK, found method, parse its body to find if that handles me. 
        int methodBodyStartPos = parser.getMethodBodyStartPos();
        
        pos = parser.methodBodyIndexOf(. + ":PROPERTY_" + partnerRole.getName().toUpperCase() , methodBodyStartPos);
  
        if (pos < 0)
        {         
           // need to add if block to generic get method
           parser.methodBodyIndexOf(.methodBodyStartPos);
           
           int lastIfEndPos = parser.lastIfEnd + 2; // add 1 to be after } and 1 to be after \n
           if (lastIfEndPos - 2  < 0)
           {
              lastIfEndPos = methodBodyStartPos + 1;
           }
           
           StringBuilder text = new StringBuilder
              (  "\n      if (ClassName.PROPERTY_NAME.equalsIgnoreCase(attribute))" +
                 "\n      {" +
                 "\n         return ((ClassName) target).getPropertyName();" +
                 "\n      }" +
                 "\n" 
                 );
  
           String partnerRoleNameUpFirst = StrUtil.upFirstChar(partnerRole.getName());
           
           CGUtil.replaceAll(text
              "ClassName", CGUtil.shortClassName(clazz.getName()),
              "PropertyName"partnerRoleNameUpFirst,
              "PROPERTY_NAME""PROPERTY_" + partnerRole.getName().toUpperCase()
              );
  
           parser.insert(lastIfEndPostext.toString());
        }
     }
  
     
     //   private void generateEmptySetInPartnerClass(String rootDir, Role partnerRole)
     //   {
     //      // generate EMPTY_SET in partner class
     //      Clazz partnerClass = partnerRole.getClazz();
     //      
     //      GenClass partnerClazz = getGenerator(partnerClass);
     //      Parser partnerParser = partnerClazz.getOrCreateParser(rootDir);
     //      
     //      int partnerPos = partnerParser.indexOf(Parser.ATTRIBUTE + ":EMPTY_SET");
     //      
     //      if (partnerPos < 0)
     //      {
     //         // add attribute declaration in class file
     //         partnerPos = partnerParser.indexOf(Parser.CLASS_END);
     //
     //         StringBuilder partnerText = new StringBuilder
     //            (  "\n   " +
     //               "\n   public static final type EMPTY_SET = new type()READONLY;" +
     //               "\n"
     //               );
    //
    //         String replaceReadOnly = ".withReadOnly(true)";
    //         if(!(partnerClass.hasFeature(Feature.ALBERTsSets))){
    //        	 replaceReadOnly = "";
    //         }
    //         
    //         CGUtil.replaceAll(partnerText, 
    //            "type", partnerClazz.getModelSetClassNameShort(),
    //            "READONLY", replaceReadOnly
    //            );
    //         
    //         partnerParser.insert(partnerPos, partnerText.toString());
    //         
    //         // insert import 
    ////         String ownerClassName = partnerClass.getFullName();
    ////         String packageName = CGUtil.packageName(ownerClassName);
    ////         String shortClassName = CGUtil.shortClassName(ownerClassName);
    //         getGenerator(partnerClass).insertImport(partnerClazz.getModelSetClassName());
    //         
    //         getGenerator(partnerRole.getClazz()).printFile();
    //
    //      }
    //      if (model.getPartnerRole().getCard().equals(Card.MANY.toString())){
    //         getGenerator(partnerRole.getClazz()).insertImport(getGenerator(partnerRole.getClazz()).getModelSetClassName());
    //      }
    //   }
 
    private void generateToManyRole(Parser myParserClazz clazzRole partnerRoleStringBuilder text)
    {
       String myClassName = CGUtil.shortClassName(.getClazz().getFullName());
       
       GenClass partnerClazz = getGenerator(partnerRole.getClazz());
       String partnerClassName = CGUtil.shortClassName(partnerRole.getClazz().getFullName());
       
       
       String partnerRoleName = partnerRole.getName();
       String partnerClassNameSet = partnerClazz.getModelSetClassNameShort();
       
       String partnerRoleUpFirstChar = StrUtil.upFirstChar(partnerRoleName);
       
       int pos = myParser.indexOf(. + ":PROPERTY_" + partnerRole.getName().toUpperCase());
 
       if (pos < 0)
       {
          text.append
          (  "\n   " +
                "\n   /********************************************************************" +
                "\n    * <pre>" +
                "\n    *              myCard                       partnerCard" +
                "\n    * myClassName ----------------------------------- partnerClassName" +
                "\n    *              myRoleName                   partnerRoleName" +
                "\n    * </pre>" +
                "\n    */" + 
                "\n   " +
                "\n   public static final String PROPERTY_PARTNER_ROLE_NAME = \"partnerRoleName\";" +
                "\n" );
       }
       
       if (! clazz.isInterface())
       {
          pos = myParser.indexOf(. + ":" + partnerRoleName);
 
          if (pos < 0)
          {
             text.append ("\n   private type partnerRoleName = null;" +
                          "\n   ");
          }
       }
       
       pos = myParser.indexOf(. + ":get" + partnerRoleUpFirstChar + "()");
 
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             text.append 
             (     "\n   public type getPartnerRoleName()" +
                   "\n   {" +
                   "\n      if (this.partnerRoleName == null)" +
                   "\n      {" +
                   "\n         return partnerClassNameSet.EMPTY_SET;" +
                   "\n      }" +
                   "\n   " +
                   "\n      return this.partnerRoleName;" +
                   "\n   }" +
                   "\n");
             
             if (!clazz.hasFeature(.
             		&& !clazz.hasFeature(.)
             		&& !clazz.hasFeature(.) ) {
             	CGUtil.replaceAll(text"partnerClassNameSet.EMPTY_SET""new type()");
             }
          }
          else
          {
             text.append
             (     "\n   public type getPartnerRoleName();" +
                   "\n");
          }
       }
       
       ArrayList<SymTabEntrysymTabEntries = myParser.getSymTabEntriesFor(. + ":get" + partnerRoleUpFirstChar + "()");
       
        = -1;
       
       if (symTabEntries.size() > 0)
       {
          String type = symTabEntries.get(0).getType();
           = type.indexOf(":EList<");
       }
       
       if ( >= 0)
       {
          pos = myParser.indexOf(. + ":get" + partnerRoleUpFirstChar + "Set()");
 
          if (pos < 0)
          {
             if (! clazz.isInterface())
             {
                text.append 
                (     "  public type getPartnerRoleNameSet()\n" + 
                      "  {\n" + 
                      "     return new type().with(getPartnerRoleName());\n" + 
                      "  }\n" + 
                      "\n");
             }
             else
             {
                text.append
                (     "\n   public type getPartnerRoleNameSet();" +
                      "\n");
             }
          }
       }
       
       
       if (.getClazz() == .getPartnerRole().getClazz())
       {
          // recursive assoc, add getTransitive methods
          
          pos = myParser.indexOf(. + ":get" + partnerRoleUpFirstChar + "Transitive()");
          
          if (pos < 0)
          {
             if (! clazz.isInterface())
             {
                text.append(
                   "   public partnerClassNameSet getPartnerRoleNameTransitive()\n" + 
                   "   {\n" + 
                   "      partnerClassNameSet result = new partnerClassNameSet().with(this);\n" + 
                   "      return result.getPartnerRoleNameTransitive();\n" + 
                   "   }\n" + 
                   "\n" 
                   );
             }
             else
             {
                text.append
                (     "\n   public partnerClassNameSet getPartnerRoleNameTransitive();" +
                      "\n");
             }
             getGenerator(clazz).insertImport(partnerClazz.getModelSetClassName());
          }
       }
       
       pos = myParser.indexOf(. + ":with" + partnerRoleUpFirstChar + "(" + partnerClassName  +  "...)");
       
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             String withMeth = 
                   "\n   public myClassName withPartnerRoleName(partnerClassName... value)" +
                   "\n   {" +
                   "\n      if(value==null){" + 
                   "\n         return this;" +
                   "\n      }" +
                   "\n      for (partnerClassName item : value)" +
                   "\n      {" +
                   "\n         if (item != null)" +
                   "\n         {" +
                   "\n            if (this.partnerRoleName == null)" +
                   "\n            {" +
                   "\n               this.partnerRoleName = initRoleVar;" +
                   "\n            }" +
                   "\n            " +
                   "\n            boolean changed = this.partnerRoleName.add (item);" +
                   "\n" +
                   "\n            if (changed)" +
                   "\n            {" +
                   "\n               item.withMyRoleName(this);" +
                   "\n               PROPERTYCHANGEADD" +
                   "\n            }" +
                   "\n         }" +
                   "\n      }" +
                   "\n      return this;" +
                   "\n   } " +
                   "\n";
             
             if (this..getName().equals(""))
             {
                // uni directional no reverse call
                withMeth = CGUtil.replaceAll(withMeth"\n               item.withMyRoleName(this);""");
             }
             
             text.append(withMeth);
            
          }
          else
          {
             text.append
             (     "\n   public myClassName withPartnerRoleName(partnerClassName... value);" +
                   "\n");
          }
          
          if ( >= 0)
          {
             CGUtil.replaceAll(text"initRoleVar""this.getPartnerRoleName()");
          }
       }
       
       
       pos = myParser.indexOf(. + ":without" + partnerRoleUpFirstChar + "(" + partnerClassName  +  "...)");
       
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             String withOutMeth = 
                   "\n   public myClassName withoutPartnerRoleName(partnerClassName... value)" +
                   "\n   {" +
                   "\n      for (partnerClassName item : value)" +
                   "\n      {" +
                   "\n         if ((this.partnerRoleName != null) && (item != null))" +
                   "\n         {" +
                   "\n            if (this.partnerRoleName.remove(item))" +
                   "\n            {" +
                   "\n               item.reverseWithoutCall(this);" +
                   "\n               PROPERTYCHANGEREMOVE" +
                   "\n            }" +
                   "\n         }" +
                   "\n      }" +
                   "\n      return this;" +
                   "\n   }" +
                   "\n";
             
             if (this..getName().equals(""))
             {
                // uni directional no reverse call
                withOutMeth = CGUtil.replaceAll(withOutMeth"\n               item.reverseWithoutCall(this);""");
             }
             
             text.append (withOutMeth);
          }
          else
          {
             text.append
             (     "\n   public myClassName withoutPartnerRoleName(partnerClassName... value);" +
                   "\n");
          }
       }
       
       pos = myParser.indexOf(. + ":create" + partnerRoleUpFirstChar + "()");
       
       //TODO UEBERPRUEFEN
       // System.out.println(partnerClassName +" ->" +genClazz.getName());
       String realPartnerClassName = partnerClassName;
       ClazzSet kidClasses = partnerRole.getClazz().getKidClazzes();
       ClazzSet kidClassesInterfaces =new ClazzSet();
       for(Clazz item : kidClasses){
          if(item.isInterface()){
             kidClassesInterfaces.add(item);
          }
       }
       if (partnerRole.getClazz().isInterface() && kidClassesInterfaces.size() == 1)
       {
          realPartnerClassName = CGUtil.shortClassName(kidClassesInterfaces.first().getFullName());
       }
       
       if (pos < 0 && ! partnerRole.getClazz().isInterface() && kidClassesInterfaces.size() != 1)
       {
          if (! clazz.isInterface())
          {
             text.append 
             (     "\n   public partnerClassName createPartnerRoleName()" +
                   "\n   {" +
                   "\n      partnerClassName value = new realPartnerClassName();" +
                   "\n      withPartnerRoleName(value);" +
                   "\n      return value;" +
                   "\n   } " +
                   "\n");
          }
          else
          {
             text.append
             (     "\n   public partnerClassName createPartnerRoleName();" +
                   "\n");
          }
       }
       
       GenClass generator = getGenerator(.getClazz());
       
       // if my partnerclass has subclasses generate createPartnerRoleNameSubClassName() methods
       kidClasses = partnerRole.getClazz().getKidClazzesTransitive().without(partnerRole.getClazz());
       
       for (Clazz kid : kidClasses)
       {
          String kidClassName = CGUtil.shortClassName(kid.getName());
          pos = myParser.indexOf(. + ":create" + partnerRoleUpFirstChar + kidClassName + "()");
          
          
          if (pos < 0 && ! kid.isInterface())
          {
             if (! clazz.isInterface())
             {
                text.append 
                (     "\n   public KidClassName createPartnerRoleNameSubClassName()" +
                      "\n   {" +
                      "\n      KidClassName value = new KidClassName();" +
                      "\n      withPartnerRoleName(value);" +
                      "\n      return value;" +
                      "\n   } " +
                      "\n");
             }
             else
             {
                text.append
                (     "\n   public KidClassName createPartnerRoleNameSubClassName();" +
                      "\n");
             }
          }
          
          CGUtil.replaceAll(text
             "KidClassName"kidClassName
             "PartnerRoleNameSubClassName"partnerRoleUpFirstChar + kidClassName
         	);
          
          if(generator!=null)
          {
             generator.insertImport(myParserkid.getFullName());
          }
       }
       
       String reverseWithoutCall = "set" + StrUtil.upFirstChar(.getName()) + "(null)";
       
       if (.getCard().equals(..toString()))
       {
          reverseWithoutCall = "without" + StrUtil.upFirstChar(.getName()) + "(this)";
       }
       
       String propertyChangeAdd = "";
       String propertyChangeRemove = "";
      	 propertyChangeAdd = "getPropertyChangeSupport().firePropertyChange(PROPERTY_PARTNER_ROLE_NAME, null, item);";
      	 propertyChangeRemove = "getPropertyChangeSupport().firePropertyChange(PROPERTY_PARTNER_ROLE_NAME, item, null);"
       }
       
       CGUtil.replaceAll(text"PROPERTYCHANGEADD"propertyChangeAdd"PROPERTYCHANGEREMOVE"propertyChangeRemove);
 
       CGUtil.replaceAll(text
          "myCard".getCard(),
          "partnerCard"partnerRole.getCard(),
          "type"partnerClassNameSet
          "initRoleVar""new " + partnerClassNameSet + "()"
          "myClassName"myClassName,
          "partnerClassName"partnerClassName,
          "realPartnerClassName"realPartnerClassName,
          "myRoleName".getName(),
          "MyRoleName", StrUtil.upFirstChar(.getName()),
          "partnerRoleName"partnerRoleName,
          "PARTNER_ROLE_NAME"partnerRoleName.toUpperCase(),
          "PartnerRoleName"partnerRoleUpFirstChar,
          "reverseWithoutCall(this)"reverseWithoutCall
          );
       if(generator!=null){
          generator.insertImport(myParsergetGenerator(partnerRole.getClazz()).getModelSetClassName());
       }
    } 
    
    private void generateToOneRole(Parser myParserClazz clazzRole partnerRoleStringBuilder text)
    {
       String myClassName = CGUtil.shortClassName(.getClazz().getFullName());
       
       String partnerClassName = CGUtil.shortClassName(partnerRole.getClazz().getFullName());
       
       String partnerRoleName = partnerRole.getName();
       
       String partnerRoleUpFirstChar = StrUtil.upFirstChar(partnerRoleName);
       
       int pos = myParser.indexOf(. + ":PROPERTY_" + partnerRole.getName().toUpperCase());
 
       if (pos < 0)
       {
          text.append
          (  "\n   " +
                "\n   /********************************************************************" +
                "\n    * <pre>" +
                "\n    *              myCard                       partnerCard" +
                "\n    * myClassName ----------------------------------- partnerClassName" +
                "\n    *              myRoleName                   partnerRoleName" +
                "\n    * </pre>" +
                "\n    */" + 
                "\n   " +
                "\n   public static final String PROPERTY_PARTNER_ROLE_NAME = \"partnerRoleName\";" +
                "\n" );
       }
       
       if (! clazz.isInterface())
       {
          pos = myParser.indexOf(. + ":" + partnerRoleName);
 
          if (pos < 0)
          {
             text.append ("\n   private partnerClassName partnerRoleName = null;" +
                          "\n");
          }
       }
       
       pos = myParser.indexOf(. + ":get" + partnerRoleUpFirstChar + "()");
 
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             text.append 
             (     "\n   public partnerClassName getPartnerRoleName()" +
                   "\n   {" +
                   "\n      return this.partnerRoleName;" +
                   "\n   }" +
                   "\n");
          }
          else
          {
             text.append
             (     "\n   public partnerClassName getPartnerRoleName();" +
                   "\n");
          }
       }
       
       
       if (.getClazz() == .getPartnerRole().getClazz())
       {
          // recursive assoc, add getTransitive methods
          
          pos = myParser.indexOf(. + ":get" + partnerRoleUpFirstChar + "Transitive()");
          
          if (pos < 0)
          {
             if (! clazz.isInterface())
             {
                text.append(
                   "   public partnerClassNameSet getPartnerRoleNameTransitive()\n" + 
                   "   {\n" + 
                   "      partnerClassNameSet result = new partnerClassNameSet().with(this);\n" + 
                   "      return result.getPartnerRoleNameTransitive();\n" + 
                   "   }\n" + 
                   "\n");
             }
             else
             {
                text.append
                (     "\n   public partnerClassName getPartnerRoleNameTransitive();" +
                      "\n");
             }
             getGenerator(clazz).insertImport(CGUtil.helperClassName(partnerRole.getClazz().getFullName(),"Set"));
          
          }
       }
       
       pos = myParser.indexOf(. + ":set" + partnerRoleUpFirstChar + "(" + partnerClassName  +  ")");
       
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             String setMeth = "\n   public boolean setPartnerRoleName(partnerClassName value)" +
                   "\n   {" +
                   "\n      boolean changed = false;" +
                   "\n      " +
                   "\n      if (this.partnerRoleName != value)" +
                   "\n      {" +
                   "\n         partnerClassName oldValue = this.partnerRoleName;" +
                   "\n         " +
                   "\n         if (this.partnerRoleName != null)" +
                   "\n         {" +
                   "\n            this.partnerRoleName = null;" +
                   "\n            oldValue.withoutMethodCall(this);" +
                   "\n         }" +
                   "\n         " +
                   "\n         this.partnerRoleName = value;" +
                   "\n         " +
                   "\n         if (value != null)" +
                   "\n         {" +
                   "\n            value.withMyRoleName(this);" +
                   "\n         }" +
                   "\n         " +
                   "\n         PROPERTYCHANGEADD" +
                   "\n         changed = true;" +
                   "\n      }" +
                   "\n      " +
                   "\n      return changed;" +
                   "\n   }" +
                   "\n";
 
             if (this..getName().equals(""))
             {
                // uni directional assoc, do not call reverse
                setMeth = CGUtil.replaceAll(setMeth
                   "\n         if (this.partnerRoleName != null)" +
                         "\n         {" +
                         "\n            this.partnerRoleName = null;" +
                         "\n            oldValue.withoutMethodCall(this);" +
                         "\n         }""",
                         "\n         if (value != null)" +
                               "\n         {" +
                               "\n            value.withMyRoleName(this);" +
                               "\n         }"""
                      );
             }
             text.append(setMeth);
          }
          else
          {
             text.append
             (     "\n   public boolean setPartnerRoleName(partnerClassName value);" +
                   "\n");
          }
       }
       
       
       pos = myParser.indexOf(. + ":with" + partnerRoleUpFirstChar + "(" + partnerClassName  +  ")");
       
       if (pos < 0)
       {
          if (! clazz.isInterface())
          {
             text.append 
             (     "\n   public myClassName withPartnerRoleName(partnerClassName value)" +
                   "\n   {" +
                   "\n      setPartnerRoleName(value);" +
                   "\n      return this;" +
                   "\n   } " +
                   "\n");
          }
          else
          {
             text.append
             (     "\n   public myClassName withPartnerRoleName(partnerClassName value);" +
                   "\n");
          }
       }
       
 
       pos = myParser.indexOf(. + ":create" + partnerRoleUpFirstChar + "()");
       
       String realPartnerClassName = partnerClassName;
       
       ClazzSet kidClasses = partnerRole.getClazz().getKidClazzesTransitive().without(partnerRole.getClazz());
       ClazzSet kidClassesInterfaces =new ClazzSet();
       for(Clazz item : kidClasses){
          if(item.isWithNoObjects()){
             kidClassesInterfaces.add(item);
          }
       }
       
       if (partnerRole.getClazz().isWithNoObjects() && kidClassesInterfaces.size() == 1)
       {
          realPartnerClassName = CGUtil.shortClassName(kidClassesInterfaces.first().getFullName());
       }
       
       if (pos < 0 && ! (partnerRole.getClazz().isWithNoObjects() && kidClassesInterfaces.size() != 1))
       {
          if (! clazz.isWithNoObjects())
          {
             text.append 
             (     "\n   public partnerClassName createPartnerRoleName()" +
                   "\n   {" +
                   "\n      partnerClassName value = new realPartnerClassName();" +
                   "\n      withPartnerRoleName(value);" +
                   "\n      return value;" +
                   "\n   } " +
                   "\n");
          }
          else
          {
             text.append
             (     "\n   public partnerClassName createPartnerRoleName();" +
                   "\n");
          }
       }
       
       
 
       String reverseWithoutCall = "set" + StrUtil.upFirstChar(.getName()) + "(null)";
       
       if (.getCard().equals(..toString()))
       {
          reverseWithoutCall = "without" + StrUtil.upFirstChar(.getName()) + "(this)";
       }
       
       String propertyChangeAdd = "";
      	 propertyChangeAdd = "getPropertyChangeSupport().firePropertyChange(PROPERTY_PARTNER_ROLE_NAME, oldValue, value);";
       }
       CGUtil.replaceAll(text"PROPERTYCHANGEADD"propertyChangeAdd);
       CGUtil.replaceAll(text
          "myCard".getCard(),
          "partnerCard"partnerRole.getCard(),
          "myClassName"myClassName,
          "partnerClassName"partnerClassName,
          "realPartnerClassName"realPartnerClassName,
          "myRoleName".getName(),
          "MyRoleName", StrUtil.upFirstChar(.getName()),
          "partnerRoleName"partnerRoleName,
          
          "PARTNER_ROLE_NAME"partnerRoleName.toUpperCase(),
          "PartnerRoleName"partnerRoleUpFirstChar,
          "withoutMethodCall(this)"reverseWithoutCall
          );
       
       GenClass generator = getGenerator(.getClazz());
       if (.getPartnerRole().getCard().equals(..toString())){
          if(generator!=null ){
             generator.insertImport(myParsergetGenerator(partnerRole.getClazz()).getModelSetClassName());
          }
       }
    } 
    public void generate(String rootDirString helperDirRole partnerRole)
    {
       generate(.getClazz(), rootDirhelperDirpartnerRolefalse);
    }
    
    
    public void generate(Clazz clazzString rootDirString helperDirRole partnerRoleboolean fromSuperClass)
    {
       if (clazz.isExternal())
       {
          return;
       }
       
       Parser myParser = getGenerator(clazz).getOrCreateParser(rootDir);
       
       if ( ! fromSuperClass)
       {
             // add attribute declaration in class file
             StringBuilder text = new StringBuilder();
 
             if (StrUtil.stringEquals(partnerRole.getCard(), ..toString()))
             {
                generateToManyRole(myParserclazzpartnerRoletext);
 //               getGenerator(clazz).insertImport(LinkedHashSet.class.getName());
             }
             else
             {
                generateToOneRole(myParserclazzpartnerRoletext);
             }
 
             int pos = myParser.indexOf(.);
             try{
             	myParser.insert(postext.toString());
             }catch(Exception e){
             	..println("FILE: "+myParser.getFileName());
             	..println("FILEBODY: " +myParser.getFileBody());
             	throw e;
             }
 
 //         if (StrUtil.stringEquals(partnerRole.getCard(), Card.MANY.toString()))
 //         {
 //            generateEmptySetInPartnerClass(rootDir, partnerRole);
 //         }
       }
       
       //import partner role class if package name has changed
       if(!StrUtil.stringEquals(clazz.getFullName().substring(0, clazz.getFullName().lastIndexOf(".")), partnerRole.getClazz().getFullName().substring(0, partnerRole.getClazz().getFullName().lastIndexOf(".")))){
          getGenerator(clazz).insertImport(partnerRole.getClazz().getFullName());
       }
       
       if(!clazz.hasFeature(.)) {
     	  insertRemovalInRemoveYou(clazzmyParserpartnerRole);
     	  getGenerator(clazz).printFile();
     	  return;
       }
       
       Parser creatorParser = getGenerator(clazz).getOrCreateParserForCreatorClass(helperDir);
       
       insertCaseInGenericGet(clazzcreatorParserpartnerRolerootDir);
 
       if (StrUtil.stringEquals(partnerRole.getCard(), ..toString()))
       {
          insertCaseInGenericSetToMany(clazzcreatorParserpartnerRolerootDir);
       }
       else
       {
          insertCaseInGenericSetToOne(clazzcreatorParserpartnerRolerootDir);
       }
       
       insertRemovalInRemoveYou(clazzmyParserpartnerRole);
       
       getGenerator(clazz).printFile();
       
       
       // generate property in creator class
       if (!clazz.isInterface() && partnerRole.getClazz().hasFeature(.))
       {
          insertPropertyInCreatorClass(clazzcreatorParserpartnerRole);
 
          getGenerator(clazz).printFile(creatorParser);
       }
       
       if (partnerRole.getClazz().hasFeature(.)) {
 	      // generate property in model set class
 	      Parser modelSetParser = getGenerator(clazz).getOrCreateParserForModelSetFile(helperDir);
 	      
 		  insertGetterInModelSetFile(clazzmodelSetParsermyParserpartnerRole);
 		  insertSetterInModelSetFile(clazzmodelSetParserpartnerRole);
 	      
 	      getGenerator(clazz).printFile(modelSetParser);
 	
 	      // generate property in pattern object class
 	      Parser patternObjectParser = getGenerator(clazz).getOrCreateParserForPatternObjectFile(helperDir);
 	      
 	      insertGetterInPatternObjectFile(clazzpatternObjectParserpartnerRole);
 	      
 	      getGenerator(clazz).printFile(patternObjectParser);
 	      }
       }
    }
    
    private void insertRemovalInRemoveYou(Clazz clazzParser parserRole partnerRole)
    {
       if (clazz.isInterface())
       {
          return;
       }
       
       int pos = parser.indexOf(. + ":removeYou()");
 
       if (pos < 0)
       {
          // ups, did not find generic set method. 
          ..println("Warning: SDMLib codgen for role " + partnerRole.getName() + " for class " + clazz.getFullName() 
             + ": \nDid not find method removeYou(). Should have been generated by my clazz. " 
             + "\nCould not add required code fragment there. :( ");
 
          return;
       }
 
       // OK, found method, parse its body to find if that handles me. 
       String removeCall = "set" + StrUtil.upFirstChar(partnerRole.getName());
       String fullRemoveCall = removeCall + "(null);\n      ";
       if (partnerRole.getCard().equals(..toString()))
       {
          String name = StrUtil.upFirstChar(partnerRole.getName());
          String clazzName = StrUtil.upFirstChar(partnerRole.getClazz().getName());
          clazzName = CGUtil.shortClassName(clazzName);
          removeCall = "without"+name;
          fullRemoveCall = removeCall + "(this.get"+name+"().toArray(new "+clazzName+"[this.get"+name+"().size()]));\n      ";
       }            
       
       int methodBodyStartPos = parser.getMethodBodyStartPos();
       
       pos = parser.methodBodyIndexOf(. + ":" + removeCallmethodBodyStartPos);
 
       if (pos < 0)
       {         
          // need to add remove call
          pos = parser.methodBodyIndexOf(. + ":getPropertyChangeSupport"methodBodyStartPos);
          
          if (pos < 0)
          {
             ..println("Warning: SDMLib codgen for role " + partnerRole.getName() + " for class " + clazz.getFullName() 
                + ": \nDid not find getPropertyChangeSupport call in method removeYou(). Should have been generated by my clazz. " 
                + "\nCould not add required code fragment there. :( ");
 
             return;
          }
          
          parser.insert(posfullRemoveCall);
       }
    }
 
    private void insertGetterInModelSetFile(Clazz tgtClassParser parserParser modelClassParserRole partnerRole)
    {
       String key = . + ":get" + StrUtil.upFirstChar(partnerRole.getName()) + "()";
       int pos = parser.indexOf(key);
 
       StringBuilder text = new StringBuilder();
       
       if (pos < 0)
       {
             text.append(
             "   public ModelSetType getName()\n" + 
             "   {\n" + 
             "      ModelSetType result = new ModelSetType();\n" + 
             "      \n" + 
             "      for (ContentType obj : this)\n" + 
             "      {\n" + 
             "         result.addOneOrMore(obj.getName());\n" + 
             "      }\n" + 
             "      \n" + 
             "      return result;\n" + 
             "   }\n" + 
             "\n" + 
             "   public ContentTypeSet hasName(Object value)\n" + 
             "   {\n" + 
             "      ObjectSet neighbors = new ObjectSet();\n" + 
             "\n" + 
             "      if (value instanceof Collection)\n" + 
             "      {\n" + 
             "         neighbors.addAll((Collection<?>) value);\n" + 
             "      }\n" + 
             "      else\n" + 
             "      {\n" + 
             "         neighbors.add(value);\n" + 
             "      }\n" + 
             "      \n" + 
             "      ContentTypeSet answer = new ContentTypeSet();\n" + 
             "      \n" + 
             "      for (ContentType obj : this)\n" + 
             "      {\n" + 
             "         if (containsClause)\n" + 
             "         {\n" + 
             "            answer.add(obj);\n" + 
             "         }\n" + 
             "      }\n" + 
             "      \n" + 
             "      return answer;\n" + 
             "   }\n" + 
             "\n");
             getGenerator(tgtClass).insertImport(parserCollection.class.getName());
             getGenerator(tgtClass).insertImport(parserObjectSet.class.getName());
             
             String containsClause = "neighbors.contains(obj.get"
                   + StrUtil.upFirstChar(partnerRole.getName()) + "())";
             
             if (partnerRole.getCard().equals(..toString()))
             {
                containsClause = " ! Collections.disjoint(neighbors, obj.get" 
                      + StrUtil.upFirstChar(partnerRole.getName()) + "())";
                getGenerator(tgtClass).insertImport(parserCollections.class.getName());
             }
             CGUtil.replaceAll(text"containsClause"containsClause);
             
       }
        
       
       String key2 = . + ":get" + StrUtil.upFirstChar(partnerRole.getName()) + "Transitive()";
       int pos2 = parser.indexOf(key2);
       
       if (pos2 < 0)
       {
          if (.getClazz() == .getPartnerRole().getClazz())
          {
             text
                .append("\n   public ModelSetType getNameTransitive()\n"
                   + "   {\n"
                   + "      ModelSetType todo = new ModelSetType().with(this);\n"
                   + "      \n"
                   + "      ModelSetType result = new ModelSetType();\n"
                   + "      \n" + "      while ( ! todo.isEmpty())\n"
                   + "      {\n"
                   + "         ModelType current = todo.first();\n"
                   + "         \n" + "         todo.remove(current);\n"
                   + "         \n"
                   + "         if ( ! result.contains(current))\n"
                   + "         {\n" + "            result.add(current);\n"
                   + "            \n"
                   + "            todo.with(current.getPartnerrolenameupfirst().minus(result));\n"
                   + "         }\n" + "      }\n" + "      \n"
                   + "      return result;\n" + "   }\n" + "\n" + "");
             
             if (partnerRole.getCard().equals(..toString()))
             {
                CGUtil.replaceAll(text
                   "todo.with(current.getPartnerrolenameupfirst().minus(result));"
                   "if ( ! result.contains(current.getName()))\n"
                   + "            {\n"
                   + "               todo.with(current.getName());\n"
                   + "            }");
             }
             getGenerator(.getClazz()).insertImport(CGUtil.helperClassName(partnerRole.getClazz().getFullName() ,"Set"));
          }
       }
       
       if (pos < 0 || pos2 < 0)
       {      
          String add = "add";
          if (partnerRole.getCard().equalsIgnoreCase(..name()))
          {
             add = "addAll";
          }
          
          String partnerRoleNameUpFirst = StrUtil.upFirstChar(partnerRole.getName());
          String partnerGetterName = partnerRoleNameUpFirst;
          
          modelClassParser.indexOf(key);
          ArrayList<SymTabEntrysymTabEntries = modelClassParser.getSymTabEntriesFor(. + ":get" + partnerRoleNameUpFirst + "()");
          
           = -1;
         
         if (symTabEntries.size() > 0)
         {
            String type = symTabEntries.get(0).getType();
             = type.indexOf(":EList<");
         }
         
         if ( >= 0)
         {
            partnerGetterName += "Set";
         }
         
         CGUtil.replaceAll(text
            "ContentType", CGUtil.shortClassName(tgtClass.getFullName()),
            "ModelType", CGUtil.shortClassName(partnerRole.getClazz().getFullName()),
            "ModelSetType", CGUtil.shortClassName(partnerRole.getClazz().getFullName()) + "Set",
            "Name"partnerRoleNameUpFirst,
            "addOneOrMore"add,
            "Partnerrolenameupfirst"partnerGetterName
            );
         int classEnd = parser.indexOf(.);
         
         parser.insert(classEndtext.toString());
         
         if (! partnerRole.getClazz().isExternal())
         {
            // external classes get a set in this util package, no need for an import
            // thus just for real classes that may be in other packages
            String helperClassName = CGUtil.helperClassName(partnerRole.getClazz().getFullName(),"Set");
            
            getGenerator(tgtClass).insertImport(parserhelperClassName);
         }
      }
   }
   private void insertGetterInPatternObjectFile(Clazz clazzParser parserRole partnerRole)
   {
      insertHasNoParamInPatternObjectFile(clazzparserpartnerRole);
      insertCreateNoParamInPatternObjectFile(clazzparserpartnerRole);
      insertHasWithParamInPatternObjectFile(clazzparserpartnerRole);
      insertCreateWithParamInPatternObjectFile(clazzparserpartnerRole);
      insertGetInPatternObjectFile(clazzparserpartnerRole);
   }
   private void insertGetInPatternObjectFile(Clazz clazzParser parserRole partnerRole)
   {
      String key = . + ":get" + StrUtil.upFirstChar(partnerRole.getName()) + "()";
      int pos = parser.indexOf(key);
      if (pos < 0)
      {
         StringBuilder text = new StringBuilder();
         
         if ( < 0 || partnerRole.getCard().equals(..toString()))
         {
            text.append
            (       "   public TargetType getRoleName()\n"
                  + "   {\n"
                  + "      if (this.getPattern().getHasMatch())\n"
                  + "      {\n"
                  + "         return ((ModelClass) this.getCurrentMatch()).getRoleName();\n"
                  + "      }\n" + "      return null;\n" + "   }\n\n");
         }
         else
         {
            text.append
            (       "   public TargetType getRoleName()\n"
                  + "   {\n"
                  + "      if (this.getPattern().getHasMatch())\n"
                  + "      {\n"
                  + "         return ((ModelClass) this.getCurrentMatch()).getRoleNameSet();\n"
                  + "      }\n" + "      return null;\n" + "   }\n\n");
         }
         
         
//         getGenerator(clazz).insertImport(parser, PatternLink.class.getName());
         String targetType;
         
         if (partnerRole.getCard().equals(..toString()))
         {
            String fullTargetType = CGUtil.helperClassName(partnerRole.getClazz().getFullName(), "Set");
            if (partnerRole.getClazz().isExternal())
            {
               targetType = CGUtil.shortClassName(partnerRole.getClazz().getName()) + "Set";
            }
            else
            {
               targetType = getGenerator(partnerRole.getClazz()).shortNameAndImport(fullTargetTypeparser);
            }
         }else{
            targetType = getGenerator(partnerRole.getClazz()).shortNameAndImport(partnerRole.getClazz().getFullName(), parser);
         }
         
         CGUtil.replaceAll(text
            "TargetType"targetType,
            "ModelClass"getGenerator(.getClazz()).shortNameAndImport(.getClazz().getFullName(), parser),
            "RoleName", StrUtil.upFirstChar(partnerRole.getName()), 
            "PROPERTY_NAME""PROPERTY_" + partnerRole.getName().toUpperCase());
         int classEnd = parser.indexOf(.);
         
         parser.insert(classEndtext.toString());
      }
   }
   private void insertHasNoParamInPatternObjectFile(Clazz clazzParser parser,
         Role partnerRole)
   {
      String key = . + ":has" + StrUtil.upFirstChar(partnerRole.getName()) + "()";
      int pos = parser.indexOf(key);
      if (pos < 0)
      {
         StringBuilder text = new StringBuilder(
            "   public PatternObjectType hasName()\n" + 
            "   {\n" + 
            "      PatternObjectType result = new PatternObjectType(new ClassObjectType[]{});\n" + 
            "      \n" + 
            "      result.setModifier(this.getPattern().getModifier());\n" + 
            "      super.hasLink(ModelClass.PROPERTY_NAME, result);\n" + 
            "      \n" +