最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java如何修改.class文件變量

 更新時(shí)間:2020年09月07日 10:01:36   作者:zero  
這篇文章主要介紹了Java如何修改.class文件變量,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下

最近遇到了一個(gè)問題,一份很老的代碼要修改里面的變量,源碼早就和開發(fā)者一起不知去向,其中引用了一些jar包導(dǎo)致無法直接編譯,只能直接修改.class文件

idea安裝jclasslib-bytecode-viewer插件

標(biāo)準(zhǔn)方式安裝插件

準(zhǔn)備要修改的.class文件

這里我們寫一個(gè)簡單的java方法

/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/9/5 11:18
 * @history: 1.2020/9/5 created by wei.wang
 */
public class HelloWorld {
  public static void main(String[] args) {
    String word = "Hello World";
    System.out.println(word);
  }
}

查找要修改的變量

打開要修改的.class文件,點(diǎn)擊view->Show Bytecode With Jclasslib ,在Constants Pool中使用Text filter功能找到要修改的內(nèi)容,我們發(fā)現(xiàn)有一個(gè)String類型常量,指向23,點(diǎn)擊23就能看到要修改的內(nèi)容

修改.class文件中的變量

23是要修改的內(nèi)容

/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/9/4 19:42
 * @history: 1.2020/9/4 created by wei.wang
 */

import java.io.*;

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

public class Test {
  public static void main(String[] args) throws Exception {

    String filePath = "F:\\GitCode\\zero\\test111\\target\\classes\\HelloWorld.class";
    FileInputStream fis = new FileInputStream(filePath);

    DataInput di = new DataInputStream(fis);
    ClassFile cf = new ClassFile();
    cf.read(di);

    CPInfo[] infos = cf.getConstantPool();

    int count = infos.length;
    System.out.println(count);

    for (int i = 0; i < count; i++) {
      if (infos[i] != null) {
        System.out.print(i);
        System.out.print(" = ");
        System.out.print(infos[i].getVerbose());
        System.out.print(" = ");
        System.out.println(infos[i].getTagVerbose());
        //對23進(jìn)行修改
        if(i == 23){
          ConstantUtf8Info uInfo = (ConstantUtf8Info)infos[i];
          uInfo.setBytes("Hello World HELLO WORLD".getBytes());
          infos[i]=uInfo;
        }
      }
    }

    cf.setConstantPool(infos);
    fis.close();
    File f = new File(filePath);
    ClassFileWriter.writeToFile(f, cf);
  }
}

執(zhí)行結(jié)果

可以看到已經(jīng)修改完成

public class HelloWorld {
  public HelloWorld() {
  }

  public static void main(String[] args) {
    String word = "Hello World HELLO WORLD";
    System.out.println(word);
  }
}

以上就是Java如何修改.class文件變量的詳細(xì)內(nèi)容,更多關(guān)于Java修改文件變量的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

威远县| 广河县| 盐源县| 博乐市| 佛坪县| 东阿县| 武平县| 邛崃市| 舟山市| 布拖县| 军事| 竹溪县| 灵丘县| 泉州市| 遂宁市| 安西县| 武城县| 牟定县| 鲜城| 陵川县| 扎兰屯市| 烟台市| 惠州市| 永善县| 延边| 务川| 黄龙县| 海南省| 潍坊市| 宁远县| 荣昌县| 衡南县| 沁阳市| 固阳县| 思茅市| 德阳市| 黑龙江省| 辛集市| 华蓥市| 襄垣县| 珠海市|