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 !!
> 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.