KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > 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;
21
22 import javax.jmi.model.Attribute;
23 import javax.jmi.model.ModelElement;
24 import javax.jmi.model.MofClass;
25 import javax.jmi.model.Operation;
26 import javax.jmi.model.ScopeKindEnum;
27
28 import org.objectweb.modfact.jmi.helper.ImplHelper;
29 import org.objectweb.modfact.jmi.helper.JMIProvider;
30 import org.objectweb.modfact.jmi.helper.MofHelper;
31 import org.objectweb.modfact.jmi.logging.ModFactLogger;
32
33 /**
34  * @author Xavier
35  *
36  */

37 public class ClassProxyImplementationGenerator
38     extends CommonImplementationGenerator {
39
40     MofClass[] input;
41
42     ModFactLogger logger;
43
44     /**
45      * Set Input
46      */

47     public void setInput(ModelElement[] elt) {
48         input = new MofClass[elt.length];
49         for (int i = 0; i < input.length; i++) {
50             input[i] = (MofClass) elt[i];
51         }
52     }
53
54     /**
55      * Set Trace
56      */

57     public void setLogger(ModFactLogger log) {
58         logger = log;
59     }
60
61     /**
62      * @see org.objectweb.modfact.generation.Generator#generate()
63      */

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