Finally I try to utilize BCELifier to generate Java codes from both generated and wrote class files, and I found the differences between them.
public static void main(String[] args) throws Throwable {
final JavaClass outerClass = Repository.lookupClass(ServiceOrder.class.getName());
StringRepresentation s = new StringRepresentation(outerClass);
System.out.println(s);
BCELifier fier = new BCELifier(outerClass, System.out);
fier.start();
final JavaClass outer2Class = Repository.lookupClass(net.madz.lifecycle.solutionOne.ServiceOrder.class.getName());
BCELifier fier2 = new BCELifier(outer2Class, System.out);
fier2.start();
And then found the difference part:
CORRECT:
il.append(_factory.createFieldAccess("java.lang.Long", "TYPE", new ObjectType("java.lang.Class"), Constants.GETSTATIC));
WRONG:
il.append(_factory.createFieldAccess("java.lang.Long", "TYPE", Type.LONG, Constants.GETSTATIC));
In my code, actually I am using:
ilist.append(ifact.createGetStatic(convertType2ClassName(type), "TYPE", Type.LONG));
Obviously it should be:
ilist.append(ifact.createGetStatic(convertType2ClassName(type), "TYPE", new ObjectType("java.lang.Class")));
In summary, there are two mistakes I made:
- I did not use InstructionFactory.createFieldAccess method, but I choose InstructionFactory.createGetStatic, although they can produce same results, but createFieldAccess has documents and createGetStatic does not.
- I should use new ObjectType(Class.class.getName()) other than Type.LONG, since it should be the field type, which should match Class[].