3 Replies Latest reply on Nov 27, 2005 10:11 AM by johncoleman

    ClassFile write creates bad byte code

    johncoleman

      I've followed this example from notes:

      BufferedInputStream fin
      = new BufferedInputStream(new FileInputStream("Point.class"));
      ClassFile cf = new ClassFile(new DataInputStream(fin));


      And then used the write method to create the new class. However, tad throws an exception, so it appears the bytecode is invalid.

      Here is my code:

      InputStream is = getClass().getResourceAsStream(resourceName);
      DataInputStream dis = new DataInputStream(is);
      ClassFile cf = new ClassFile(dis);
      cf.addInterface(interfaceName);
      DataOutputStream dos = new DataOutputStream(new FileOutputStream(
      rtDirName + File.separator + "new" + ".class"));
      cf.write(dos);
      dos.flush();
      dos.close();
      dis.close();

      Any ideas what is wrong? If I use the CtClass regular methods to augment the code, it creates valid byte code.