KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > generator > common > lib > StateMemberMapping


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.generator.common.lib;
28
29 // Package dependencies
30
/** To access AST. */
31 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl;
32 import org.objectweb.openccm.ast.api.AbstractStorageTypeDecl;
33 /** To access Java AST. */
34 import org.objectweb.openccm.generator.java.ast.api.*;
35 import org.objectweb.openccm.generator.java.ast.lib.*;
36 /** Others. */
37 import org.objectweb.openccm.generator.common.lib.GenerationException;
38 import org.objectweb.openccm.generator.translator.idl2java.api.PSDL_JavaTranslator;
39 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
40
41
42 /**
43  * This class generates the mapping for a State Member.
44  *
45  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
46  *
47  * @version 0.1
48  */

49 public class StateMemberMapping
50   implements org.objectweb.openccm.pss.generator.common.api.StateMemberMapping
51 {
52
53     // ==================================================================
54
//
55
// Internal state.
56
//
57
// ==================================================================
58

59     /** The State member declaration. */
60     protected StorageTypeStateMemberDecl state_;
61
62     // ==================================================================
63
//
64
// Constructors.
65
//
66
// ==================================================================
67

68     /**
69      * The default constructor.
70      */

71     public StateMemberMapping()
72     {
73         // Init internal state
74
state_ = null;
75     }
76
77     // ==================================================================
78
//
79
// Internal methods.
80
//
81
// ==================================================================
82

83     // ==================================================================
84
//
85
// Public methods.
86
//
87
// ==================================================================
88

89     /**
90      * Set the State Memeber to map.
91      *
92      * @param state - The State Member to map.
93      */

94      public void
95      setStateMember(StorageTypeStateMemberDecl state)
96      {
97          state_ = state;
98      }
99
100     /**
101      * Map the name of the state member.
102      *
103      * @return The name of the mapping attribute.
104      */

105     public String JavaDoc
106     mapStateName()
107     {
108         return mapStateName( state_.getName() );
109     }
110
111     /**
112      * Get the name of the mapping attribute.
113      *
114      * @return The state name.
115      */

116     protected String JavaDoc
117     mapMarshalledStateName()
118     {
119         return mapMarshalledStateName( state_.getName() );
120     }
121
122     /**
123      * Map a Storage Type State Member.
124      *
125      * @param translator - A Java utility class to translate types.
126      * @param obj - The StorageType mapping object.
127      * @param finalAccessor - True if accessors must be final.
128      */

129     public void
130     toJavaAccessors( PSDL_JavaTranslator translator,
131                      InterfaceObject obj,
132                      boolean finalAccessor )
133     {
134         // Must be implemented in sub-classes.
135
}
136
137     /**
138      * Map a Read Only Storage Type State Member to a user non-available modifier.
139      *
140      * @param translator - A Java utility class to translate types.
141      * @param clazz - The StorageType mapping object.
142      */

143     public void
144     toJavaReadOnlyStateMemberModifier( PSDL_JavaTranslator translator,
145                                        ClassObject clazz )
146     {
147         MethodObject method = null;
148         ParameterObject param = null;
149
150         method = new MethodObjectImpl();
151         method.addComment("Set the "+state_.getName()+" value.");
152         method.addComment(" ");
153         method.addComment("@param initial_value - The value to set.");
154         method.setName( "set_" + state_.getName() );
155         method.setReturnType("void");
156         method.setFinal(true);
157         param = new ParameterObjectImpl();
158         param.setName("initial_value");
159         param.setType( translator.toJava(state_.getType()) );
160         method.addParameter(param);
161         method.getImpl().setMacro("SET_METHOD");
162         method.getImpl().addContextValue("var", mapStateName());
163         method.getImpl().addContextValue("value", "initial_value");
164         clazz.addMethod(method);
165     }
166
167     /**
168      * Map a Storage Type State Member to an attribute.
169      *
170      * @param translator - A Java utility class to translate types.
171      * @param clazz - The StorageType mapping object.
172      * @param st - The StorageType declaration.
173      */

174     public void
175     toJavaAttribute(PSDL_JavaTranslator translator,
176                     ClassObject clazz,
177                     org.objectweb.openccm.ast.api.StorageTypeDecl st)
178     {
179         AttributeObject att = null;
180         String JavaDoc marshalled_type = null;
181
182         att = new AttributeObjectImpl();
183         att.addComment("The "+state_.getName()+" state.");
184         att.setName( mapStateName() );
185         att.setType( translator.toJava(state_.getType()) );
186         att.setModifier( ModifierKindImpl.mk_private );
187         clazz.addAttribute(att);
188
189         // Add the mapping attribute if necessary
190
marshalled_type = PSDLMappingTools.getMarshalledType(st, state_);
191         if (marshalled_type != null)
192         {
193             // Add a XXX_marshalled attribute
194
att = new AttributeObjectImpl();
195             att.addComment("The " + state_.getName() + " marshalled state.");
196             att.setName( mapMarshalledStateName() );
197             att.setType(marshalled_type);
198             att.setModifier( ModifierKindImpl.mk_private );
199             clazz.addAttribute(att);
200         }
201     }
202
203     /**
204      * Map a state member to a java parameter.
205      *
206      * @param translator - A Java utility class to translate types.
207      * @param method - The method that will contain parameters.
208      **/

209     public void
210     toJavaParameter( PSDL_JavaTranslator translator,
211                      MethodObject method )
212     {
213         ParameterObject param = null;
214
215         // Create the corresponding parameter
216
param = new ParameterObjectImpl();
217         param.setName( state_.getName() );
218         param.setType( translator.toJava(state_.getType()) );
219         method.addParameter(param);
220     }
221
222     /**
223      * Map a state list (string[]) to ParameterObject for the java AST.
224      *
225      * @param generator - The PSDL to Java generator.
226      * @param states - The state list.
227      * @param method - The method that will contain parameters.
228      * @param ast - The storage type containing states.
229      **/

230     public static void
231     mapStateListToParameters( PSDL2JavaGenerator generator,
232                               String JavaDoc[] states,
233                               MethodObject method,
234                               AbstractStorageTypeDecl ast )
235     throws GenerationException
236     {
237         StorageTypeStateMemberDecl state = null;
238         ParameterObject param = null;
239
240         for (int i=0; i<states.length; i++)
241         {
242             // Find the state in the current storageType
243
state = (StorageTypeStateMemberDecl)generator.getDeclaration(ast, states[i]);
244
245             if (state == null)
246             {
247                 AbstractStorageTypeDecl[] implemented = null;
248
249                 // Find in the implemented classes
250
implemented = ast.getAllImplementedAbstractStorageTypes().getAbstractStorageTypes();
251                 if (implemented.length > 0)
252                 {
253                     int j = 0;
254                     while( (j<implemented.length) && (state == null) )
255                     {
256                         state = (StorageTypeStateMemberDecl)generator.getDeclaration(implemented[j], states[i]);
257                         j++;
258                     }
259                 }
260                 if (state == null)
261                 {
262                     String JavaDoc msg = "State Member '"+states[i]+"' not found!";
263                     throw new GenerationException(msg);
264                 }
265             }
266
267             // Create the corresponding parameter
268
org.objectweb.openccm.pss.generator.common.api.StateMemberMapping smm
269                 = new StateMemberMapping();
270             smm.setStateMember(state);
271             smm.toJavaParameter(generator.getTranslator(), method);
272         }
273     }
274
275     /**
276      * Map the name of the state member.
277      *
278      * @param name - The state member name.
279      *
280      * @return The name of the mapping attribute.
281      */

282     public static String JavaDoc
283     mapStateName(String JavaDoc name)
284     {
285         return name + "_state";
286     }
287
288     /**
289      * Get the name of the mapping attribute.
290      *
291      * @param name - The state member name.
292      *
293      * @return The state name.
294      */

295     public static String JavaDoc
296     mapMarshalledStateName(String JavaDoc name)
297     {
298         return name + "_marshalled";
299     }
300 }
301
Popular Tags