First two appologies: I don't seem to be able to watch this thread so I missed
your post and secondly my test class is a little long :( . Take a look at lines
75 to 90 which demonstrate the problem I am having with array class literals
and also with testing for null on primitive arrays (see seperate post to this
forum for null problems).
| package examples.javassisttests.poolget;
|
| import javassist.*;
| import pec.compile.*;
| import java.io.*;
| import java.lang.reflect.*;
|
| public class Test {
| static class Y {}
| static class X extends Y {}
| public static void main( final String[] notUsed ) throws Exception {
| // Test of assignment compatability
| final X[] xs = new X[ 1 ];
| final Y[] ys = xs;
| ys[ 0 ] = new X(); // OK
| // ys[ 0 ] = new Y(); // ArrayStoreException
| final ClassPool pool = ClassPool.getDefault();
| final CtClass intClass = pool.get( "int" );
| final CtClass longClass = pool.get( "long" );
| System.out.println( "Class: " + long.class.isAssignableFrom(
int.class ) );
| System.out.println( "CtClass: " + longClass.subtypeOf( intClass ) );
|
| // get classes
| System.out.println( "int = " + intClass );
| final CtClass intArrayClass = pool.get( "int[]" );
| System.out.println( "int[] = " + intArrayClass );
| final CtClass objectArrayClass = pool.get( "java.lang.Object[]" );
| System.out.println( "Object[] = " + objectArrayClass );
|
| // class definition (AbstractTest)
| final CtClass clazz = pool.makeClass(
"examples.javassisttests.poolget.AbstractTest" );
| clazz.setModifiers( javassist.Modifier.ABSTRACT );
| clazz.setModifiers( javassist.Modifier.PUBLIC );
|
| // protected constructor definition
| final CtConstructor constructor =
CtNewConstructor.defaultConstructor(
| clazz );
| constructor.setModifiers( javassist.Modifier.PROTECTED );
| clazz.addConstructor( constructor );
|
| // abstract method definition
| final CtClass[] arguments = new CtClass[] { intClass,
intArrayClass, objectArrayClass };
| final CtMethod method = CtNewMethod.abstractMethod(
| intClass,
| "f",
| arguments,
| null,
| clazz );
| clazz.addMethod( method );
|
| // write the abstract class
| clazz.writeFile( "C:\\Personal\\Java" );
|
| // print the abstract class
| final Class classOfClazz = Class.forName(
"examples.javassisttests.poolget.AbstractTest" );
| System.out.println();
| Utilities.printSignature( classOfClazz, null );
|
| // class definition (DerivedTest)
| final CtClass derivedClass = pool.makeClass(
|
"examples.javassisttests.poolget.DerivedTest", clazz );
| derivedClass.setModifiers( javassist.Modifier.PUBLIC );
|
| // public constructor definition
| final CtConstructor derivedConstructor =
CtNewConstructor.defaultConstructor(
| derivedClass );
| derivedClass.addConstructor( derivedConstructor );
|
| // method definition
| final CtMethod derivedMethod = CtNewMethod.make(
| intClass,
| "f",
| arguments,
| null,
| /*"{\n" +
| "if ( $2 != null )\n" +
| "return $2.length;\n" +
| "if ( $3 instanceof
Object[].class )\n" +
| "return $3.length;\n" +
| "return $1;\n" +
| "}",*/
| "{\n" +
| "if ( System.identityHashCode(
$2 ) != 0 )\n" +
| "return $2.length;\n" +
| "Class array =
java.lang.reflect.Array.newInstance( Object.class, 0 ).getClass();\n" +
| "if ( array.isInstance( $3 )
)\n" +
| "return $3.length;\n" +
| "return