1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.treedl;
33
34 import java.util.Iterator;
35
36 import com.unitesk.atp.treedl.TDL.*;
37 import com.unitesk.atp.tool.PluginClass;
38
39 /***
40 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
41 * @version $Id: JavaCopyVisitorGenerator.java,v 1.21 2005/11/23 11:11:12 all-x Exp $
42 */
43 public class JavaCopyVisitorGenerator
44 extends JavaTextGenerator
45 implements TDL_Visitor
46 {
47 /***
48 * {@inheritDoc}
49 */
50 public String getVersion()
51 {
52 return PluginClass.getRevision( "$Revision: 1.21 $" ) + "-stable";
53 }
54
55 public void visitModule( Module node )
56 {
57 setFunction( "Up", new NameFunction() );
58
59 generateHeader();
60
61 int indexOfLastDot = getOutClassName().lastIndexOf( '.' );
62 if( indexOfLastDot != -1 )
63 {
64 txt( "package " + getOutClassName().substring( 0, indexOfLastDot ) + ";" ); nl();
65 nl();
66 }
67
68 txt( "import ${name}.*;" ); nl();
69 nl();
70
71 String outShortClassName = getOutClassName().substring( indexOfLastDot + 1 );
72
73 txt( "public class " + outShortClassName + " implements ${name}.${visitorName}" ); nl();
74 block_nl();
75
76 txt( "private ${.Node} res;" ); nl();
77 nl();
78 txt( "public ${.Node} copy( ${.Node} node )" ); nl();
79 block_nl();
80 txt( "if( node != null ) node.accept( this ); else res = null;" ); nl();
81 txt( "return res;" ); nl();
82 unblock_nl();
83 nl();
84 txt( "public java.util.List copyList( java.util.List list )" ); nl();
85 block_nl();
86 txt( "java.util.List res;" ); nl();
87 txt( "try" ); nl();
88 block_nl();
89 txt( " res = (java.util.List)list.getClass().newInstance();" ); nl();
90 unblock_nl();
91 txt( "catch( java.lang.InstantiationException ie )" ); nl();
92 block_nl();
93 txt( "throw new java.lang.IllegalArgumentException( ie.toString() );" ); nl();
94 unblock_nl();
95 txt( "catch( java.lang.IllegalAccessException iae )" ); nl();
96 block_nl();
97 txt( "throw new java.lang.IllegalArgumentException( iae.toString() );" ); nl();
98 unblock_nl();
99 nl();
100 txt( "for( int i = 0; i < list.size(); i++ )" ); nl();
101 block_nl();
102 txt( "res.add( copy( (${.Node})list.get(i) ) );" ); nl();
103 unblock_nl();
104 txt( "return res;" ); nl();
105 unblock_nl();
106
107 TDL_ModuleMap moduleMap = node.getModuleMap();
108 Iterator it = moduleMap.getModuleNames().iterator();
109 while( it.hasNext() )
110 {
111 TDL_Module tdlModule = moduleMap.getModule( (QID)it.next() );
112 Module t = tdlModule.getRoot();
113 pushNode( t );
114 list( "i", 0, t.sizeOptMemberList(), "${optMemberList[i]}", null );
115 popNode();
116 }
117 unblock_nl();
118 }
119
120 public void visitNodeTypeDecl( NodeTypeDecl node )
121 {
122 if( node.isAbstract() ) return;
123 nl();
124 txt( "public void visit${name}( ${parent.name}.${name} node )" ); nl();
125 block_nl();
126 txt( "res =" ); nl();
127 txt( "new ${parent.name}.${name}" );
128 incIndent();
129 int constructorParametersCount = 0;
130 for( int i = 0; i < node.sizeFullFieldList(); i++ )
131 {
132 Field field = node.getFullFieldList( i );
133
134 if( TDL_Module.isConstructorParameter( field ) )
135 {
136 pushNode( field );
137
138 nl();
139 txtif( constructorParametersCount == 0, "(", "," );
140 if( field.isChild() )
141 {
142 if( field.getType().checkCardinality( Cardinality.LIST ) )
143 {
144 txt( " copyList( node.get${Up:name}() )" );
145 } else {
146 txt( " (${node.type})copy( node.get${Up:name}() )" );
147 }
148 } else {
149 txt( " node.get${Up:name}()" );
150 }
151
152 constructorParametersCount++;
153
154 popNode();
155 }
156 }
157 switch( constructorParametersCount )
158 {
159 case 0:
160 txt( "(" );
161 break;
162 case 1:
163 txt( " " );
164 break;
165 default:
166 nl();
167 break;
168 }
169 txt( ");" ); nl();
170 popIndent();
171 unblock_nl();
172 }
173
174 public void visitConstTypeDecl( ConstTypeDecl node )
175 {
176 }
177
178 public void visitOperationDecl( OperationDecl node )
179 {
180 }
181
182 public void visitBaseModule( BaseModule node )
183 {
184 throw new UnsupportedOperationException();
185 }
186
187 public void visitConstructorCodeMember( ConstructorCodeMember node )
188 {
189 throw new UnsupportedOperationException();
190 }
191
192 public void visitBodyCodeMember( BodyCodeMember node )
193 {
194 throw new UnsupportedOperationException();
195 }
196
197 public void visitField( Field node )
198 {
199 throw new UnsupportedOperationException();
200 }
201
202 public void visitNonVirtualParameterDecl( NonVirtualParameterDecl node )
203 {
204 throw new UnsupportedOperationException();
205 }
206
207 public void visitVirtualParameterDecl( VirtualParameterDecl node )
208 {
209 throw new UnsupportedOperationException();
210 }
211
212 public void visitCase( Case node )
213 {
214 throw new UnsupportedOperationException();
215 }
216
217 public void visitCaseSignature( CaseSignature node )
218 {
219 throw new UnsupportedOperationException();
220 }
221
222 public void visitParameter( Parameter node )
223 {
224 throw new UnsupportedOperationException();
225 }
226 }