-/*\r
- A sample program using sample.vector.VectorAssistant\r
- and the javassist.preproc package.\r
-\r
- This automatically produces the classes representing vectors of integer\r
- and vectors of java.lang.String.\r
-\r
- To compile and run this program, do as follows:\r
-\r
- % java javassist.tool.Compiler sample/vector/Test.j\r
- % javac sample/vector/Test.java\r
- % java sample.vector.Test\r
-\r
- The first line produces one source file (sample/Test.java) and\r
- two class files (sample/vector/intVector.class and\r
- sample/vector/StringVector.class).\r
-*/\r
-\r
-package sample.vector;\r
-\r
-import java.util.Vector by sample.vector.VectorAssistant(java.lang.String);\r
-import java.util.Vector by sample.vector.VectorAssistant(int);\r
-\r
-public class Test {\r
- public static void main(String[] args) {\r
- intVector iv = new intVector();\r
- iv.add(3);\r
- iv.add(4);\r
- for (int i = 0; i < iv.size(); ++i)\r
- System.out.println(iv.at(i));\r
-\r
- StringVector sv = new StringVector();\r
- sv.add("foo");\r
- sv.add("bar");\r
- for (int i = 0; i < sv.size(); ++i)\r
- System.out.println(sv.at(i));\r
- }\r
-}\r
+/*
+ A sample program using sample.vector.VectorAssistant
+ and the sample.preproc package.
+
+ This automatically produces the classes representing vectors of integer
+ and vectors of java.lang.String.
+
+ To compile and run this program, do as follows:
+
+ % java sample.preproc.Compiler sample/vector/Test.j
+ % javac sample/vector/Test.java
+ % java sample.vector.Test
+
+ The first line produces one source file (sample/Test.java) and
+ two class files (sample/vector/intVector.class and
+ sample/vector/StringVector.class).
+*/
+
+package sample.vector;
+
+import java.util.Vector by sample.vector.VectorAssistant(java.lang.String);
+import java.util.Vector by sample.vector.VectorAssistant(int);
+
+public class Test {
+ public static void main(String[] args) {
+ intVector iv = new intVector();
+ iv.add(3);
+ iv.add(4);
+ for (int i = 0; i < iv.size(); ++i)
+ System.out.println(iv.at(i));
+
+ StringVector sv = new StringVector();
+ sv.add("foo");
+ sv.add("bar");
+ for (int i = 0; i < sv.size(); ++i)
+ System.out.println(sv.at(i));
+ }
+}
-package sample.vector;\r
-\r
-import java.io.IOException;\r
-import javassist.*;\r
-import javassist.preproc.Assistant;\r
-\r
-/**\r
- * This is a Javassist program which produce a new class representing\r
- * vectors of a given type. For example,\r
- *\r
- * <ul>import java.util.Vector by sample.vector.VectorAssistant(int)</ul>\r
- *\r
- * <p>requests the Javassist preprocessor to substitute the following\r
- * lines for the original import declaration:\r
- *\r
- * <ul><pre>\r
- * import java.util.Vector;\r
- * import sample.vector.intVector;\r
- * </pre></ul>\r
- *\r
- * <p>The Javassist preprocessor calls <code>VectorAssistant.assist()</code>\r
- * and produces class <code>intVector</code> equivalent to:\r
- *\r
- * <ul><pre>\r
- * package sample.vector;\r
- *\r
- * public class intVector extends Vector {\r
- * pubilc void add(int value) {\r
- * addElement(new Integer(value));\r
- * }\r
- *\r
- * public int at(int index) {\r
- * return elementAt(index).intValue();\r
- * }\r
- * }\r
- * </pre></ul>\r
- *\r
- * <p><code>VectorAssistant.assist()</code> uses\r
- * <code>sample.vector.Sample</code> and <code>sample.vector.Sample2</code>\r
- * as a template to produce the methods <code>add()</code> and\r
- * <code>at()</code>.\r
- */\r
-public class VectorAssistant implements Assistant {\r
- public final String packageName = "sample.vector.";\r
-\r
- /**\r
- * Calls <code>makeSubclass()</code> and produces a new vector class.\r
- * This method is called by a <code>javassist.preproc.Compiler</code>.\r
- *\r
- * @see javassist.preproc.Compiler\r
- */\r
- public CtClass[] assist(ClassPool pool, String vec, String[] args)\r
- throws CannotCompileException\r
- {\r
- if (args.length != 1)\r
- throw new CannotCompileException(\r
- "VectorAssistant receives a single argument.");\r
-\r
- try {\r
- CtClass subclass;\r
- CtClass elementType = pool.get(args[0]);\r
- if (elementType.isPrimitive())\r
- subclass = makeSubclass2(pool, elementType);\r
- else\r
- subclass = makeSubclass(pool, elementType);\r
-\r
- CtClass[] results = { subclass, pool.get(vec) };\r
- return results;\r
- }\r
- catch (NotFoundException e) {\r
- throw new CannotCompileException(e);\r
- }\r
- catch (IOException e) {\r
- throw new CannotCompileException(e);\r
- }\r
- }\r
-\r
- /**\r
- * Produces a new vector class. This method does not work if\r
- * the element type is a primitive type.\r
- *\r
- * @param type the type of elements\r
- */\r
- public CtClass makeSubclass(ClassPool pool, CtClass type)\r
- throws CannotCompileException, NotFoundException, IOException\r
- {\r
- CtClass vec = pool.makeClass(makeClassName(type));\r
- vec.setSuperclass(pool.get("java.util.Vector"));\r
-\r
- CtClass c = pool.get("sample.vector.Sample");\r
- CtMethod addmethod = c.getDeclaredMethod("add");\r
- CtMethod atmethod = c.getDeclaredMethod("at");\r
-\r
- ClassMap map = new ClassMap();\r
- map.put("sample.vector.X", type.getName());\r
-\r
- vec.addMethod(CtNewMethod.copy(addmethod, "add", vec, map));\r
- vec.addMethod(CtNewMethod.copy(atmethod, "at", vec, map));\r
- vec.writeFile();\r
- return vec;\r
- }\r
-\r
- /**\r
- * Produces a new vector class. This uses wrapped methods so that\r
- * the element type can be a primitive type.\r
- *\r
- * @param type the type of elements\r
- */\r
- public CtClass makeSubclass2(ClassPool pool, CtClass type)\r
- throws CannotCompileException, NotFoundException, IOException\r
- {\r
- CtClass vec = pool.makeClass(makeClassName(type));\r
- vec.setSuperclass(pool.get("java.util.Vector"));\r
-\r
- CtClass c = pool.get("sample.vector.Sample2");\r
- CtMethod addmethod = c.getDeclaredMethod("add");\r
- CtMethod atmethod = c.getDeclaredMethod("at");\r
-\r
- CtClass[] args1 = { type };\r
- CtClass[] args2 = { CtClass.intType };\r
- CtMethod m\r
- = CtNewMethod.wrapped(CtClass.voidType, "add", args1,\r
- null, addmethod, null, vec);\r
- vec.addMethod(m);\r
- m = CtNewMethod.wrapped(type, "at", args2,\r
- null, atmethod, null, vec);\r
- vec.addMethod(m);\r
- vec.writeFile();\r
- return vec;\r
- }\r
-\r
- private String makeClassName(CtClass type) {\r
- return packageName + type.getSimpleName() + "Vector";\r
- }\r
-}\r
+package sample.vector;
+
+import java.io.IOException;
+import javassist.*;
+import sample.preproc.Assistant;
+
+/**
+ * This is a Javassist program which produce a new class representing
+ * vectors of a given type. For example,
+ *
+ * <ul>import java.util.Vector by sample.vector.VectorAssistant(int)</ul>
+ *
+ * <p>requests the Javassist preprocessor to substitute the following
+ * lines for the original import declaration:
+ *
+ * <ul><pre>
+ * import java.util.Vector;
+ * import sample.vector.intVector;
+ * </pre></ul>
+ *
+ * <p>The Javassist preprocessor calls <code>VectorAssistant.assist()</code>
+ * and produces class <code>intVector</code> equivalent to:
+ *
+ * <ul><pre>
+ * package sample.vector;
+ *
+ * public class intVector extends Vector {
+ * pubilc void add(int value) {
+ * addElement(new Integer(value));
+ * }
+ *
+ * public int at(int index) {
+ * return elementAt(index).intValue();
+ * }
+ * }
+ * </pre></ul>
+ *
+ * <p><code>VectorAssistant.assist()</code> uses
+ * <code>sample.vector.Sample</code> and <code>sample.vector.Sample2</code>
+ * as a template to produce the methods <code>add()</code> and
+ * <code>at()</code>.
+ */
+public class VectorAssistant implements Assistant {
+ public final String packageName = "sample.vector.";
+
+ /**
+ * Calls <code>makeSubclass()</code> and produces a new vector class.
+ * This method is called by a <code>sample.preproc.Compiler</code>.
+ *
+ * @see sample.preproc.Compiler
+ */
+ public CtClass[] assist(ClassPool pool, String vec, String[] args)
+ throws CannotCompileException
+ {
+ if (args.length != 1)
+ throw new CannotCompileException(
+ "VectorAssistant receives a single argument.");
+
+ try {
+ CtClass subclass;
+ CtClass elementType = pool.get(args[0]);
+ if (elementType.isPrimitive())
+ subclass = makeSubclass2(pool, elementType);
+ else
+ subclass = makeSubclass(pool, elementType);
+
+ CtClass[] results = { subclass, pool.get(vec) };
+ return results;
+ }
+ catch (NotFoundException e) {
+ throw new CannotCompileException(e);
+ }
+ catch (IOException e) {
+ throw new CannotCompileException(e);
+ }
+ }
+
+ /**
+ * Produces a new vector class. This method does not work if
+ * the element type is a primitive type.
+ *
+ * @param type the type of elements
+ */
+ public CtClass makeSubclass(ClassPool pool, CtClass type)
+ throws CannotCompileException, NotFoundException, IOException
+ {
+ CtClass vec = pool.makeClass(makeClassName(type));
+ vec.setSuperclass(pool.get("java.util.Vector"));
+
+ CtClass c = pool.get("sample.vector.Sample");
+ CtMethod addmethod = c.getDeclaredMethod("add");
+ CtMethod atmethod = c.getDeclaredMethod("at");
+
+ ClassMap map = new ClassMap();
+ map.put("sample.vector.X", type.getName());
+
+ vec.addMethod(CtNewMethod.copy(addmethod, "add", vec, map));
+ vec.addMethod(CtNewMethod.copy(atmethod, "at", vec, map));
+ vec.writeFile();
+ return vec;
+ }
+
+ /**
+ * Produces a new vector class. This uses wrapped methods so that
+ * the element type can be a primitive type.
+ *
+ * @param type the type of elements
+ */
+ public CtClass makeSubclass2(ClassPool pool, CtClass type)
+ throws CannotCompileException, NotFoundException, IOException
+ {
+ CtClass vec = pool.makeClass(makeClassName(type));
+ vec.setSuperclass(pool.get("java.util.Vector"));
+
+ CtClass c = pool.get("sample.vector.Sample2");
+ CtMethod addmethod = c.getDeclaredMethod("add");
+ CtMethod atmethod = c.getDeclaredMethod("at");
+
+ CtClass[] args1 = { type };
+ CtClass[] args2 = { CtClass.intType };
+ CtMethod m
+ = CtNewMethod.wrapped(CtClass.voidType, "add", args1,
+ null, addmethod, null, vec);
+ vec.addMethod(m);
+ m = CtNewMethod.wrapped(type, "at", args2,
+ null, atmethod, null, vec);
+ vec.addMethod(m);
+ vec.writeFile();
+ return vec;
+ }
+
+ private String makeClassName(CtClass type) {
+ return packageName + type.getSimpleName() + "Vector";
+ }
+}