KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > mapping > ClassFileMapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.jmi.mapping;
20
21 import org.netbeans.api.mdr.JMIStreamFactory;
22 import org.netbeans.lib.jmi.util.ClassFileGenerator;
23 import org.netbeans.lib.jmi.util.ContainsIterator;
24 import javax.jmi.model.*;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.DataOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  *
36  * @author Dusan Balek
37  * @version 0.1
38  */

39 public class ClassFileMapper extends GenericMapper {
40     protected String JavaDoc[] getAncestorNames(GeneralizableElement ge, String JavaDoc postfix, String JavaDoc defaultAncestor) {
41         Collection JavaDoc supertypes = ge.getSupertypes();
42         if (supertypes.size() == 0)
43             return new String JavaDoc [] {defaultAncestor};
44             String JavaDoc[] ancestors = new String JavaDoc [supertypes.size()];
45             int i = 0;
46             for (Iterator it = supertypes.iterator(); it.hasNext(); i++)
47                 ancestors[i] = tagProvider.getTypeFullName((ModelElement) it.next()) + postfix;
48             return ancestors;
49     }
50     
51     protected static Object JavaDoc getTypedValue(String JavaDoc typeName, String JavaDoc value) {
52         if (value == null)
53             return null;
54         else if (typeName.equals("String")) //NOI18N
55
return value;
56         else if (typeName.equals("Integer")) //NOI18N
57
return new Integer JavaDoc(value);
58         else if (typeName.equals("Boolean")) //NOI18N
59
return new Integer JavaDoc(value.equalsIgnoreCase("true") ? 1 : 0); //NOI18N
60
else if (typeName.equals("Double")) //NOI18N
61
return new Double JavaDoc(value);
62         else if (typeName.equals("Float")) //NOI18N
63
return new Float JavaDoc(value);
64         else if (typeName.equals("Long")) //NOI18N
65
return new Long JavaDoc(value);
66         else
67             return null;
68     }
69
70     public ClassFileMapper(JMIStreamFactory sf) {
71         this.generator = sf;
72     }
73     
74     private final JMIStreamFactory generator;
75     private OutputStream JavaDoc stream;
76
77     protected boolean createStream(List JavaDoc pkg, String JavaDoc fileName) throws IOException JavaDoc {
78         if (stream != null) {
79             closeStream();
80             throw new IllegalStateException JavaDoc("Attempting to create stream before previous stream was closed.");
81         }
82         stream = generator.createStream(pkg, fileName, JMIStreamFactory.EXT_CLASS);
83         return stream != null;
84     }
85     
86     protected void closeStream() throws IOException JavaDoc {
87         if (stream == null)
88             throw new IllegalStateException JavaDoc("Attempting to close the stream without opening it first.");
89         OutputStream JavaDoc os = stream;
90         stream = null;
91         os.close();
92     }
93     
94     protected void classProxyTemplate(javax.jmi.model.MofClass objClass) throws IOException JavaDoc {
95         new ClassProxyGenerator(objClass).generate();
96     }
97     
98     protected void classInstanceTemplate(javax.jmi.model.MofClass objClass) throws IOException JavaDoc {
99         new ClassInstanceGenerator(objClass).generate();
100     }
101     
102     // generates interface for a given association
103
protected void associationTemplate(Association objAssociation) throws IOException JavaDoc {
104         new AssociationGenerator(objAssociation).generate();
105     }
106     
107     // generates interface for a given package
108
protected void packageTemplate(MofPackage objPackage) throws IOException JavaDoc {
109         new PackageGenerator(objPackage).generate();
110     }
111     
112     //generates class for an exception
113
protected void exceptionTemplate(MofException objException) throws IOException JavaDoc {
114         new ExceptionGenerator(objException).generate();
115     }
116     
117     //generates interface for an enumeration
118
protected void enumerationInterfaceTemplate(EnumerationType objEnumeration) throws IOException JavaDoc {
119         new EnumerationGenerator(objEnumeration).generate();
120     }
121     
122     //generates class for an enumeration
123
protected void enumerationClassTemplate(EnumerationType objEnumeration) throws IOException JavaDoc {
124         new EnumerationImplGenerator(objEnumeration).generate();
125     }
126     
127     // generates interface for a structure
128
protected void structureTemplate(StructureType objStructure) throws IOException JavaDoc {
129         new StructureGenerator(objStructure).generate();
130     }
131     
132     abstract class JMIClassFileGenerator extends ClassFileGenerator {
133         protected JMIClassFileGenerator(String JavaDoc className, String JavaDoc[] interfaces, String JavaDoc superclass, int accessFlags) {
134             super(className, interfaces, superclass, accessFlags);
135         }
136         
137         protected void generate() throws IOException JavaDoc {
138             generateClassFile(stream);
139         }
140     }
141     
142     class ClassProxyGenerator extends JMIClassFileGenerator {
143         
144         protected javax.jmi.model.MofClass objClass;
145         
146         public ClassProxyGenerator(javax.jmi.model.MofClass objClass) {
147             super(tagProvider.getTypeFullName(objClass) + CLASS_POSTFIX, new String JavaDoc[] {DT_CLASS}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
148             this.objClass = objClass;
149         }
150         
151         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
152             ArrayList JavaDoc methods = new ArrayList JavaDoc();
153             for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
154                 ModelElement element = (ModelElement) it.next();
155                 if (element instanceof StructureType) {
156                     ArrayList JavaDoc parameters = new ArrayList JavaDoc();
157                     for (Iterator itt = ((StructureType)element).getContents().iterator(); itt.hasNext();) {
158                         ModelElement field = (ModelElement) itt.next();
159                         if (field instanceof StructureField)
160                             parameters.add(getTypeName2((StructureField) field));
161                     }
162                     methods.add(new MethodInfo("create" + firstUpper(tagProvider.getSubstName(element)), getMethodDescriptor((String JavaDoc[])parameters.toArray(new String JavaDoc[parameters.size()]), tagProvider.getTypeFullName(element)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
163
}
164             }
165             ArrayList JavaDoc allAttributes = new ArrayList JavaDoc();
166             for (Iterator it = new ContainsIterator(objClass); it.hasNext();) {
167                 ModelElement element = (ModelElement) it.next();
168                 if (element instanceof Attribute) {
169                     Attribute attr = (Attribute) element;
170                     if (attr.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) {
171                         String JavaDoc attrName = firstUpper(tagProvider.getSubstName(attr));
172                         Classifier attrType = getAttrType(attr);
173                         String JavaDoc attrTypeName = getTypeName(attrType);
174                         if (attr.getMultiplicity().getUpper() == 1) {
175                             String JavaDoc name;
176                             String JavaDoc setterName;
177                             if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { //NOI18N
178
if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); //NOI18N
179
else name = "is" + attrName; //NOI18N
180
setterName = "set" + name.substring(2); //NOI18N
181
} else {
182                                 name = "get" + attrName; //NOI18N
183
setterName = "set" + attrName; //NOI18N
184
}
185                             String JavaDoc getterType = attrTypeName;
186                             if (attr.getMultiplicity().getLower() == 1)
187                                 getterType = getPrimitiveName(getterType);
188                             methods.add(new MethodInfo(name, getMethodDescriptor(new String JavaDoc[0], getterType), ACC_PUBLIC | ACC_ABSTRACT));
189                             if (attr.isChangeable())
190                                 methods.add(new MethodInfo(setterName, getMethodDescriptor(new String JavaDoc[] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
191
} else if (attr.getMultiplicity().getUpper() != 0)
192                             methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String JavaDoc[0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
193
} else if (!attr.isDerived())
194                         allAttributes.add(getTypeName(attr));
195                 } else if (element instanceof Operation) {
196                     Operation oper = (Operation) element;
197                     if (oper.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) {
198                         Collection JavaDoc parameters = new ArrayList JavaDoc();
199                         String JavaDoc operType = "void"; //NOI18N
200
for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) {
201                             ModelElement el = (ModelElement) itt.next();
202                             if (el instanceof Parameter) {
203                                 Parameter param = (Parameter) el;
204                                 if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR))
205                                     operType = getTypeName(param);
206                                 else
207                                     parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); //NOI18N
208
}
209                         }
210                         MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String JavaDoc[])parameters.toArray(new String JavaDoc [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT);
211                         Collection JavaDoc exceptions = oper.getExceptions();
212                         short[] declaredExceptions = new short[exceptions.size()];
213                         int i = 0;
214                         for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++)
215                             declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next())));
216                         mInfo.setDeclaredExceptions(declaredExceptions);
217                         methods.add(mInfo);
218                     }
219                 }
220             }
221             if (!objClass.isAbstract()) {
222                 methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor(new String JavaDoc[0], tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
223
if (allAttributes.size() > 0)
224                     methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor((String JavaDoc[])allAttributes.toArray(new String JavaDoc [allAttributes.size()]), tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
225
}
226             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
227         }
228         
229         protected FieldInfo[] generateFields() throws IOException JavaDoc {
230             return new FieldInfo[0];
231         }
232     }
233     
234     class ClassInstanceGenerator extends JMIClassFileGenerator {
235         
236         protected javax.jmi.model.MofClass objClass;
237         
238         public ClassInstanceGenerator(javax.jmi.model.MofClass objClass) {
239             super(tagProvider.getTypeFullName(objClass), getAncestorNames(objClass, "", DT_INSTANCE), DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); //NOI18N
240
this.objClass = objClass;
241         }
242         
243         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
244             ArrayList JavaDoc methods = new ArrayList JavaDoc();
245             for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
246                 ModelElement element = (ModelElement) it.next();
247                 if (element instanceof Feature) {
248                     Feature feature = (Feature) element;
249                     if (feature.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) {
250                         if (feature instanceof Attribute) {
251                             Attribute attr = (Attribute) feature;
252                             String JavaDoc attrName = firstUpper(tagProvider.getSubstName(attr));
253                             Classifier attrType = getAttrType(attr);
254                             String JavaDoc attrTypeName = getTypeName(attrType);
255                             if (attr.getMultiplicity().getUpper() == 1) {
256                                 String JavaDoc name;
257                                 String JavaDoc setterName;
258                                 if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { //NOI18N
259
if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); //NOI18N
260
else name = "is" + attrName; //NOI18N
261
setterName = "set" + name.substring(2); //NOI18N
262
} else {
263                                     name = "get" + attrName; //NOI18N
264
setterName = "set" + attrName; //NOI18N
265
}
266                                 String JavaDoc getterType = attrTypeName;
267                                 if (attr.getMultiplicity().getLower() == 1)
268                                     getterType = getPrimitiveName(getterType);
269                                 methods.add(new MethodInfo(name, getMethodDescriptor(new String JavaDoc[0], getterType), ACC_PUBLIC | ACC_ABSTRACT));
270                                 if (attr.isChangeable())
271                                     methods.add(new MethodInfo(setterName, getMethodDescriptor(new String JavaDoc[] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
272
} else if (attr.getMultiplicity().getUpper() != 0)
273                                 methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String JavaDoc[0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
274
} else if (feature instanceof Operation) {
275                             Operation oper = (Operation) feature;
276                             Collection JavaDoc parameters = new ArrayList JavaDoc();
277                             String JavaDoc operType = "void"; //NOI18N
278
for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) {
279                                 ModelElement el = (ModelElement) itt.next();
280                                 if (el instanceof Parameter) {
281                                     Parameter param = (Parameter) el;
282                                     if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR))
283                                         operType = getTypeName(param);
284                                     else
285                                         parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); //NOI18N
286
}
287                             }
288                             MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String JavaDoc[])parameters.toArray(new String JavaDoc [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT);
289                             Collection JavaDoc exceptions = oper.getExceptions();
290                             short[] declaredExceptions = new short[exceptions.size()];
291                             int i = 0;
292                             for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++)
293                                 declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next())));
294                             mInfo.setDeclaredExceptions(declaredExceptions);
295                             methods.add(mInfo);
296                         } else if (feature instanceof Reference) {
297                             Reference ref = (Reference) feature;
298                             String JavaDoc refName = firstUpper(tagProvider.getSubstName(ref));
299                             String JavaDoc refType = getTypeName(getAttrType(ref));
300                             if (ref.getMultiplicity().getUpper() == 1) {
301                                 methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String JavaDoc[0], refType), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
302
if (ref.isChangeable())
303                                     methods.add(new MethodInfo("set" + refName, getMethodDescriptor(new String JavaDoc[] {refType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
304
} else if (ref.getMultiplicity().getUpper() != 0) {
305                                 methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String JavaDoc[0], (ref.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
306
}
307                         }
308                     }
309                 }
310             }
311             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
312         }
313         
314         protected FieldInfo[] generateFields() throws IOException JavaDoc {
315             ArrayList JavaDoc fields = new ArrayList JavaDoc();
316             for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
317                 ModelElement element = (ModelElement) it.next();
318                 if (element instanceof Constant) {
319                     DataType type = (DataType) getAttrType((Constant)element);
320                     String JavaDoc value = ((Constant)element).getValue();
321                     FieldInfo fInfo = new FieldInfo(tagProvider.getSubstName(element), getFieldType(getTypeName2((Constant)element)), ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
322                     fInfo.setConstValue(getTypedValue(type.getName(), value));
323                     fields.add(fInfo);
324                 }
325             }
326             return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]);
327         }
328     }
329     
330     class AssociationGenerator extends JMIClassFileGenerator {
331         
332         protected Association objAssociation;
333         
334         public AssociationGenerator(Association objAssociation) {
335             super(tagProvider.getTypeFullName(objAssociation), new String JavaDoc[] {DT_ASSOCIATION}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
336             this.objAssociation = objAssociation;
337             
338             
339         }
340         
341         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
342             ArrayList JavaDoc methods = new ArrayList JavaDoc();
343             AssociationEnd[] ends = new AssociationEnd[2];
344             int i = 0;
345             for (Iterator it = objAssociation.getContents().iterator(); it.hasNext() && i < 2;) {
346                 Object JavaDoc element = it.next();
347                 if (element instanceof AssociationEnd)
348                     ends[i++] = (AssociationEnd) element;
349             }
350             String JavaDoc end1Name = tagProvider.getSubstName(ends[0]);
351             String JavaDoc end1Class = tagProvider.getTypeFullName(getAttrType(ends[0]));
352             String JavaDoc end2Name = tagProvider.getSubstName(ends[1]);
353             String JavaDoc end2Class = tagProvider.getTypeFullName(getAttrType(ends[1]));
354             // exists(<associationEnd1>, <associationEnd2>)
355
methods.add(new MethodInfo("exists", getMethodDescriptor(new String JavaDoc[] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
356
// <associationEnd1>(<associationEnd2>)
357
if (ends[0].isNavigable()) {
358                 methods.add(new MethodInfo("get" + firstUpper(end1Name), getMethodDescriptor(new String JavaDoc[] {end2Class}, //NOI18N
359
ends[0].getMultiplicity().getUpper() == 1 ? end1Class : (ends[0].getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT));
360             }
361             // <associationEnd2>(<associationEnd1>)
362
if (ends[1].isNavigable()) {
363                 methods.add(new MethodInfo("get" + firstUpper(end2Name), getMethodDescriptor(new String JavaDoc[] {end1Class}, //NOI18N
364
ends[1].getMultiplicity().getUpper() == 1 ? end2Class : (ends[1].getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT));
365             }
366             if (ends[0].isChangeable() && ends[1].isChangeable()) {
367                 // add(<associationEnd1>, <associationEnd2>)
368
methods.add(new MethodInfo("add", getMethodDescriptor(new String JavaDoc[] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
369
// remove(<associationEnd1>, <associationEnd2>)
370
methods.add(new MethodInfo("remove", getMethodDescriptor(new String JavaDoc[] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
371
}
372             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
373         }
374         
375         protected FieldInfo[] generateFields() throws IOException JavaDoc {
376             return new FieldInfo[0];
377         }
378     }
379     
380     class PackageGenerator extends JMIClassFileGenerator {
381         
382         protected MofPackage objPackage;
383         
384         public PackageGenerator(MofPackage objPackage) {
385             super(tagProvider.getTypeFullName(objPackage) + PACKAGE_POSTFIX, getAncestorNames(objPackage, PACKAGE_POSTFIX, DT_PACKAGE), DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
386             this.objPackage = objPackage;
387         }
388         
389         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
390             ArrayList JavaDoc methods = new ArrayList JavaDoc();
391             for (Iterator it = objPackage.getContents().iterator(); it.hasNext();) {
392                 ModelElement element = (ModelElement) it.next();
393                 String JavaDoc typeName = tagProvider.getTypeFullName(element);
394                 String JavaDoc elementName = tagProvider.getSubstName(element);
395                 if (element instanceof Association) {
396                 } else if (element instanceof javax.jmi.model.MofClass) {
397                     typeName += CLASS_POSTFIX;
398                 } else if (element instanceof MofPackage) {
399                     typeName += PACKAGE_POSTFIX;
400                 } else if (element instanceof EnumerationType) {
401                     continue;
402                 } else if (element instanceof StructureType) {
403                     ArrayList JavaDoc parameters = new ArrayList JavaDoc();
404                     for (Iterator itt = ((StructureType)element).getContents().iterator(); itt.hasNext();) {
405                         ModelElement field = (ModelElement) itt.next();
406                         if (field instanceof StructureField)
407                             parameters.add(getTypeName2((StructureField) field));
408                     }
409                     methods.add(new MethodInfo("create" + firstUpper(tagProvider.getSubstName(element)), getMethodDescriptor((String JavaDoc[])parameters.toArray(new String JavaDoc[parameters.size()]), tagProvider.getTypeFullName(element)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
410
continue;
411                 } else if (element instanceof Import) {
412                     Import imp = (Import) element;
413                     if (imp.isClustered() && imp.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) {
414                         Namespace namespace = imp.getImportedNamespace();
415                         if (namespace instanceof MofPackage) {
416                             typeName = tagProvider.getTypeFullName(namespace) + PACKAGE_POSTFIX;
417                             element = namespace;
418                         }
419                     } else
420                         continue;
421                 } else
422                     continue;
423                 if (((GeneralizableElement) element).getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) {
424                     methods.add(new MethodInfo("get" + elementName, getMethodDescriptor(new String JavaDoc[0], typeName), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
425
}
426             }
427             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
428         }
429         
430         protected FieldInfo[] generateFields() throws IOException JavaDoc {
431             return new FieldInfo[] {};
432         }
433     }
434     
435     class ExceptionGenerator extends JMIClassFileGenerator {
436         
437         protected MofException objException;
438         
439         public ExceptionGenerator(MofException objException) {
440             super(tagProvider.getTypeFullName(objException), new String JavaDoc[0], DT_EXCEPTION, ACC_PUBLIC | ACC_SUPER);
441             this.objException = objException;
442         }
443         
444         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
445             ArrayList JavaDoc methods = new ArrayList JavaDoc();
446             ArrayList JavaDoc paramInits = new ArrayList JavaDoc();
447             ArrayList JavaDoc paramTypes = new ArrayList JavaDoc();
448             // parameter accessors
449
short i = 1;
450             for (Iterator it = objException.getContents().iterator(); it.hasNext();) {
451                 Object JavaDoc element = it.next();
452                 if (element instanceof Parameter) {
453                     Parameter param = (Parameter) element;
454                     String JavaDoc typeName = getTypeName(param);
455                     paramTypes.add(typeName);
456                     String JavaDoc getterName = firstLower(tagProvider.getSubstName(param));
457                     String JavaDoc paramName = removeUnderscores(getterName);
458                     if (param.getType() instanceof PrimitiveType && param.getType().getName().equals("Boolean")) { //NOI18N
459
if (getterName.indexOf("is") != 0) //NOI18N
460
getterName = removeUnderscores("is_" + getterName); //NOI18N
461
} else
462                         getterName = removeUnderscores("get_" + getterName); //NOI18N
463
MethodInfo mInfo = new MethodInfo(getterName, getMethodDescriptor(new String JavaDoc[0], typeName), ACC_PUBLIC);
464                     DataOutputStream JavaDoc code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
465                     codeReturnFieldValue(className, paramName, getFieldType(typeName), false, code);
466                     mInfo.setMaxStack((short)1);
467                     mInfo.setMaxLocals((short)1);
468                     methods.add(mInfo);
469                     ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc(6);
470                     DataOutputStream JavaDoc paramInitCode = new DataOutputStream JavaDoc(bout);
471                     code_aload(0, paramInitCode);
472                     code_aload(i++, paramInitCode);
473                     paramInitCode.writeByte(opc_putfield);
474                     paramInitCode.writeShort(cp.getFieldRef(dotToSlash(className), paramName, getFieldType(typeName)));
475                     paramInits.add(bout.toByteArray());
476                     // getterName = paramName + ": \" + " + paramName;
477
// if (codeMsg.length() == 0) {
478
// codeMsg.append("\"" + getterName);
479
// } else {
480
// codeMsg.append(" + \", " + getterName);
481
}
482             }
483             //constructor
484
MethodInfo mInfo = new MethodInfo("<init>", getMethodDescriptor((String JavaDoc[])paramTypes.toArray(new String JavaDoc[paramTypes.size()]), "void"), ACC_PUBLIC); //NOI18N
485
DataOutputStream JavaDoc code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
486 // generate("super(" + codeMsg.toString() + ");");
487
code_aload(0, code);
488             code.writeByte(opc_invokespecial);
489             code.writeShort(cp.getMethodRef(dotToSlash(className), "<init>", getMethodDescriptor(new String JavaDoc[0], "void"))); //NOI18N
490
for (Iterator it = paramInits.iterator(); it.hasNext();)
491                 code.write((byte[]) it.next());
492             code.writeByte(opc_return);
493             mInfo.setMaxStack((short)2);
494             mInfo.setMaxLocals(i);
495             methods.add(mInfo);
496             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
497         }
498         
499         protected FieldInfo[] generateFields() throws IOException JavaDoc {
500             ArrayList JavaDoc fields = new ArrayList JavaDoc();
501             // private parameter holders
502
for (Iterator it = objException.getContents().iterator(); it.hasNext();) {
503                 Object JavaDoc element = it.next();
504                 if (element instanceof Parameter) {
505                     Parameter param = (Parameter) element;
506                     String JavaDoc typeName = getTypeName(param);
507                     String JavaDoc paramName = removeUnderscores(firstLower(tagProvider.getSubstName(param)));
508                     fields.add(new FieldInfo(paramName, getFieldType(typeName), ACC_PRIVATE | ACC_FINAL));
509                 }
510             }
511             return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]);
512         }
513     }
514     
515     class EnumerationGenerator extends JMIClassFileGenerator {
516         
517         protected EnumerationType objEnumeration;
518         
519         public EnumerationGenerator(EnumerationType objEnumeration) {
520             super(tagProvider.getTypeFullName(objEnumeration), new String JavaDoc[] {DT_ENUMERATION}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
521             this.objEnumeration = objEnumeration;
522         }
523         
524         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
525             return new MethodInfo[0];
526         }
527         
528         protected FieldInfo[] generateFields() throws IOException JavaDoc {
529             return new FieldInfo[0];
530         }
531     }
532     
533     class EnumerationImplGenerator extends JMIClassFileGenerator {
534         
535         protected EnumerationType objEnumeration;
536         
537         public EnumerationImplGenerator(EnumerationType objEnumeration) {
538             super(tagProvider.getTypeFullName(objEnumeration) + ENUM_POSTFIX, new String JavaDoc[] {tagProvider.getTypeFullName(objEnumeration)}, DT_ANY, ACC_PUBLIC | ACC_FINAL | ACC_SUPER);
539             this.objEnumeration = objEnumeration;
540         }
541         
542         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
543             ArrayList JavaDoc methods = new ArrayList JavaDoc();
544             // constructor
545
MethodInfo mInfo = new MethodInfo("<init>", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "void"), ACC_PRIVATE); //NOI18N
546
DataOutputStream JavaDoc code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
547             code_aload(0, code);
548             code.writeByte(opc_invokespecial);
549             code.writeShort(cp.getMethodRef("java/lang/Object", "<init>", getMethodDescriptor(new String JavaDoc[0], "void"))); //NOI18N
550
code_aload(0, code);
551             code_aload(1, code);
552             code.writeByte(opc_putfield);
553             code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); //NOI18N
554
code.writeByte(opc_return);
555             mInfo.setMaxStack((short)2);
556             mInfo.setMaxLocals((short)2);
557             methods.add(mInfo);
558             // refTypeName
559
mInfo = new MethodInfo("refTypeName", getMethodDescriptor(new String JavaDoc[0], "java.util.List"), ACC_PUBLIC); //NOI18N
560
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
561             codeReturnFieldValue(className, "typeName", getFieldType("java.util.List"), false, code); //NOI18N
562
mInfo.setMaxStack((short)1);
563             mInfo.setMaxLocals((short)1);
564             methods.add(mInfo);
565             // toString
566
mInfo = new MethodInfo("toString", getMethodDescriptor(new String JavaDoc[0], "java.lang.String"), ACC_PUBLIC); //NOI18N
567
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
568             codeReturnFieldValue(className, "literalName", getFieldType("java.lang.String"), false, code); //NOI18N
569
mInfo.setMaxStack((short)1);
570             mInfo.setMaxLocals((short)1);
571             methods.add(mInfo);
572             // hashCode
573
mInfo = new MethodInfo("hashCode", getMethodDescriptor(new String JavaDoc[0], "int"), ACC_PUBLIC); //NOI18N
574
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
575             code_aload(0, code);
576             code.writeByte(opc_getfield);
577             code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); //NOI18N
578
code.write(opc_invokevirtual);
579             code.writeShort(cp.getMethodRef("java/lang/Object", "hashCode", getMethodDescriptor(new String JavaDoc[0], "int"))); //NOI18N
580
code.writeByte(opc_ireturn);
581             mInfo.setMaxStack((short)1);
582             mInfo.setMaxLocals((short)1);
583             methods.add(mInfo);
584             // equals
585
mInfo = new MethodInfo("equals", getMethodDescriptor(new String JavaDoc[] {"java.lang.Object"}, "boolean"), ACC_PUBLIC); //NOI18N
586
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
587             // if (o instanceof <EnumIdentifier>Enum) return (o == this);
588
code_aload(1, code);
589             code.writeByte(opc_instanceof);
590             code.writeShort(cp.getClass(dotToSlash(className)));
591             code.writeByte(opc_ifeq);
592             code.writeShort(12);
593             code.writeByte(opc_aload_0 + 1);
594             code.writeByte(opc_aload_0);
595             code.writeByte(opc_if_acmpne);
596             code.writeShort(5);
597             code.writeByte(opc_iconst_1);
598             code.writeByte(opc_ireturn);
599             code.writeByte(opc_iconst_0);
600             code.writeByte(opc_ireturn);
601             // else if (o instanceof <EnumIdentifier>)
602
// return (o.toString().equals(literalName));
603
code_aload(1, code);
604             code.writeByte(opc_instanceof);
605             code.writeShort(cp.getClass(dotToSlash(tagProvider.getTypeFullName(objEnumeration))));
606             code.writeByte(opc_ifeq);
607             code.writeShort(15);
608             code.writeByte(opc_aload_0 + 1);
609             code.writeByte(opc_invokevirtual);
610             code.writeShort(cp.getMethodRef("java/lang/Object", "toString", getMethodDescriptor(new String JavaDoc[0], "java.lang.String"))); //NOI18N
611
code.writeByte(opc_aload_0);
612             code.writeByte(opc_getfield);
613             code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); //NOI18N
614
code.writeByte(opc_invokevirtual);
615             code.writeShort(cp.getMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String JavaDoc[] {"java.lang.Object"}, "boolean"))); //NOI18N
616
code.writeByte(opc_ireturn);
617             // else return ((o instanceof javax.jmi.reflect.RefEnum)
618
// && ((javax.jmi.reflect.RefEnum) o).refTypeName().equals(typeName)
619
// && o.toString().equals(literalName));
620
code_aload(1, code);
621             code.writeByte(opc_instanceof);
622             code.writeShort(cp.getClass(dotToSlash(DT_ENUMERATION)));
623             code.writeByte(opc_ifeq);
624             code.writeShort(39);
625             code.writeByte(opc_aload_0 + 1);
626             code.writeByte(opc_checkcast);
627             code.writeShort(cp.getClass(dotToSlash(DT_ENUMERATION)));
628             code.writeByte(opc_invokeinterface);
629             code.writeShort(cp.getInterfaceMethodRef(dotToSlash(DT_ENUMERATION), "refTypeName", getMethodDescriptor(new String JavaDoc[0], "java.util.List"))); //NOI18N
630
code.writeByte(1);
631             code.writeByte(0);
632             code.writeByte(opc_getstatic);
633             code.writeShort(cp.getFieldRef(dotToSlash(className), "typeName", getFieldType("java.util.List"))); //NOI18N
634
code.writeByte(opc_invokeinterface);
635             code.writeShort(cp.getInterfaceMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String JavaDoc[] {"java.lang.Object"}, "boolean"))); //NOI18N
636
code.writeByte(2);
637             code.writeByte(0);
638             code.writeByte(opc_ifeq);
639             code.writeShort(19);
640             code.writeByte(opc_aload_0 + 1);
641             code.writeByte(opc_invokevirtual);
642             code.writeShort(cp.getMethodRef("java/lang/Object", "toString", getMethodDescriptor(new String JavaDoc[0], "java.lang.String"))); //NOI18N
643
code.writeByte(opc_aload_0);
644             code.writeByte(opc_getfield);
645             code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); //NOI18N
646
code.writeByte(opc_invokevirtual);
647             code.writeShort(cp.getMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String JavaDoc[] {"java.lang.Object"}, "boolean"))); //NOI18N
648
code.writeByte(opc_ifeq);
649             code.writeShort(5);
650             code.writeByte(opc_iconst_1);
651             code.writeByte(opc_ireturn);
652             code.writeByte(opc_iconst_0);
653             code.writeByte(opc_ireturn);
654             mInfo.setMaxStack((short)2);
655             mInfo.setMaxLocals((short)2);
656             methods.add(mInfo);
657             // readResolve
658
mInfo = new MethodInfo("readResolve", getMethodDescriptor(new String JavaDoc[0], "java.lang.Object"), ACC_PROTECTED); //NOI18N
659
mInfo.setDeclaredExceptions(new short[] {cp.getClass("java/io/ObjectStreamException")}); //NOI18N
660
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
661             code_aload(0, code);
662             code.write(opc_getfield);
663             code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); //NOI18N
664
code.writeByte(opc_invokestatic);
665             code.writeShort(cp.getMethodRef(dotToSlash(className), "forName", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, tagProvider.getTypeFullName(objEnumeration)))); //NOI18N
666
code.writeByte(opc_areturn);
667             code_astore(1, code);
668             code.writeByte(opc_new);
669             code.writeShort(cp.getClass("java/io/ObjectStreamException")); //NOI18N
670
code.writeByte(opc_dup);
671             code_aload(1, code);
672             code.writeByte(opc_invokevirtual);
673             code.writeShort(cp.getMethodRef("java/lang/Throwable", "getMessage", getMethodDescriptor(new String JavaDoc[0], "java.lang.String"))); //NOI18N
674
code.writeByte(opc_invokespecial);
675             code.writeShort(cp.getMethodRef("java/io/ObjectStreamException", "<init>", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "void"))); //NOI18N
676
code.writeByte(opc_athrow);
677             mInfo.getExceptionTable().add(new ExceptionTableEntry((short) 0, (short) 8, (short) 8, cp.getClass("java/lang/IllegalArgumentException"))); //NOI18N
678
mInfo.setMaxStack((short)3);
679             mInfo.setMaxLocals((short)2);
680             methods.add(mInfo);
681             // forName & static initializer
682
mInfo = new MethodInfo("forName", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, tagProvider.getTypeFullName(objEnumeration)), ACC_PUBLIC | ACC_STATIC); //NOI18N
683
code = new DataOutputStream JavaDoc(mInfo.getCodeStream());
684             MethodInfo initInfo = new MethodInfo("<clinit>", getMethodDescriptor(new String JavaDoc[0], "void"), ACC_STATIC); //NOI18N
685
DataOutputStream JavaDoc initCode = new DataOutputStream JavaDoc(initInfo.getCodeStream());
686             for (Iterator it = objEnumeration.getLabels().iterator(); it.hasNext();) {
687                 String JavaDoc literal = (String JavaDoc) it.next();
688                 code_aload(0, code);
689                 code_ldc(cp.getString(literal), code);
690                 code.writeByte(opc_invokevirtual);
691                 code.writeShort(cp.getMethodRef("java/lang/String", "equals", getMethodDescriptor(new String JavaDoc[] {"java.lang.Object"}, "boolean"))); //NOI18N
692
code.writeByte(opc_ifeq);
693                 code.writeShort(7);
694                 code.writeByte(opc_getstatic);
695                 code.writeShort(cp.getFieldRef(dotToSlash(className), tagProvider.mapEnumLiteral(literal), getFieldType(className)));
696                 code.write(opc_areturn);
697                 initCode.writeByte(opc_new);
698                 initCode.writeShort(cp.getClass(dotToSlash(className)));
699                 initCode.writeByte(opc_dup);
700                 code_ldc(cp.getString(literal), initCode);
701                 initCode.writeByte(opc_invokespecial);
702                 initCode.writeShort(cp.getMethodRef(dotToSlash(className), "<init>", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "void"))); //NOI18N
703
initCode.writeByte(opc_putstatic);
704                 initCode.writeShort(cp.getFieldRef(dotToSlash(className), tagProvider.mapEnumLiteral(literal), getFieldType(className)));
705             }
706             code.writeByte(opc_new);
707             code.writeShort(cp.getClass("java/lang/IllegalArgumentException")); //NOI18N
708
code.writeByte(opc_dup);
709             code.writeByte(opc_new);
710             code.writeShort(cp.getClass("java/lang/StringBuffer")); //NOI18N
711
code.writeByte(opc_dup);
712             code.writeByte(opc_invokespecial);
713             code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "<init>", getMethodDescriptor(new String JavaDoc[0], "void"))); //NOI18N
714
code_ldc(cp.getString("Unknown enumeration value "), code); //NOI18N
715
code.writeByte(opc_invokevirtual);
716             code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "java.lang.StringBuffer"))); //NOI18N
717
code_aload(0, code);
718             code.writeByte(opc_invokevirtual);
719             code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "java.lang.StringBuffer"))); //NOI18N
720
code_ldc(cp.getString(" for type " + tagProvider.getTypeFullName(objEnumeration)), code); //NOI18N
721
code.writeByte(opc_invokevirtual);
722             code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "java.lang.StringBuffer"))); //NOI18N
723
code.writeByte(opc_invokevirtual);
724             code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "toString", getMethodDescriptor(new String JavaDoc[0], "java.lang.String"))); //NOI18N
725
code.writeByte(opc_invokespecial);
726             code.writeShort(cp.getMethodRef("java/lang/IllegalArgumentException", "<init>", getMethodDescriptor(new String JavaDoc[] {"java.lang.String"}, "void"))); //NOI18N
727
code.writeByte(opc_athrow);
728             mInfo.setMaxStack((short)4);
729             mInfo.setMaxLocals((short)1);
730             methods.add(mInfo);
731             initCode.writeByte(opc_new);
732             initCode.writeShort(cp.getClass("java/util/ArrayList")); //NOI18N
733
initCode.writeByte(opc_dup);
734             initCode.writeByte(opc_invokespecial);
735             initCode.writeShort(cp.getMethodRef("java/util/ArrayList", "<init>", getMethodDescriptor(new String JavaDoc[0], "void"))); //NOI18N
736
code_astore(0, initCode);
737             for (Iterator it = objEnumeration.getQualifiedName().iterator(); it.hasNext();) {
738                 code_aload(0, initCode);
739                 code_ldc(cp.getString((String JavaDoc)it.next()), initCode);
740                 initCode.writeByte(opc_invokevirtual);
741                 initCode.writeShort(cp.getMethodRef("java/util/ArrayList", "add", getMethodDescriptor(new String JavaDoc [] {"java.lang.Object"}, "boolean"))); //NOI18N
742
initCode.writeByte(opc_pop);
743             }
744             code_aload(0, initCode);
745             initCode.writeByte(opc_invokestatic);
746             initCode.writeShort(cp.getMethodRef("java/util/Collections", "unmodifiableList", getMethodDescriptor(new String JavaDoc[] {"java.util.List"}, "java.util.List"))); //NOI18N
747
initCode.writeByte(opc_putstatic);
748             initCode.writeShort(cp.getFieldRef(dotToSlash(className), "typeName", getFieldType("java.util.List"))); //NOI18N
749
initCode.writeByte(opc_return);
750             initInfo.setMaxStack((short)3);
751             initInfo.setMaxLocals((short)1);
752             methods.add(initInfo);
753             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
754         }
755         
756         protected FieldInfo[] generateFields() throws IOException JavaDoc {
757             ArrayList JavaDoc fields = new ArrayList JavaDoc();
758             // public fields (literals)
759
for (Iterator it = objEnumeration.getLabels().iterator(); it.hasNext();) {
760                 String JavaDoc literal = (String JavaDoc) it.next();
761                 FieldInfo fInfo = new FieldInfo(tagProvider.mapEnumLiteral(literal), getFieldType(className), ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
762                 fields.add(fInfo);
763             }
764             // private fields
765
fields.add(new FieldInfo("typeName", getFieldType("java.util.List"), ACC_PRIVATE | ACC_STATIC | ACC_FINAL)); //NOI18N
766
fields.add(new FieldInfo("literalName", getFieldType("java.lang.String"), ACC_PRIVATE | ACC_FINAL)); //NOI18N
767
return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]);
768         }
769     }
770     
771     class StructureGenerator extends JMIClassFileGenerator {
772         
773         protected StructureType objStructure;
774         
775         public StructureGenerator(StructureType objStructure) {
776             super(tagProvider.getTypeFullName(objStructure), new String JavaDoc[] {DT_STRUCTURE}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
777             this.objStructure = objStructure;
778         }
779         
780         protected MethodInfo[] generateMethods() throws IOException JavaDoc {
781             ArrayList JavaDoc methods = new ArrayList JavaDoc();
782             // attribute accessors
783
for (Iterator it = objStructure.getContents().iterator(); it.hasNext();) {
784                 ModelElement field = (ModelElement) it.next();
785                 if (field instanceof StructureField) {
786                     Classifier fieldType = ((StructureField) field).getType();
787                     String JavaDoc memberName = firstUpper(tagProvider.getSubstName(field));
788                     if (fieldType instanceof PrimitiveType && fieldType.getName().equals("Boolean")) { //NOI18N
789
if (memberName.indexOf("Is") != 0) //NOI18N
790
memberName = "is" + memberName; //NOI18N
791
else
792                             memberName = firstLower(memberName);
793                     } else
794                         memberName = "get" + memberName; //NOI18N
795
MethodInfo mInfo = new MethodInfo(memberName, getMethodDescriptor(new String JavaDoc[0], getPrimitiveName(getTypeName(fieldType))), ACC_PUBLIC | ACC_ABSTRACT);
796 // mInfo.setDeclaredExceptions(new short[] {cp.getClass("javax/jmi/reflect/JmiException")});
797
methods.add(mInfo);
798                 }
799             }
800             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
801         }
802         
803         protected FieldInfo[] generateFields() throws IOException JavaDoc {
804             return new FieldInfo[0];
805         }
806     }
807 }
808
Popular Tags