Menu

#4 help MethodGen and constantPool

open
nobody
None
5
2005-04-13
2005-04-13
Anonymous
No

Hello,

I want to create a class from 2 different classes.

I begin just to create a classe from an existing classe
using ClassGen and MethodGen
and copying its fields and its methods in the ClassGen.

When I add method to the ClassGen, the constant pool
don't correspond.
References used in the added method are not the good
one in the constant pool.

How can I make the constant pool right?

You can see above the instructions list for the method
and the constant pool
for the class C1:

package p3;

class A {

int a1;

A() {
a1 = 1;
}

public void paint(){
}
}

-------------------------------------------

Methods ...
void <init>()
list inst : <init>
0: aload_0[42](1)
1: invokespecial[183](3) 11
4: aload_0[42](1)
5: iconst_1[4](1)
6: putfield[181](3) 13
9: return[177](1)

public void paint()
list inst : paint
0: return[177](1)

constant pool ...
1)CONSTANT_Utf8[1]("AB")
2)CONSTANT_Class[7](name_index = 1)
3)CONSTANT_Utf8[1]("java/lang/Object")
4)CONSTANT_Class[7](name_index = 3)
5)CONSTANT_Utf8[1]("a1")
6)CONSTANT_Utf8[1]("I")
7)CONSTANT_NameAndType[12](name_index = 5,
signature_index = 6)
8)CONSTANT_Fieldref[9](class_index = 2,
name_and_type_index = 7)
9)CONSTANT_Utf8[1]("<init>")
10)CONSTANT_Utf8[1]("()V")
11)CONSTANT_NameAndType[12](name_index = 9,
signature_index = 10)
12)CONSTANT_Methodref[10](class_index = 2,
name_and_type_index = 11)
13)CONSTANT_Utf8[1]("this")
14)CONSTANT_Utf8[1]("Lp3/A;")
15)CONSTANT_Utf8[1]("LocalVariableTable")
16)CONSTANT_Utf8[1]("LineNumberTable")
17)CONSTANT_Utf8[1]("Code")
18)CONSTANT_Utf8[1]("paint")
19)CONSTANT_NameAndType[12](name_index = 18,
signature_index = 10)
20)CONSTANT_Methodref[10](class_index = 2,
name_and_type_index = 19)

---------------------------------------

When I decompile the class that I create, instead of the
method m1 there is this comment:
// JavaClassFileOutputException:
get_constant: invalid tag

Some help will be great appreciate!

Here is my code:

c1 is a java.lang.Class

public void integre() {

String nomGen = "tests/bin/gen/"
+ C1.getName()
+ "_.class";

JavaClass clazz1 = Repository.lookupClass
(c1.getCl().getName());

ClassGen cg1 = new ClassGen(clazz1);
ConstantPoolGen cp1 = cg1.getConstantPool();

String nom = C1.getName() + "_.class";
ClassGen _cg = new ClassGen(nom,

"java.lang.Object",
null,

Constants.ACC_PUBLIC,
null);

ConstantPoolGen _cp = _cg.getConstantPool
();

System.out.println("Fields ... ");

Field[] tfields = cg1.getFields();
for (int i = 0; i < tfields.length; i++) {
FieldGen fg = new FieldGen(tfields
[i], _cp);
_cg.addField(fg.getField());
_cp.addFieldref
(_cg.getClassName(),fg.getName(),fg.getSignature());
_cp.addNameAndType
(fg.getName(), fg.getSignature());
}

System.out.println("Methods ... ");
Method[] methods = cg1.getMethods();
for (int j=0; j< methods.length; j++) {
MethodGen m = new MethodGen
(methods[j],

_cg.getClassName(),
_cp);
_cp.addMethodref(m);
_cp.addNameAndType(m.getName(),
m.getSignature());
_cg.addMethod(m.getMethod());
}

JavaClass c3 = _cg.getJavaClass();
c3.setConstantPool
(_cp.getFinalConstantPool());

try {
c3.dump(nomGen);
} catch(java.io.IOException e) {
System.err.println(e); }

}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.