Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VectorAssistant.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package sample.vector;
  2. import java.io.IOException;
  3. import javassist.*;
  4. import sample.preproc.Assistant;
  5. /**
  6. * This is a Javassist program which produce a new class representing
  7. * vectors of a given type. For example,
  8. *
  9. * <ul>import java.util.Vector by sample.vector.VectorAssistant(int)</ul>
  10. *
  11. * <p>requests the Javassist preprocessor to substitute the following
  12. * lines for the original import declaration:
  13. *
  14. * <ul><pre>
  15. * import java.util.Vector;
  16. * import sample.vector.intVector;
  17. * </pre></ul>
  18. *
  19. * <p>The Javassist preprocessor calls <code>VectorAssistant.assist()</code>
  20. * and produces class <code>intVector</code> equivalent to:
  21. *
  22. * <ul><pre>
  23. * package sample.vector;
  24. *
  25. * public class intVector extends Vector {
  26. * pubilc void add(int value) {
  27. * addElement(new Integer(value));
  28. * }
  29. *
  30. * public int at(int index) {
  31. * return elementAt(index).intValue();
  32. * }
  33. * }
  34. * </pre></ul>
  35. *
  36. * <p><code>VectorAssistant.assist()</code> uses
  37. * <code>sample.vector.Sample</code> and <code>sample.vector.Sample2</code>
  38. * as a template to produce the methods <code>add()</code> and
  39. * <code>at()</code>.
  40. */
  41. public class VectorAssistant implements Assistant {
  42. public final String packageName = "sample.vector.";
  43. /**
  44. * Calls <code>makeSubclass()</code> and produces a new vector class.
  45. * This method is called by a <code>sample.preproc.Compiler</code>.
  46. *
  47. * @see sample.preproc.Compiler
  48. */
  49. public CtClass[] assist(ClassPool pool, String vec, String[] args)
  50. throws CannotCompileException
  51. {
  52. if (args.length != 1)
  53. throw new CannotCompileException(
  54. "VectorAssistant receives a single argument.");
  55. try {
  56. CtClass subclass;
  57. CtClass elementType = pool.get(args[0]);
  58. if (elementType.isPrimitive())
  59. subclass = makeSubclass2(pool, elementType);
  60. else
  61. subclass = makeSubclass(pool, elementType);
  62. CtClass[] results = { subclass, pool.get(vec) };
  63. return results;
  64. }
  65. catch (NotFoundException e) {
  66. throw new CannotCompileException(e);
  67. }
  68. catch (IOException e) {
  69. throw new CannotCompileException(e);
  70. }
  71. }
  72. /**
  73. * Produces a new vector class. This method does not work if
  74. * the element type is a primitive type.
  75. *
  76. * @param type the type of elements
  77. */
  78. public CtClass makeSubclass(ClassPool pool, CtClass type)
  79. throws CannotCompileException, NotFoundException, IOException
  80. {
  81. CtClass vec = pool.makeClass(makeClassName(type));
  82. vec.setSuperclass(pool.get("java.util.Vector"));
  83. CtClass c = pool.get("sample.vector.Sample");
  84. CtMethod addmethod = c.getDeclaredMethod("add");
  85. CtMethod atmethod = c.getDeclaredMethod("at");
  86. ClassMap map = new ClassMap();
  87. map.put("sample.vector.X", type.getName());
  88. vec.addMethod(CtNewMethod.copy(addmethod, "add", vec, map));
  89. vec.addMethod(CtNewMethod.copy(atmethod, "at", vec, map));
  90. vec.writeFile();
  91. return vec;
  92. }
  93. /**
  94. * Produces a new vector class. This uses wrapped methods so that
  95. * the element type can be a primitive type.
  96. *
  97. * @param type the type of elements
  98. */
  99. public CtClass makeSubclass2(ClassPool pool, CtClass type)
  100. throws CannotCompileException, NotFoundException, IOException
  101. {
  102. CtClass vec = pool.makeClass(makeClassName(type));
  103. vec.setSuperclass(pool.get("java.util.Vector"));
  104. CtClass c = pool.get("sample.vector.Sample2");
  105. CtMethod addmethod = c.getDeclaredMethod("add");
  106. CtMethod atmethod = c.getDeclaredMethod("at");
  107. CtClass[] args1 = { type };
  108. CtClass[] args2 = { CtClass.intType };
  109. CtMethod m
  110. = CtNewMethod.wrapped(CtClass.voidType, "add", args1,
  111. null, addmethod, null, vec);
  112. vec.addMethod(m);
  113. m = CtNewMethod.wrapped(type, "at", args2,
  114. null, atmethod, null, vec);
  115. vec.addMethod(m);
  116. vec.writeFile();
  117. return vec;
  118. }
  119. private String makeClassName(CtClass type) {
  120. return packageName + type.getSimpleName() + "Vector";
  121. }
  122. }