📄 classsaver.java
字号:
/*
*
*/
package ee.ioc.cs.jbe.browser.codeedit;
import java.io.IOException;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
public class ClassSaver implements Runnable {
public static final int SAVE_MISC = 1;
public static final int SAVE_CODE = 2;
public static final int SAVE_CONSTANT = 3;
public static final int REMOVE_CONSTANT = 4;
public static final int SAVE_FIELD = 5;
public static final int REMOVE_FIELD = 6;
public static final int SAVE_METHOD = 7;
public static final int REMOVE_METHOD = 8;
public static final int SAVE_EXCEPTION = 9;
public static final int REMOVE_EXCEPTION = 10;
public static final int REMOVE_INTERFACE = 11;
public static final int ADD_METHOD = 12;
public static final int ADD_INTERFACE = 13;
static public final int ADD_FIELD = 14;
private int state;
private int maxStack;
private int maxLocals;
private int index;
private String fileName;
private String methodBody;
private boolean exceptionOccured = false;
private String exceptionVerbose = "";
private byte constType;
private String[] constInfo;
private int startPc;
private int endPc;
private int handlerPc;
private String handlerClass;
private int exceptionIndex;
private int accessFlags;
private String methodName;
private String methodDescriptor;
private String interfaceName;
private String fieldDescriptor;
private String fieldName;
public ClassSaver(int state, String fileName, int maxStack, int maxLocals,
int methodIndex) {
this.state = state;
this.fileName = fileName;
this.maxStack = maxStack;
this.maxLocals = maxLocals;
this.index = methodIndex;
}
public ClassSaver(int state, String fileName, String methodBody,
int methodIndex) {
this.state = state;
this.fileName = fileName;
this.methodBody = methodBody;
this.index = methodIndex;
}
public ClassSaver(int state, String fileName, int index) {
this.state = state;
this.fileName = fileName;
this.index = index;
}
public ClassSaver(int state, String fileName, String[] constInfo,
byte constType) {
this.state = state;
this.fileName = fileName;
this.constInfo = constInfo;
this.constType = constType;
}
public ClassSaver(int state, String fileName, int methodIndex, int startPc,
int endPc, int handlerPc, String handlerClass) {
this.state = state;
this.fileName = fileName;
this.index = methodIndex;
this.startPc = startPc;
this.endPc = endPc;
this.handlerPc = handlerPc;
this.handlerClass = handlerClass;
}
public ClassSaver(int state, String fileName, int methodIndex,
int exceptionIndex) {
this.state = state;
this.fileName = fileName;
this.index = methodIndex;
this.exceptionIndex = exceptionIndex;
}
public ClassSaver(int state, String fileName, int accessFlags,
String methodName, String methodDescriptor) {
this.state = state;
this.fileName = fileName;
this.accessFlags = accessFlags;
this.methodName = methodName;
this.methodDescriptor = methodDescriptor;
}
public ClassSaver(int state, String fileName, String interfaceName) {
this.state = state;
this.fileName = fileName;
this.interfaceName = interfaceName;
}
public ClassSaver(int state, String fileName, String fieldName,
String fieldDescriptor, int accessFlags) {
this.state = state;
this.fileName = fileName;
this.fieldName = fieldName;
this.fieldDescriptor = fieldDescriptor;
this.accessFlags = accessFlags;
}
public void saveMisc(String fileName, int maxStack, int maxLocals,
int methodIndex) {
try {
JavaClass javaClass = new ClassParser(fileName).parse();
String className = javaClass.getClassName();
Method[] methods = javaClass.getMethods();
ConstantPool constants = javaClass.getConstantPool();
ConstantPoolGen cpg = new ConstantPoolGen(constants);
MethodGen mg = new MethodGen(methods[methodIndex], className, cpg);
mg.setMaxStack(maxStack);
mg.setMaxLocals(maxLocals);
methods[methodIndex] = mg.getMethod();
javaClass.setConstantPool(cpg.getFinalConstantPool());
javaClass.dump(fileName);
} catch (ClassFormatException e) {
exceptionOccured = true;
exceptionVerbose = e.getMessage();
} catch (IOException e) {
exceptionOccured = true;
exceptionVerbose = e.getMessage();
}
}
public void saveCode(String fileName, String jasm, int methodIndex) {
try {
JavaClass javaClass = new ClassParser(fileName).parse();
String className = javaClass.getClassName();
// Set up constant pool
ConstantPool constants = javaClass.getConstantPool();
ConstantPoolGen cpg = new ConstantPoolGen(constants);
JAsmParser codeParser = new JAsmParser();
// Set up methods
Method[] methods = javaClass.getMethods();
MethodGen mg = new MethodGen(methods[methodIndex], className, cpg);
// Note that parsing has a sideeffect, it updates
// constant pool, to have the required constants ready.
// Of course the constant pool isnt dumped into the classfile unless
// parsing was successful
InstructionList il = codeParser.parse(jasm, cpg);
il.setPositions(true);
mg.removeLineNumbers();
mg.removeLocalVariables();
mg.setInstructionList(il);
methods[methodIndex] = mg.getMethod();
javaClass.setConstantPool(cpg.getFinalConstantPool());
javaClass.dump(fileName);
} catch (ClassFormatException e) {
exceptionOccured = true;
exceptionVerbose = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
exceptionOccured = true;
exceptionVerbose = e.getMessage();
} catch (JAsmParseException e) {
exceptionOccured = true;
exceptionVerbose = e.getErrorVerbose();
}
}
public void saveConstant(String fileName, String[] constInfo, byte constType) {
try {
JavaClass javaClass = new ClassParser(fileName).parse();
ConstantPool constants = javaClass.getConstantPool();
ConstantPoolGen cpg = new ConstantPoolGen(constants);
switch (constType) {
case Constants.CONSTANT_Class:
cpg.addClass(constInfo[0]);
break;
case Constants.CONSTANT_Double:
cpg.addDouble(Double.parseDouble(constInfo[0]));
break;
case Constants.CONSTANT_Integer:
cpg.addInteger(Integer.parseInt(constInfo[0]));
break;
case Constants.CONSTANT_Fieldref:
cpg.addFieldref(constInfo[0], constInfo[1], constInfo[2]);
break;
case Constants.CONSTANT_Float:
cpg.addFloat(Float.parseFloat(constInfo[0]));
break;
case Constants.CONSTANT_InterfaceMethodref:
cpg.addInterfaceMethodref(constInfo[0], constInfo[1],
constInfo[2]);
break;
case Constants.CONSTANT_Methodref:
cpg.addMethodref(constInfo[0], constInfo[1], constInfo[2]);
break;
case Constants.CONSTANT_Long:
cpg.addLong(Long.parseLong(constInfo[0]));
break;
case Constants.CONSTANT_NameAndType:
cpg.addNameAndType(constInfo[0], constInfo[1]);
break;
case Constants.CONSTANT_String:
cpg.addString(constInfo[0]);
break;
case Constants.CONSTANT_Utf8:
cpg.addUtf8(constInfo[0]);
break;
}
javaClass.setConstantPool(cpg.getFinalConstantPool());
javaClass.dump(fileName);
} catch (ClassFormatException e) {
exceptionOccured = true;
exceptionVerbose = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
exceptionOccured = true;
exceptionVerbose = e.getMessage();
e.printStackTrace();
}
}
public void removeConstant(String fileName, int constantPoolIndex) {
try {
JavaClass javaClass = new ClassParser(fileName).parse();
ConstantPool cp = javaClass.getConstantPool();
Constant[] constants = cp.getConstantPool();
Constant[] newPool = new Constant[constants.length - 1];
int j = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -