From 721370a3918f59344b541f9e4cc361826c418818 Mon Sep 17 00:00:00 2001 From: chiba Date: Thu, 27 Oct 2005 11:27:27 +0000 Subject: [PATCH] moved javassist.preproc package to sample/preproc directory. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@213 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- Readme.html | 4 +- build.xml | 4 +- sample/vector/Test.j | 76 ++++---- sample/vector/VectorAssistant.java | 270 ++++++++++++++--------------- 4 files changed, 177 insertions(+), 177 deletions(-) diff --git a/Readme.html b/Readme.html index 14dc8a10..e5dec48f 100644 --- a/Readme.html +++ b/Readme.html @@ -181,12 +181,11 @@ set CLASSPATH=.;javassist.jar

This example shows the use of Javassit for producing a class representing a vector of a given type at compile time. -This is a demonstration of the use of javassist.preproc package.

To run, type the commands:

@@ -287,6 +286,7 @@ see javassist.Dump.

- version 3.1 RC2 in September 7, 2005 diff --git a/build.xml b/build.xml index 6d846887..b7562364 100644 --- a/build.xml +++ b/build.xml @@ -179,8 +179,8 @@ Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.]]> - javassist.preproc.Compiler sample/vector/Test.j - + sample.preproc.Compiler sample/vector/Test.j + diff --git a/sample/vector/Test.j b/sample/vector/Test.j index 6f524c93..4ae7f6b6 100644 --- a/sample/vector/Test.j +++ b/sample/vector/Test.j @@ -1,38 +1,38 @@ -/* - A sample program using sample.vector.VectorAssistant - and the javassist.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 javassist.tool.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)); - } -} +/* + 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)); + } +} diff --git a/sample/vector/VectorAssistant.java b/sample/vector/VectorAssistant.java index 27d5f702..8b721dbf 100644 --- a/sample/vector/VectorAssistant.java +++ b/sample/vector/VectorAssistant.java @@ -1,135 +1,135 @@ -package sample.vector; - -import java.io.IOException; -import javassist.*; -import javassist.preproc.Assistant; - -/** - * This is a Javassist program which produce a new class representing - * vectors of a given type. For example, - * - *

- * - *

requests the Javassist preprocessor to substitute the following - * lines for the original import declaration: - * - *

- * - *

The Javassist preprocessor calls VectorAssistant.assist() - * and produces class intVector equivalent to: - * - *

- * - *

VectorAssistant.assist() uses - * sample.vector.Sample and sample.vector.Sample2 - * as a template to produce the methods add() and - * at(). - */ -public class VectorAssistant implements Assistant { - public final String packageName = "sample.vector."; - - /** - * Calls makeSubclass() and produces a new vector class. - * This method is called by a javassist.preproc.Compiler. - * - * @see javassist.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"; - } -} +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, + * + *

+ * + *

requests the Javassist preprocessor to substitute the following + * lines for the original import declaration: + * + *

+ * + *

The Javassist preprocessor calls VectorAssistant.assist() + * and produces class intVector equivalent to: + * + *

+ * + *

VectorAssistant.assist() uses + * sample.vector.Sample and sample.vector.Sample2 + * as a template to produce the methods add() and + * at(). + */ +public class VectorAssistant implements Assistant { + public final String packageName = "sample.vector."; + + /** + * Calls makeSubclass() and produces a new vector class. + * This method is called by a sample.preproc.Compiler. + * + * @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"; + } +} -- 2.39.5