KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > gen > InstanceGenerator


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.mdr.handlers.gen;
20
21 import java.util.*;
22 import java.io.*;
23
24 import org.netbeans.mdr.storagemodel.*;
25 import org.netbeans.mdr.util.*;
26 import javax.jmi.model.*;
27
28 /**
29  *
30  * @author Martin Matula, Dusan Balek
31  * @version
32  */

33 class InstanceGenerator extends FeaturedGenerator {
34
35     InstanceGenerator(String JavaDoc name, Class JavaDoc ifc, Class JavaDoc handler, StorableObject storable, Class JavaDoc custom) {
36         super(name, ifc, handler, storable, custom);
37     }
38
39     protected String JavaDoc getConstructorDescriptor() {
40         return "(Lorg/netbeans/mdr/storagemodel/StorableObject;)V"; //NOI18N
41
}
42         
43     protected MethodInfo[] generateMethods() throws IOException {
44         try {
45             ArrayList methods = new ArrayList();
46             HashSet createdMethods = new HashSet();
47         methods.add(generateConstructor());
48             for (Iterator it = new ContainsIterator(obj.getMetaObject()); it.hasNext();) {
49                 StorableObject element = (StorableObject) it.next();
50                 String JavaDoc elementName = (String JavaDoc) element.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
51                 String JavaDoc metaTypeName = (String JavaDoc) element.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
52                 String JavaDoc substName = TagSupport.getSubstName(element, elementName, metaTypeName);
53                 if (metaTypeName.equals(MOFConstants.SH_MODEL_ATTRIBUTE)) {
54                     VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY);
55                     ScopeKind scope = (ScopeKind) element.getAttribute(MOFConstants.SH_MODEL_FEATURE_SCOPE);
56                     if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS) && scope.equals(ScopeKindEnum.INSTANCE_LEVEL)) {
57                         String JavaDoc attrName = firstUpper(substName);
58                         String JavaDoc attrTypeName = TagSupport.getDataTypeName(getAttrType(element));
59                         MultiplicityType multiplicity = (MultiplicityType) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY);
60                         if (multiplicity.getUpper() == 1) {
61                             String JavaDoc name;
62                             String JavaDoc setterName;
63                             if (attrTypeName.equals("java.lang.Boolean")) { //NOI18N
64
if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); //NOI18N
65
else name = "is" + attrName; //NOI18N
66
setterName = "set" + name.substring(2); //NOI18N
67
} else {
68                                 name = "get" + attrName; //NOI18N
69
setterName = "set" + attrName; //NOI18N
70
}
71                             String JavaDoc getterType = attrTypeName;
72                             if (multiplicity.getLower() == 1)
73                                 getterType = getPrimitiveName(getterType);
74                             String JavaDoc sign = name + getMethodDescriptor(new String JavaDoc[0], getterType);
75                             if (!createdMethods.contains(sign)) {
76                                 methods.addAll(getFeatureMethod(name, new String JavaDoc[0], getterType, M_GET_NAME, M_GET_DESC, M_GET_TYPE, element, (StorableFeatured)obj, "_getAttribute")); //NOI18N
77
createdMethods.add(sign);
78                             }
79                             boolean changeable = ((Boolean JavaDoc) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_IS_CHANGEABLE)).booleanValue();
80                             if (changeable) {
81                                 sign = setterName + getMethodDescriptor(new String JavaDoc[] {getterType}, "void"); //NOI18N
82
if (!createdMethods.contains(sign)) {
83                                     methods.addAll(getFeatureMethod(setterName, new String JavaDoc[] {getterType}, "void", M_SET_NAME, M_SET_DESC, M_SET_TYPE, element, (StorableFeatured)obj, "_setAttribute")); //NOI18N
84
createdMethods.add(sign);
85                                 }
86                             }
87                         } else if (multiplicity.getUpper() != 0) {
88                             String JavaDoc sign = "get" + attrName + getMethodDescriptor(new String JavaDoc[0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED)); //NOI18N
89
if (!createdMethods.contains(sign)) {
90                                 methods.addAll(getFeatureMethod("get" + attrName, new String JavaDoc[0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED), M_GET_NAME, M_GET_DESC, M_GET_TYPE, element, (StorableFeatured)obj, "_getAttribute")); //NOI18N
91
createdMethods.add(sign);
92                             }
93                         }
94                     }
95                 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_OPERATION)) {
96                     VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY);
97                     if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) {
98                         Collection parameters = new ArrayList();
99                         String JavaDoc operType = "void"; //NOI18N
100
for (Iterator itt = ((List) element.getReference(MOFConstants.SH_MODEL_NAMESPACE_CONTENTS)).iterator(); itt.hasNext();) {
101                             StorableObject el = (StorableObject) itt.next();
102                             String JavaDoc mtName = (String JavaDoc) el.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
103                             if (mtName.equals(MOFConstants.SH_MODEL_PARAMETER)) {
104                                 DirectionKind direction = (DirectionKind) el.getAttribute(MOFConstants.SH_MODEL_PARAMETER_DIRECTION_KIND);
105                                 if (direction.equals(DirectionKindEnum.RETURN_DIR))
106                                     operType = getAttrTypeName(el);
107                                 else
108                                     parameters.add(getAttrTypeName(el) + (direction.equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); //NOI18N
109
}
110                         }
111                         String JavaDoc[] prms = (String JavaDoc[])parameters.toArray(new String JavaDoc [parameters.size()]);
112                         String JavaDoc sign = substName + getMethodDescriptor(prms, operType);
113                         if (!createdMethods.contains(sign)) {
114                             MethodInfo mInfo = getOperationMethod(substName, prms, operType, elementName);
115                             if (mInfo != null) {
116                                 Collection exceptions = (Collection) element.getReference(MOFConstants.SH_MODEL_OPERATION_EXCEPTIONS);
117                                 short[] declaredExceptions = new short[exceptions.size()];
118                                 int i = 0;
119                                 for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++)
120                                     declaredExceptions[i] = cp.getClass(dotToSlash(TagSupport.getTypeFullName((StorableObject)itt.next())));
121                                 mInfo.setDeclaredExceptions(declaredExceptions);
122                                 methods.add(mInfo);
123                             }
124                             createdMethods.add(sign);
125                         }
126                     }
127                 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_REFERENCE)) {
128                     VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY);
129                     if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) {
130                         String JavaDoc refName = firstUpper(substName);
131                         String JavaDoc refType = TagSupport.getDataTypeName(getAttrType(element));
132                         MultiplicityType multiplicity = (MultiplicityType) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY);
133                         if (multiplicity.getUpper() == 1) {
134                             String JavaDoc sign = "get" + refName + getMethodDescriptor(new String JavaDoc[0], refType); //NOI18N
135
if (!createdMethods.contains(sign)) {
136                                 methods.addAll(getHandlerMethod("get" + refName, new String JavaDoc[0], refType, M_GET_NAME + "R", M_GET_DESC, M_GET_TYPE, elementName)); //NOI18N
137
createdMethods.add(sign);
138                             }
139                             boolean changeable = ((Boolean JavaDoc) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_IS_CHANGEABLE)).booleanValue();
140                             if (changeable) {
141                                 sign = "set" + refName + getMethodDescriptor(new String JavaDoc[] {refType}, "void"); //NOI18N
142
if (!createdMethods.contains(sign)) {
143                                     methods.addAll(getHandlerMethod("set" + refName, new String JavaDoc[] {refType}, "void", M_SET_NAME + "R", M_SET_DESC, M_SET_TYPE, elementName)); //NOI18N
144
createdMethods.add(sign);
145                                 }
146                             }
147                         } else if (multiplicity.getUpper() != 0) {
148                             String JavaDoc sign = "get" + refName + getMethodDescriptor(new String JavaDoc[0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED)); //NOI18N
149
if (!createdMethods.contains(sign)) {
150                                 methods.addAll(getHandlerMethod("get" + refName, new String JavaDoc[0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED), M_GET_NAME + "R", M_GET_DESC, M_GET_TYPE, elementName)); //NOI18N
151
createdMethods.add(sign);
152                             }
153                         }
154                     }
155                 }
156             }
157             
158             MethodInfo[] mtds = super.generateMethods();
159             for(int i = 0; i < mtds.length; i++)
160                 methods.add(mtds[i]);
161             return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
162         }catch (Exception JavaDoc e) {
163             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
164         }
165     }
166 }
167
Popular Tags