Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Creating a Constructor in BCEL (http://www.velocityreviews.com/forums/t149654-creating-a-constructor-in-bcel.html)

oulan bator 01-25-2006 02:13 PM

Creating a Constructor in BCEL
 
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 01-25-2006 03:41 PM

Re: Creating a Constructor in BCEL
 
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.


Chris Uppal 01-25-2006 03:59 PM

Re: Creating a Constructor in BCEL
 
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



oulan bator 01-25-2006 04:22 PM

Re: Creating a Constructor in BCEL
 
thanks, I've got my answers....

and BCELifier is a good tool too !!


Thomas Hawtin 01-25-2006 05:08 PM

Re: Creating a Constructor in BCEL
 
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/

oulan bator 01-26-2006 09:53 PM

Re: Creating a Constructor in BCEL
 
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.



All times are GMT. The time now is 05:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.