1 20 package org.objectweb.modfact.jmi.impl.tayloredBased; 21 22 import org.objectweb.modfact.jmi.helper.*; 23 import org.objectweb.modfact.jmi.logging.ModFactLogger; 24 25 26 import javax.jmi.model.*; 27 28 import org.objectweb.modfact.jmi.generator.PrintGenerator; 29 30 31 35 public class AssociationImplementationGenerator extends PrintGenerator { 36 Association[] input; 37 38 ModFactLogger logger; 39 40 43 public void setInput(ModelElement[] elt) { 44 input = new Association[elt.length]; 45 for (int i = 0; i < input.length; i++) { 46 input[i] = (Association) elt[i]; 47 } 48 } 49 50 53 public void setLogger(ModFactLogger log) { 54 logger = log; 55 } 56 57 58 Association association; 59 AssociationEnd[] ends; 60 String classEnd0; 61 String classEnd1; 62 String end0; 63 String end1; 64 65 public void generate() { 66 association = input[0]; 67 out.println("package " +ImplHelper.implPrefix +JMIProvider.shortQualifierOf(association) +";"); 68 out.println("import " +JMIProvider.qualifierOf(association) +".*;"); 69 out.println("import org.objectweb.modfact.jmi.reflect.*;"); 70 out.println( 71 "public class " 72 + JMIProvider.jmiAssociationName(association) 73 + "Impl extends RefAssociationImpl " 74 + " implements " 75 + JMIProvider.jmiAssociationQualifiedName(association) 76 + " { \n" ); 77 78 ends = MofHelper.associationEndsOfAssociation(association); 79 classEnd0 = JMIProvider.jmiClassifierQualifiedName(ends[0].getType()); 80 classEnd1 = JMIProvider.jmiClassifierQualifiedName(ends[1].getType()); 81 end0 = ends[0].getName(); 82 end1 = ends[1].getName(); 83 84 existsTemplate(); 85 addTemplate(); 86 removeTemplate(); 87 queryTemplate(ends[0], end0, classEnd0, end1, classEnd1); 88 queryTemplate(ends[1], end1, classEnd1, end0, classEnd0); 89 90 out.println("}"); 91 out.flush(); 92 } 93 94 95 void queryTemplate(AssociationEnd end, String endName, String classEndName, 96 String otherEndName, String otherClassEndName) { 97 if (end.isNavigable() 99 && isSingleValued(end) ) { 100 out.println( 101 "\tpublic " 102 + classEndName 103 + " get" 104 + JMIProvider.jmiFormat1(endName) 105 + "(" 106 + otherClassEndName 107 + " " 108 + otherEndName 109 + ") throws javax.jmi.reflect.JmiException { "); 110 111 out.println("\t\tjava.util.Collection c =refQuery(\""+otherEndName +"\"," +otherEndName +");"); 112 out.println("\t\tif(c.isEmpty()) return null;"); 113 out.println("\t\treturn (" +classEndName +") c.iterator().next();"); 114 out.println("\t } "); 115 } 116 117 if (end.isNavigable() 119 && !isSingleValued(end) ) { 120 out.println( 121 "\tpublic " 122 +(end.getMultiplicity().isOrdered()? 123 "java.util.List" : "java.util.Collection" 124 ) 125 +" get" 126 + JMIProvider.jmiFormat1(endName) 127 + "(" 128 + otherClassEndName 129 + " " 130 + "end" 131 + ") throws javax.jmi.reflect.JmiException { "); 132 133 out.println("\t\treturn (java.util.List) refQuery(\""+otherEndName +"\"," +"end"+");"); 134 out.println("\t } "); 135 } 136 } 137 138 139 void addTemplate() { 140 if (ends[0].isChangeable() && ends[1].isChangeable()) { 142 out.println( 143 "\tpublic boolean add(" 144 + classEnd0 145 + " " 146 + "end0" 147 + "," 148 + classEnd1 149 + " " 150 + "end1" 151 + ") throws javax.jmi.reflect.JmiException { "); 152 out.println("\t\treturn refAddLink("+"end0"+","+"end1"+");"); 153 out.println("\t } "); 154 } 155 } 156 157 void existsTemplate() { 158 out.println( 159 "\tpublic boolean exists(" 160 + classEnd0 161 + " " 162 + "end0" 163 + "," 164 + classEnd1 165 + " " 166 + "end1" 167 + ") throws javax.jmi.reflect.JmiException { "); 168 169 out.println("\t\treturn refLinkExists("+ "end0"+","+"end1"+");"); 170 out.println("\t} "); 171 } 172 173 void removeTemplate() { 174 out.println( 175 "\tpublic boolean remove(" 176 + classEnd0 177 + " " 178 + "end0" 179 + "," 180 + classEnd1 181 + " " 182 + "end1" 183 + ") throws javax.jmi.reflect.JmiException " 184 +" { " ); 186 187 out.println("\t\treturn refRemoveLink("+"end0"+","+"end1"+");"); 188 out.println("\t } "); 189 } 190 191 192 193 boolean isSingleValued(AssociationEnd end) { 194 return end.getMultiplicity().getUpper() == 1; 195 } 196 197 } 198 | Popular Tags |