KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.modfact.jmi.logging.ModFactLogger;
24
25 import javax.jmi.model.*;
26
27 /**
28  * @author Xavier
29  *
30  */

31 public class ClassProxyImplementationGenerator
32     extends CommonImplementationGenerator {
33
34     MofClass[] input;
35
36     ModFactLogger logger;
37
38     /**
39      * Set Input
40      */

41     public void setInput(ModelElement[] elt) {
42         input = new MofClass[elt.length];
43         for (int i = 0; i < input.length; i++) {
44             input[i] = (MofClass) elt[i];
45         }
46     }
47
48     /**
49      * Set Trace
50      */

51     public void setLogger(ModFactLogger log) {
52         logger = log;
53     }
54
55     /**
56      * @see org.objectweb.modfact.generation.Generator#generate()
57      */

58     public void generate() {
59         //Tag todo
60
int c = 0;
61
62         out.println("package " +ImplHelper.implPrefix + JMIProvider.shortQualifierOf(input[c]) + ";" );
63         out.println("import " + JMIProvider.qualifierOf(input[c]) + ".*;" );
64         out.println("import org.objectweb.modfact.jmi.reflect.*;");
65         out.println(
66             "public class "
67                 + JMIProvider.jmiClassName(input[c])
68                 + "ClassImpl extends org.objectweb.modfact.jmi.reflect.RefClassImpl"
69                 + " implements "
70                 + JMIProvider.jmiClassQualifiedName(input[c]) +"Class"
71                 + " {");
72         
73         creatorTemplate(input[c]);
74         newObjectTemplate(input[c]);
75
76         //for each StructType directly contained by the class (correction of the spec ?)
77
dataTypeTemplates(input[c]);
78
79         newEnumTemplate(input[c]);
80         newStructTemplate(input[c]);
81
82         //for each attribute and operation contained in this class or one of its
83
//supertypes with "classifier-level" scope, generate the appropriate operations
84
Attribute[] attributes = MofHelper.attributesOfClass(
85                 input[c],
86                 ScopeKindEnum.forName("classifier_level"), true );
87         for (int i = 0; i < attributes.length; i++) {
88             attributeTemplate(attributes[i]);
89         }
90
91         Operation[] operations =
92             MofHelper.operationsOfClass(
93                 input[c],
94                 ScopeKindEnum.forName("classifier_level"), true );
95         for (int i = 0; i < operations.length; i++) {
96             operationTemplate(operations[i]);
97         }
98
99         out.println("}//end of interface");
100         out.flush();
101     }
102     
103     void creatorTemplate(MofClass cl) {
104         //If isAbstract is not set to true, generate factory methods.
105
if (!cl.isAbstract()) {
106             String JavaDoc javaType = JMIProvider.jmiClassQualifiedName(cl);
107             out.println(
108                 "\tpublic "
109                     + javaType
110                     + " create"
111                     + JMIProvider.jmiClassName(cl)
112                     + "() throws javax.jmi.reflect.JmiException { ");
113              
114               out.println("\t\t return ("
115                 +javaType
116                 +") refCreateInstance(null);");
117             out.println("\t} \n");
118
119             ArgList argList = new ArgList();
120             Attribute[] attributes = MofHelper.attributesOfClass(cl,
121                     ScopeKindEnum.forName("instance_level"), true );
122                                         
123             java.util.List JavaDoc v = new java.util.Vector JavaDoc();
124             for(int i =0; i<attributes.length; i++) {
125               if(!attributes[i].isDerived()) v.add(attributes[i]);
126             }
127             attributes = (Attribute[]) v.toArray(new Attribute[0]);
128                                 
129             if (attributes.length != 0) {
130                 out.print(
131                     "\tpublic "
132                         + JMIProvider.jmiClassQualifiedName(cl)
133                         + " create"
134                         + JMIProvider.jmiClassName(cl)
135                         + "(");
136                 //for each non-derived direct or inherited attribute
137
for (int i = 0; i < attributes.length; i++) {
138                     
139                     out.print(
140                         JMIProvider.typeTemplate(attributes[i]) +" " +attributes[i].getName()
141                     );
142                     argList.add(attributes[i]);
143                     if (i != attributes.length - 1)
144                         out.print(" , ");
145                 }
146                 out.println(") throws javax.jmi.reflect.JmiException { \n");
147                     out.print(argList.code);
148                     out.println("\t\t return ("
149                         +javaType
150                         +") refCreateInstance(list);");
151                 out.println("\t} \n");
152             }
153         }
154     }
155     
156     void newObjectTemplate(MofClass cl) {
157         out.println("\tpublic RefObjectImpl newObject() { ");
158           out.println("\t\treturn new " +ImplHelper.implPrefix
159             +JMIProvider.shortQualifierOf(cl)
160             +"."
161             +JMIProvider.jmiClassName(cl)+"Impl();");
162         out.println("\t}");
163     }
164
165 }
166
Popular Tags