KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > tayloredBased > CommonImplementationGenerator


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.jmi.impl.tayloredBased;
21
22 import org.objectweb.modfact.jmi.helper.*;
23
24
25 import javax.jmi.model.*;
26 import java.util.*;
27
28 import org.objectweb.modfact.jmi.generator.PrintGenerator;
29
30 /**
31  * @author Xavier
32  *
33  */

34 public abstract class CommonImplementationGenerator
35     extends PrintGenerator {
36
37     public void attributeTemplate(Attribute attribute) {
38         if (attribute
39             .getVisibility()
40             .equals(VisibilityKindEnum.forName("public_vis"))) {
41             //<<ANNOTATION TEMPLATE>>
42

43             //----------------------- Accessor Operations
44
structuralFeatureAccessor(attribute);
45
46             //------------------------- Mutator Operations
47
structuralFeatureMutator(attribute);
48
49         }
50     }
51
52     public void referenceTemplate(Reference reference) {
53         if (reference
54             .getVisibility()
55             .equals(VisibilityKindEnum.forName("public_vis"))) {
56             //<<ANNOTATION TEMPLATE>>
57

58             //----------------------- Accessor Operations
59
structuralFeatureAccessor(reference);
60
61             //------------------------- Mutator Operations
62
structuralFeatureMutator(reference);
63
64         }
65     }
66
67     public void structuralFeatureAccessor(StructuralFeature f) {
68
69         String JavaDoc javaType = JMIProvider.typeTemplate(f);
70         String JavaDoc accessorName = JMIProvider.jmiAccessorName(f);
71
72         out.println(
73             "\tpublic "
74                 + javaType
75                 + " "
76                 + accessorName
77                 + "() throws javax.jmi.reflect.JmiException { ");
78
79         out.println(
80             "\t return ("
81                 + javaType
82                 + ") "
83                 + ImplHelper.unwrap(f, "refGetValue(\"" + f.getName() + "\")")
84                 + ";");
85         out.println("\t } ");
86     }
87
88     public void structuralFeatureMutator(StructuralFeature f) {
89
90         String JavaDoc mutatorName = JMIProvider.jmiMutatorName(f);
91         String JavaDoc javaType = JMIProvider.typeTemplate(f);
92
93         if (f.getMultiplicity().getUpper() == 1 && f.isChangeable()) {
94             out.println(
95                 "\tpublic void "
96                     + mutatorName
97                     + "("
98                     + javaType
99                     + " newValue) throws javax.jmi.reflect.JmiException { ");
100
101             out.println(
102                 "\t refSetValue("
103                     + "\""
104                     + f.getName()
105                     + "\","
106                     + ImplHelper.wrap(f, "newValue")
107                     + ");");
108             out.println("\t } ");
109         }
110     }
111
112     ///////////////// Operation //////////////////////////////////
113
public void operationTemplate(Operation operation) {
114         if (operation
115             .getVisibility()
116             .equals(VisibilityKindEnum.forName("public_vis"))) {
117             //<<JAVADOCS TEMPLATE>>
118
//Get Parameters and exceptions
119
Parameter[] parameters = MofHelper.parametersOf(operation);
120             MofException[] exceptions = MofHelper.exceptionsOf(operation);
121             Parameter returnParameter = null;
122
123             for (int i = 0; i < parameters.length; i++) {
124                 if (parameters[i]
125                     .getDirection()
126                     .equals(DirectionKindEnum.RETURN_DIR)) {
127                     returnParameter = parameters[i];
128                 }
129             }
130
131             if (returnParameter == null)
132                 out.print("\tpublic void ");
133             else
134                 out.print(
135                     "\tpublic "
136                         + JMIProvider.typeTemplate(returnParameter)
137                         + " ");
138
139             out.print(operation.getName() + "(");
140
141             boolean comma = false;
142             for (int i = 0; i < parameters.length; i++) {
143                 DirectionKind dir = parameters[i].getDirection();
144                 if (!dir.equals(DirectionKindEnum.RETURN_DIR)) {
145                     if (comma)
146                         out.print(",");
147                     out.print(
148                         JMIProvider.typeTemplate(parameters[i])
149                             + " "
150                             + parameters[i].getName());
151                     comma = true;
152                 }
153             }
154             out.print(") throws ");
155
156             //for each exception
157
for (int i = 0; i < exceptions.length; i++) {
158                 out.print(exceptions[i].getName() + ",");
159             }
160             out.println("javax.jmi.reflect.JmiException { ");
161             out.println("throw new RuntimeException(\"No Implementation\");");
162             out.println("\t } ");
163         }
164     }
165
166     /////////////////// DATA TYPE //////////////////
167
public void dataTypeTemplates(Namespace ns) {
168         //for each StructType directly contained by the package/class
169
//for each EnumType directly contained by the package/class
170
DataType[] containedDataTypes = MofHelper.datatypesOf(ns, true);
171         for (int i = 0; i < containedDataTypes.length; i++) {
172             if (containedDataTypes[i] instanceof StructureType) {
173                 structTemplate((StructureType) containedDataTypes[i]);
174
175             }
176         }
177     }
178
179     public void structTemplate(StructureType t) {
180
181         String JavaDoc javaType = JMIProvider.jmiDataTypeQualifiedName(t);
182         out.print("\tpublic " + javaType + " create" + t.getName() + " (");
183
184         ArgList argList = new ArgList();
185         StructureField[] fields = MofHelper.structureFieldsOf(t);
186         for (int j = 0; j < fields.length; j++) {
187             out.print(JMIProvider.typeTemplate(fields[j]));
188             //attribute type
189
out.print(" ");
190             out.print(fields[j].getName()); //attribute name
191

192             argList.add(fields[j]);
193
194             if (j != fields.length - 1)
195                 out.print(" , ");
196         }
197         out.println(") throws javax.jmi.reflect.JmiException { ");
198         out.print(argList.code);
199         out.println(
200             "\t return ("
201                 + javaType
202                 + ") refCreateStruct(\""
203                 + t.getName()
204                 + "\",list);");
205         out.println("}");
206     }
207
208     void newStructTemplate(Namespace p) {
209         out.println("\tpublic RefStructImpl newStruct(String n) {");
210         Iterator it =
211             MofHelper
212                 .filterContentsByClass(p, StructureType.class, true)
213                 .iterator();
214         while (it.hasNext()) {
215             StructureType content = (StructureType) it.next();
216             out.println(
217                 "\t\t if(n.equals(\""
218                     + content.getName()
219                     + "\")) "
220                     + "return new " +ImplHelper.implPrefix
221                     + JMIProvider.shortQualifierOf(content)
222                     + "."
223                     + content.getName()
224                     + "Impl"
225                     + "();");
226         }
227         out.println("\t\t throw new RuntimeException(\"invalide StructureType: '\" + n + \"'\");");
228         out.println("\t}");
229     }
230
231     void newEnumTemplate(Namespace p) {
232         out.println("\tpublic Class newEnum(String n) {");
233         Iterator it =
234             MofHelper
235                 .filterContentsByClass(p, EnumerationType.class, true)
236                 .iterator();
237         while (it.hasNext()) {
238             EnumerationType content = (EnumerationType) it.next();
239             out.println(
240                 "\t\t if(n.equals(\""
241                     + content.getName()
242                     + "\")) "
243                     + "return "
244                     + JMIProvider.jmiDataTypeQualifiedName(content)
245                     + "Enum.class;");
246         }
247         out.println("\t\t throw new RuntimeException(\"invalide EnumType : '\" + n + \"'\");");
248         out.println("\t}");
249     }
250
251     // produce code to create argument list
252
static class ArgList {
253         String JavaDoc code = "\t\tjava.util.List list = new java.util.Vector(); \n";
254
255         void add(TypedElement t) {
256             code =
257                 code
258                     + "\t\t list.add("
259                     + ImplHelper.wrap(t, t.getName())
260                     + ");\n";
261         }
262
263     }
264
265 }
266
Popular Tags