0 Replies Latest reply on Mar 31, 2005 5:47 PM by Howard Lovatt

    get finds classes it shouldn't

    Howard Lovatt Newbie

      If a class is compiled that does not have a package statement but is mistakenly loaded in Javassist as though it did have a package statement then Javassist will not detect this error and find the class and some methods will give incorrect results but not thow an exception whilst others will throw an exceptoion. EG:

      // NoPackage.java - note no package statement
      public class NoPackage {}
      


      // NoPackageTest.java - note has package statement
      package examples.javassisttests;
      
      import javassist.*;
      
      
      public class NoPackageTest {
       public static void main( final String[] notUsed ) throws NotFoundException {
       final CtClass noPackage = ClassPool.getDefault().get( "examples.javassisttests.NoPackage" );
       System.out.println( "package = " + noPackage.getPackageName() );
       System.out.println( "modifiers = " + noPackage.getModifiers() );
       }
      }
      


      When run, the above produces:

      C:\Personal\Java>java -classpath javassist.jar;. examples.javassisttests.NoPackageTest
      package = examples.javassisttests
      Exception in thread "main" java.lang.RuntimeException: NoPackage in examples/javassisttests/NoPackage.java
      at javassist.CtClassType.getClassFile2(CtClassType.java:191)
      at javassist.CtClassType.getModifiers(CtClassType.java:343)
      at examples.javassisttests.NoPackageTest.main(NoPackageTest.java:10)

      Showing that get found the class even though it shouldn't, getPackageName ran but gave the incorrect package, and getModifiers threw an exception with a less then ideal error message.

      May I ask:

      1. Is a known bug?
      2. Is there a fix?
      3. Any suggestions on how to code round the problem?

      Keep up the good work, Howard.