KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.impl;
2
3 import org.objectweb.modfact.jmi.helper.*;
4
5
6 import javax.jmi.model.*;
7 import java.util.*;
8
9 import org.objectweb.modfact.jmi.generator.BracketGenerator;
10
11 /**
12  * @author Xavier
13  *
14  */

15 public abstract class CommonImplementationGenerator
16     extends BracketGenerator {
17
18     public void attributeTemplate(Attribute attribute) {
19         if (attribute
20             .getVisibility()
21             .equals(VisibilityKindEnum.forName("public_vis"))) {
22             //<<ANNOTATION TEMPLATE>>
23

24             //----------------------- Accessor Operations
25
structuralFeatureAccessor(attribute);
26
27             //------------------------- Mutator Operations
28
structuralFeatureMutator(attribute);
29
30         }
31     }
32
33     public void referenceTemplate(Reference reference) {
34         if (reference
35             .getVisibility()
36             .equals(VisibilityKindEnum.forName("public_vis"))) {
37             //<<ANNOTATION TEMPLATE>>
38

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

173             argList.add(fields[j]);
174
175             if (j != fields.length - 1)
176                 output(" , ");
177         }
178         outputln(") throws javax.jmi.reflect.JmiException { ");
179         output(argList.code);
180         outputln(
181             "return ("
182                 + javaType
183                 + ") refCreateStruct(\""
184                 + t.getName()
185                 + "\",list);");
186         outputln("}");
187     }
188
189     void newStructTemplate(Namespace p) {
190         outputln("public RefStructImpl newStruct(String n) {");
191         Iterator it =
192             MofHelper
193                 .filterContentsByClass(p, StructureType.class, true)
194                 .iterator();
195         while (it.hasNext()) {
196             StructureType content = (StructureType) it.next();
197             outputln(
198                 "if(n.equals(\""
199                     + content.getName()
200                     + "\")) "
201                     + "return new " +ImplHelper.implPrefix
202                     + JMIProvider.shortQualifierOf(content)
203                     + "."
204                     + content.getName()
205                     + "Impl"
206                     + "();");
207         }
208         outputln("throw new RuntimeException(\"invalide StructureType: '\" + n + \"'\");");
209         outputln("}");
210     }
211
212     void newEnumTemplate(Namespace p) {
213         outputln("public Class newEnum(String n) {");
214         Iterator it =
215             MofHelper
216                 .filterContentsByClass(p, EnumerationType.class, true)
217                 .iterator();
218         while (it.hasNext()) {
219             EnumerationType content = (EnumerationType) it.next();
220             outputln(
221                 "if(n.equals(\""
222                     + content.getName()
223                     + "\")) "
224                     + "return "
225                     + JMIProvider.jmiDataTypeQualifiedName(content)
226                     + "Enum.class;");
227         }
228         outputln("throw new RuntimeException(\"invalide EnumType : '\" + n + \"'\");");
229         outputln("}");
230     }
231
232     // produce code to create argument list
233
static class ArgList {
234         String JavaDoc code = "java.util.List list = new java.util.Vector(); \n";
235
236         void add(TypedElement t) {
237             code =
238                 code
239                     + "list.add("
240                     + ImplHelper.wrap(t, t.getName())
241                     + ");\n";
242         }
243
244     }
245
246 }
247
Popular Tags