You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StubGenerator.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.tools.rmi;
  17. import javassist.*;
  18. import java.lang.reflect.Method;
  19. import java.util.Hashtable;
  20. import javassist.CtMethod.ConstParameter;
  21. /**
  22. * A stub-code generator. It is used for producing a proxy class.
  23. *
  24. * <p>The proxy class for class A is as follows:
  25. *
  26. * <pre>public class A implements Proxy, Serializable {
  27. * private ObjectImporter importer;
  28. * private int objectId;
  29. * public int _getObjectId() { return objectId; }
  30. * public A(ObjectImporter oi, int id) {
  31. * importer = oi; objectId = id;
  32. * }
  33. *
  34. * ... the same methods that the original class A declares ...
  35. * }</pre>
  36. *
  37. * <p>Instances of the proxy class is created by an
  38. * <code>ObjectImporter</code> object.
  39. */
  40. public class StubGenerator implements Translator {
  41. private static final String fieldImporter = "importer";
  42. private static final String fieldObjectId = "objectId";
  43. private static final String accessorObjectId = "_getObjectId";
  44. private static final String sampleClass = "javassist.tools.rmi.Sample";
  45. private ClassPool classPool;
  46. private Hashtable proxyClasses;
  47. private CtMethod forwardMethod;
  48. private CtMethod forwardStaticMethod;
  49. private CtClass[] proxyConstructorParamTypes;
  50. private CtClass[] interfacesForProxy;
  51. private CtClass[] exceptionForProxy;
  52. /**
  53. * Constructs a stub-code generator.
  54. */
  55. public StubGenerator() {
  56. proxyClasses = new Hashtable();
  57. }
  58. /**
  59. * Initializes the object.
  60. * This is a method declared in javassist.Translator.
  61. *
  62. * @see javassist.Translator#start(ClassPool)
  63. */
  64. public void start(ClassPool pool) throws NotFoundException {
  65. classPool = pool;
  66. CtClass c = pool.get(sampleClass);
  67. forwardMethod = c.getDeclaredMethod("forward");
  68. forwardStaticMethod = c.getDeclaredMethod("forwardStatic");
  69. proxyConstructorParamTypes
  70. = pool.get(new String[] { "javassist.tools.rmi.ObjectImporter",
  71. "int" });
  72. interfacesForProxy
  73. = pool.get(new String[] { "java.io.Serializable",
  74. "javassist.tools.rmi.Proxy" });
  75. exceptionForProxy
  76. = new CtClass[] { pool.get("javassist.tools.rmi.RemoteException") };
  77. }
  78. /**
  79. * Does nothing.
  80. * This is a method declared in javassist.Translator.
  81. * @see javassist.Translator#onLoad(ClassPool,String)
  82. */
  83. public void onLoad(ClassPool pool, String classname) {}
  84. /**
  85. * Returns <code>true</code> if the specified class is a proxy class
  86. * recorded by <code>makeProxyClass()</code>.
  87. *
  88. * @param name a fully-qualified class name
  89. */
  90. public boolean isProxyClass(String name) {
  91. return proxyClasses.get(name) != null;
  92. }
  93. /**
  94. * Makes a proxy class. The produced class is substituted
  95. * for the original class.
  96. *
  97. * @param clazz the class referenced
  98. * through the proxy class.
  99. * @return <code>false</code> if the proxy class
  100. * has been already produced.
  101. */
  102. public synchronized boolean makeProxyClass(Class clazz)
  103. throws CannotCompileException, NotFoundException
  104. {
  105. String classname = clazz.getName();
  106. if (proxyClasses.get(classname) != null)
  107. return false;
  108. else {
  109. CtClass ctclazz = produceProxyClass(classPool.get(classname),
  110. clazz);
  111. proxyClasses.put(classname, ctclazz);
  112. modifySuperclass(ctclazz);
  113. return true;
  114. }
  115. }
  116. private CtClass produceProxyClass(CtClass orgclass, Class orgRtClass)
  117. throws CannotCompileException, NotFoundException
  118. {
  119. int modify = orgclass.getModifiers();
  120. if (Modifier.isAbstract(modify) || Modifier.isNative(modify)
  121. || !Modifier.isPublic(modify))
  122. throw new CannotCompileException(orgclass.getName()
  123. + " must be public, non-native, and non-abstract.");
  124. CtClass proxy = classPool.makeClass(orgclass.getName(),
  125. orgclass.getSuperclass());
  126. proxy.setInterfaces(interfacesForProxy);
  127. CtField f
  128. = new CtField(classPool.get("javassist.tools.rmi.ObjectImporter"),
  129. fieldImporter, proxy);
  130. f.setModifiers(Modifier.PRIVATE);
  131. proxy.addField(f, CtField.Initializer.byParameter(0));
  132. f = new CtField(CtClass.intType, fieldObjectId, proxy);
  133. f.setModifiers(Modifier.PRIVATE);
  134. proxy.addField(f, CtField.Initializer.byParameter(1));
  135. proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));
  136. proxy.addConstructor(CtNewConstructor.defaultConstructor(proxy));
  137. CtConstructor cons
  138. = CtNewConstructor.skeleton(proxyConstructorParamTypes,
  139. null, proxy);
  140. proxy.addConstructor(cons);
  141. try {
  142. addMethods(proxy, orgRtClass.getMethods());
  143. return proxy;
  144. }
  145. catch (SecurityException e) {
  146. throw new CannotCompileException(e);
  147. }
  148. }
  149. private CtClass toCtClass(Class rtclass) throws NotFoundException {
  150. String name;
  151. if (!rtclass.isArray())
  152. name = rtclass.getName();
  153. else {
  154. StringBuffer sbuf = new StringBuffer();
  155. do {
  156. sbuf.append("[]");
  157. rtclass = rtclass.getComponentType();
  158. } while(rtclass.isArray());
  159. sbuf.insert(0, rtclass.getName());
  160. name = sbuf.toString();
  161. }
  162. return classPool.get(name);
  163. }
  164. private CtClass[] toCtClass(Class[] rtclasses) throws NotFoundException {
  165. int n = rtclasses.length;
  166. CtClass[] ctclasses = new CtClass[n];
  167. for (int i = 0; i < n; ++i)
  168. ctclasses[i] = toCtClass(rtclasses[i]);
  169. return ctclasses;
  170. }
  171. /* ms must not be an array of CtMethod. To invoke a method ms[i]
  172. * on a server, a client must send i to the server.
  173. */
  174. private void addMethods(CtClass proxy, Method[] ms)
  175. throws CannotCompileException, NotFoundException
  176. {
  177. CtMethod wmethod;
  178. for (int i = 0; i < ms.length; ++i) {
  179. Method m = ms[i];
  180. int mod = m.getModifiers();
  181. if (m.getDeclaringClass() != Object.class
  182. && !Modifier.isFinal(mod))
  183. if (Modifier.isPublic(mod)) {
  184. CtMethod body;
  185. if (Modifier.isStatic(mod))
  186. body = forwardStaticMethod;
  187. else
  188. body = forwardMethod;
  189. wmethod
  190. = CtNewMethod.wrapped(toCtClass(m.getReturnType()),
  191. m.getName(),
  192. toCtClass(m.getParameterTypes()),
  193. exceptionForProxy,
  194. body,
  195. ConstParameter.integer(i),
  196. proxy);
  197. wmethod.setModifiers(mod);
  198. proxy.addMethod(wmethod);
  199. }
  200. else if (!Modifier.isProtected(mod)
  201. && !Modifier.isPrivate(mod))
  202. // if package method
  203. throw new CannotCompileException(
  204. "the methods must be public, protected, or private.");
  205. }
  206. }
  207. /**
  208. * Adds a default constructor to the super classes.
  209. */
  210. private void modifySuperclass(CtClass orgclass)
  211. throws CannotCompileException, NotFoundException
  212. {
  213. CtClass superclazz;
  214. for (;; orgclass = superclazz) {
  215. superclazz = orgclass.getSuperclass();
  216. if (superclazz == null)
  217. break;
  218. try {
  219. superclazz.getDeclaredConstructor(null);
  220. break; // the constructor with no arguments is found.
  221. }
  222. catch (NotFoundException e) {
  223. }
  224. superclazz.addConstructor(
  225. CtNewConstructor.defaultConstructor(superclazz));
  226. }
  227. }
  228. }