修改jar包里面的class的包名

参考文章
[url]http://blog.csdn.net/hexin373/article/details/6669813[/url]

工具
[url]http://www.ej-technologies.com/products/jclasslib/overview.html[/url]

修改class文件的代码


package com.kaka.modify;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.gjt.jclasslib.io.ClassFileWriter;
import org.gjt.jclasslib.structures.CPInfo;
import org.gjt.jclasslib.structures.ClassFile;
import org.gjt.jclasslib.structures.InvalidByteCodeException;
import org.gjt.jclasslib.structures.constants.ConstantUtf8Info;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayListlist = refreshFileList
("E:\\javaDev\\反编译工程\\miidicredit_android_2_1_5\\net\\miidi\\credit\\view\\a");
for (int i = 0; i < list.size(); i++) {
modify(list.get(i));
}

}

private static void modify(String filePath){
FileInputStream fis;
try {
fis = new FileInputStream(filePath);
DataInput di = new DataInputStream(fis);
ClassFile cf = new ClassFile();
cf.read(di);
CPInfo[] infos = cf.getConstantPool();

int count = infos.length;
for (int i = 0; i < count; i++) {
if (infos[i] != null) {
System.out.print(i);
System.out.print("|");
// 名
String str = infos[i].getVerbose();
System.out.print(str);
System.out.print("|");
// 类型
System.out.println(infos[i].getTagVerbose());
if(str!=null && str.length()>0){
if(infos[i] instanceof ConstantUtf8Info){
ConstantUtf8Info uInfo = (ConstantUtf8Info)infos[i];
if(str.contains("/miidi/")){
String str1 = str.replaceAll("/miidi/","/angela/");
uInfo.setString(str1);
infos[i]=uInfo;
}
}
}
}
}
cf.setConstantPool(infos);
fis.close();
File f = new File(filePath);
ClassFileWriter.writeToFile(f, cf);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidByteCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static ArrayList refreshFileList(String strPath) {
ArrayList filelist = new ArrayList();
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null)
return null;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
refreshFileList(files[i].getAbsolutePath());
} else {
String strFileName = files[i].getAbsolutePath(); //.toLowerCase();
System.out.println("---"+strFileName);
filelist.add(files[i].getAbsolutePath());
}
}
return filelist;
}
}

    推荐阅读