ByteCodeParser parser = new ByteCodeParser();
ReadStream is = source.openRead();
WriteStream os = null;
try {
JavaClass cl = parser.parse(is);
String cpOldName = sourceClass.replace('.', '/');
String cpClassName = targetClass.replace('.', '/');
int utf8Index = cl.getConstantPool().addUTF8(cpClassName).getIndex();
cl.getConstantPool().getClass(cpOldName).setNameIndex(utf8Index);
cl.setThisClass(cpClassName);
// need to set descriptors, too
// set private fields to protected
ArrayList<JavaField> fields = cl.getFieldList();
for (int i = 0; i < fields.size(); i++) {
JavaField field = fields.get(i);
int accessFlags = field.getAccessFlags();
if ((accessFlags & ACC_PRIVATE) != 0) {
accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
field.setAccessFlags(accessFlags);
}
}
// set private methods to protected
ArrayList<JavaMethod> methods = cl.getMethodList();
for (int i = 0; i < methods.size(); i++) {
JavaMethod method = methods.get(i);
int accessFlags = method.getAccessFlags();
if ((accessFlags & ACC_PRIVATE) != 0) {
accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
method.setAccessFlags(accessFlags);
}
}
for (int i = 0; i < _enhancerList.size(); i++) {
_enhancerList.get(i).preEnhance(cl);
}
target.getParent().mkdirs();
os = target.openWrite();
cl.write(os);
} finally {
if (is != null)
is.close();
if (os != null)