com.loocme.sys.util.ClassModfier Maven / Gradle / Ivy
package com.loocme.sys.util;
/**
* class类修改器
*
* @author loocme
*
*/
public class ClassModfier implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
private static final int CONSTANT_POOL_COUNT_INDEX = 8;
private static final int CONSTANT_UTF_8_INFO = 1;
private static final int[] CONSTANT_ITEM_LENGTH = {-1, -1, 5, -1, 5, 9, 9,
3, 3, 5, 5, 5, 5};
private static final int U_1 = 1;
private static final int U_2 = 2;
private byte[] classByte;
public ClassModfier(byte[] classByte)
{
this.classByte = classByte;
}
public byte[] modifyUTF8Constant(String oldStr, String newStr)
{
int cpc = this.getConstantPoolCount();
int offset = CONSTANT_POOL_COUNT_INDEX + U_2;
for (int i = 0; i < cpc; i++)
{
int tag = ByteUtil.bytes2Int(this.classByte, offset, U_1);
if (CONSTANT_UTF_8_INFO == tag)
{
int len = ByteUtil.bytes2Int(this.classByte, offset + U_1, U_2);
offset += (U_1 + U_2);
String str = ByteUtil.bytes2String(this.classByte, offset, len);
if (str.equalsIgnoreCase(oldStr))
{
byte[] strBytes = ByteUtil.string2Bytes(newStr);
byte[] strLen = ByteUtil.int2Byte(newStr.length(), U_2);
this.classByte = ByteUtil.bytesReplace(this.classByte,
offset - U_2, U_2, strLen);
this.classByte = ByteUtil.bytesReplace(this.classByte,
offset, len, strBytes);
return this.classByte;
}
else
{
offset += len;
}
}
else
{
offset += CONSTANT_ITEM_LENGTH[tag];
}
}
return this.classByte;
}
private int getConstantPoolCount()
{
return ByteUtil
.bytes2Int(this.classByte, CONSTANT_POOL_COUNT_INDEX, U_2);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy