libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / rmic / SourceRmicCompiler.java
blob20ab8c1563764c4a44c4f5e63a0079bbb4a0131f
1 /* SourceRmicCompiler.java -- RMI stub generator for java.rmi.*
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
28 package gnu.classpath.tools.rmic;
30 import java.lang.reflect.Method;
31 import java.io.File;
32 import java.util.Iterator;
34 import gnu.classpath.tools.rmic.AbstractMethodGenerator;
36 /**
37 * RMI stub source code generator, required to support java.rmi.*
39 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
41 public class SourceRmicCompiler extends SourceGiopRmicCompiler
43 /**
44 * If true, the zero size object array is declared in the stub to reduce
45 * garbage generation.
47 public boolean addZeroSizeObjecArray;
49 /**
50 * Generate a RMI stub.
52 * @return the string, containing the text of the generated stub.
54 public String generateStub()
56 String template = getResource("Stub_12.jav");
58 // Generate methods.
59 StringBuilder b = new StringBuilder();
60 Iterator iter = methods.iterator();
61 while (iter.hasNext())
63 RmiMethodGenerator m = (RmiMethodGenerator) iter.next();
64 b.append(m.generateStubMethod());
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)
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];");
79 else
81 vars.put("#zeroSizeObjecArray","");
82 vars.put("#zeroSizeClassArray","");
85 String output = replaceAll(template, vars);
86 return output;
89 /**
90 * Create a method generator, applicable for RMI stub methods.
92 protected AbstractMethodGenerator createMethodGenerator(Method m)
94 return new RmiMethodGenerator(m, this);
97 /**
98 * Get the stub method declarations.
100 public String getStubMethodDeclarations()
102 StringBuilder b = new StringBuilder();
104 Iterator iter = methods.iterator();
106 while (iter.hasNext())
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');
116 return b.toString();
120 * Get stub method initializations. These must be done in a try-catch
121 * statement to catch {@link NoSuchMethodException}.
123 public String getStubMethodInitializations()
125 StringBuilder b = new StringBuilder();
127 Iterator iter = methods.iterator();
129 while (iter.hasNext())
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
145 b.append("new Class[]\n {\n ");
146 b.append(method.getArgListAsClassArray());
147 b.append("\n }");
148 b.append(");");
150 b.append('\n');
152 return b.toString();
156 * Prepare for the compilation of the next class.
158 public void reset()
160 addZeroSizeObjecArray = false;
161 super.reset();
165 * Additional processing of the stub name (nothing to do for JRMP stubs).
167 public String convertStubName(String name)
169 return name;
173 * Override to do nothing.
175 protected boolean outputTie(File fw, Class c)
177 return true;