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