Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Creating a Constructor in BCEL

Thread view: 
oulan bator - 25 Jan 2006 14:13 GMT
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 - 25 Jan 2006 15:41 GMT
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 - 25 Jan 2006 15:59 GMT
> 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 - 25 Jan 2006 16:22 GMT
thanks, I've got my answers....

and BCELifier is a good tool too !!
Thomas Hawtin - 25 Jan 2006 16:54 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

oulan bator - 26 Jan 2006 21:53 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.