OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / rmic / SourceRmicCompiler.java
1 /* SourceRmicCompiler.java -- RMI stub generator for java.rmi.*
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath 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, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 */
26
27
28 package gnu.classpath.tools.rmic;
29
30 import java.lang.reflect.Method;
31 import java.io.File;
32 import java.util.Iterator;
33
34 import gnu.classpath.tools.rmic.AbstractMethodGenerator;
35
36 /**
37  * RMI stub source code generator, required to support java.rmi.*
38  *
39  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) 
40  */
41 public class SourceRmicCompiler extends SourceGiopRmicCompiler
42 {
43   /**
44    * If true, the zero size object array is declared in the stub to reduce
45    * garbage generation.
46    */
47   public boolean addZeroSizeObjecArray;
48   
49   /**
50    * Generate a RMI stub.
51    * 
52    * @return the string, containing the text of the generated stub.
53    */
54   public String generateStub()
55   {
56     String template = getResource("Stub_12.jav");
57
58     // Generate methods.
59     StringBuilder b = new StringBuilder();
60     Iterator iter = methods.iterator();
61     while (iter.hasNext())
62       {
63         RmiMethodGenerator m = (RmiMethodGenerator) iter.next();
64         b.append(m.generateStubMethod());
65       }
66
67     vars.put("#stub_methods", b.toString());
68     vars.put("#imports", getImportStatements());
69     vars.put("#interfaces", getAllInterfaces());
70     vars.put("#stub_method_declarations", getStubMethodDeclarations());
71     vars.put("#stub_method_initializations", getStubMethodInitializations());
72     if (addZeroSizeObjecArray)
73       {
74         vars.put("#zeroSizeObjecArray",
75                "private static final Object[] NO_ARGS = new Object[0];");
76         vars.put("#zeroSizeClassArray",
77                "final Class[]  NO_ARGSc = new Class[0];");
78       }
79     else
80       {
81         vars.put("#zeroSizeObjecArray","");
82         vars.put("#zeroSizeClassArray","");        
83       }
84
85     String output = replaceAll(template, vars);
86     return output;
87   }
88
89   /**
90    * Create a method generator, applicable for RMI stub methods.
91    */
92   protected AbstractMethodGenerator createMethodGenerator(Method m)
93   {
94     return new RmiMethodGenerator(m, this);
95   } 
96   
97   /**
98    * Get the stub method declarations.
99    */
100   public String getStubMethodDeclarations()
101   {
102     StringBuilder b = new StringBuilder();
103     
104     Iterator iter = methods.iterator();
105      
106     while (iter.hasNext())
107       {
108         RmiMethodGenerator method = (RmiMethodGenerator) iter.next();
109         b.append("    ");
110         b.append("private static final Method met_");
111         b.append(method.method.getName());
112         b.append(';');
113         if (iter.hasNext())
114           b.append('\n');
115       }
116     return b.toString();
117   }
118   
119   /**
120    * Get stub method initializations. These must be done in a try-catch
121    * statement to catch {@link NoSuchMethodException}.
122    */
123   public String getStubMethodInitializations()
124   {
125     StringBuilder b = new StringBuilder();
126     
127     Iterator iter = methods.iterator();
128      
129     while (iter.hasNext())
130       {
131         RmiMethodGenerator method = (RmiMethodGenerator) iter.next();
132         b.append("             ");
133         b.append("met_");
134         b.append(method.method.getName());
135         b.append(" =\n               ");
136         b.append(name(method.method.getDeclaringClass()));
137         b.append(".class.getMethod(");
138         b.append('"');
139         b.append(method.method.getName());
140         b.append("\", ");        
141         if (method.method.getParameterTypes().length == 0)
142           b.append("NO_ARGSc);");
143         else
144           {
145             b.append("new Class[]\n                 {\n                   ");
146             b.append(method.getArgListAsClassArray());
147             b.append("\n                 }");
148             b.append(");");
149           }
150         b.append('\n');
151       }
152     return b.toString();
153   }
154
155   /**
156    * Prepare for the compilation of the next class.
157    */
158   public void reset()
159   {
160     addZeroSizeObjecArray = false;
161     super.reset();
162   }
163
164   /**
165    * Additional processing of the stub name (nothing to do for JRMP stubs).
166    */
167   public String convertStubName(String name)
168   {
169     return name;
170   }  
171
172   /**
173    * Override to do nothing.
174    */
175   protected boolean outputTie(File fw, Class c)
176   {
177     return true;
178   }
179 }