View Javadoc

1   /*
2    * Copyright (c) 2001-2005,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }