1 Reply Latest reply on May 25, 2006 11:51 AM by Chang Zhou

    Why an abstract class generated?

    Chang Zhou Newbie

      I try to create a class BasicType
      public class BasicType implements java.io.Serializable{
      String _ISBN;

      public java.lang.String getISBN();
      public void setISBN(java.lang.String);
      public BasicType();
      }

      But I always get an abstract class, and the interface Serializable is not correct.
      C:\>javap BasicType
      Compiled from "FrameType.java"
      public abstract class BasicType extends java.lang.Object{
      public java.lang.String getISBN();
      public void setISBN(java.lang.String);
      public BasicType();
      }

      Anybody know why?

      Here are my codes:
      try {
      ClassPool pool = ClassPool.getDefault();
      pool.importPackage("java.io.Serializable");

      CtClass cls = pool.makeClass("BasicType");
      cls.addInterface(pool.get("java.io.Serializable"));

      CtField cfld = CtField.make("private java.lang.String _ISBN;", cls);
      cls.addField(cfld);

      CtMethod m = CtNewMethod.make("public java.lang.String getISBN();", cls);
      cls.addMethod(m);
      m.setBody("return _ISBN;");
      CtMethod m1 = CtNewMethod.make("public void setISBN(java.lang.String isbn);", cls);
      cls.addMethod(m1);
      m1.setBody(" _ISBN = $1;");

      cls.writeFile("c:\\");
      }
      catch (CannotCompileException e) {
      System.err.println("Cannot Compile Exception error");
      }
      catch (NotFoundException e) {
      System.err.println("Not Found Exception error");
      }
      catch (IOException e) {
      System.err.println("IO Exception error");
      }

        • 1. Re: Why an abstract class generated?
          Chang Zhou Newbie

          Find out the reason. I add the method too early. I add the method before I add body part of the method.

          CtMethod m = CtNewMethod.make("public java.lang.String getISBN();", cls);
          cls.addMethod(m); ///wrong, too early to do so.
          m.setBody("return _ISBN;");

          The correct way is:
          CtMethod m = CtNewMethod.make("public java.lang.String getISBN();", cls);
          m.setBody("return _ISBN;");
          cls.addMethod(m); ///Now it's not an abstract method