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)
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
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
28 package gnu.classpath.tools.rmic;
30 import java.lang.reflect.Method;
32 import java.util.Iterator;
34 import gnu.classpath.tools.rmic.AbstractMethodGenerator;
37 * RMI stub source code generator, required to support java.rmi.*
39 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
41 public class SourceRmicCompiler extends SourceGiopRmicCompiler
44 * If true, the zero size object array is declared in the stub to reduce
47 public boolean addZeroSizeObjecArray;
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");
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];");
81 vars.put("#zeroSizeObjecArray","");
82 vars.put("#zeroSizeClassArray","");
85 String output = replaceAll(template, vars);
90 * Create a method generator, applicable for RMI stub methods.
92 protected AbstractMethodGenerator createMethodGenerator(Method m)
94 return new RmiMethodGenerator(m, this);
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();
110 b.append("private static final Method met_");
111 b.append(method.method.getName());
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();
134 b.append(method.method.getName());
136 b.append(name(method.method.getDeclaringClass()));
137 b.append(".class.getMethod(");
139 b.append(method.method.getName());
141 if (method.method.getParameterTypes().length == 0)
142 b.append("NO_ARGSc);");
145 b.append("new Class[]\n {\n ");
146 b.append(method.getArgListAsClassArray());
156 * Prepare for the compilation of the next class.
160 addZeroSizeObjecArray = false;
165 * Additional processing of the stub name (nothing to do for JRMP stubs).
167 public String convertStubName(String name)
173 * Override to do nothing.
175 protected boolean outputTie(File fw, Class c)