Creating a Constructor in BCEL

Discussion in 'Java' started by oulan bator, Jan 25, 2006.

  1. oulan bator

    oulan bator Guest

    hi,

    I'm trying to write a class using BCEL from scratch and get error

    Exception in thread "main" java.lang.InstantiationException:
    org.apache.bcel.generic.ClassGen
    at java.lang.Class.newInstance0(Unknown Source)

    when I'm calling newInstance on this class. I guess that it's because I
    did'nt write a default constructor.

    How can I create a constructor in a class file using BCEL ?
    Constructors are neither methods nor have a dedicated gen in BCEL api
    like a constructor

    thanks
     
    oulan bator, Jan 25, 2006
    #1
    1. Advertising

  2. oulan bator

    oulan bator Guest

    I've got a partial answer to my question:

    ClassGen has a method addEmptyConstructor(Constants.ACC_PUBLIC);

    but it does not answer the whole one.
     
    oulan bator, Jan 25, 2006
    #2
    1. Advertising

  3. oulan bator

    Chris Uppal Guest

    oulan bator wrote:

    > ClassGen has a method addEmptyConstructor(Constants.ACC_PUBLIC);


    It's worth taking a look at the source for that method.


    > but it does not answer the whole one.


    From the point of view of the JVM a constructor is just a method with a
    specific name ("<init>") and a specific return type (void). It is also
    required to call another constructor of the same class or its superclass before
    it does anything much else. You'll see that the code for
    ClassGen.addEmptyConstructor() generates a method which satisfies those
    conditions.

    -- chris
     
    Chris Uppal, Jan 25, 2006
    #3
  4. oulan bator

    oulan bator Guest

    thanks, I've got my answers....

    and BCELifier is a good tool too !!
     
    oulan bator, Jan 25, 2006
    #4
  5. oulan bator wrote:
    >
    > when I'm calling newInstance on this class. I guess that it's because I
    > did'nt write a default constructor.


    If you save it to a file and use javap -c, you can see whether a
    constructor has been included or not.

    > How can I create a constructor in a class file using BCEL ?
    > Constructors are neither methods nor have a dedicated gen in BCEL api
    > like a constructor


    Constructors in the class file format are instance methods named
    "<init>". The method body (except for java.lang.Object) must
    invokespecial an "<init>" of the superclass, and all the usual restrictions.

    Again, you can see constructors in real classes using javap -c. Also, I
    believe BCEL contains a demo that takes a class file and produces (bad)
    BCEL code to reconstruct it.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 25, 2006
    #5
  6. oulan bator

    oulan bator Guest

    all right, thanks for the answers.

    If somebody is facing my problem, it does not come from any kind of
    constructor problems, but is due to the used of ClassGen.getClass()
    method instead of dump(File).
    I didn't manage to load the created class without writing my own
    classloader.
     
    oulan bator, Jan 26, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Zangl

    BCEL and IfThenElse construct

    Thomas Zangl, May 4, 2005, in forum: Java
    Replies:
    1
    Views:
    455
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 4, 2005
  2. Thomas Zangl

    [BCEL] If then else construct - again

    Thomas Zangl, May 13, 2005, in forum: Java
    Replies:
    4
    Views:
    851
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 13, 2005
  3. Replies:
    1
    Views:
    808
    Thomas Hawtin
    Sep 7, 2005
  4. Francesco Devittori

    ASM (vs. BCEL) - can I do this?

    Francesco Devittori, Dec 20, 2005, in forum: Java
    Replies:
    2
    Views:
    1,343
    Francesco Devittori
    Dec 21, 2005
  5. Boris Gorjan

    Instrumentation + BCEL | ASM

    Boris Gorjan, Jun 6, 2006, in forum: Java
    Replies:
    18
    Views:
    1,456
    Boris Gorjan
    Jun 12, 2006
Loading...

Share This Page