📄 vm_instructionset.java
字号:
package AM.vm_impl.vm_data_structure;
import java.lang.reflect.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import AM.am_data_structure.*;
import AM.vm_impl.util.*;
/**
* Created by IntelliJ IDEA. User: yellowicq Date: 2004-4-27 Time: 14:00:22
* To change this template use File | Settings | File Templates.
*/
public class VM_InstructionSet
implements InstructionSet {
private String configFileName = "E:\\jbproject\\AM\\src\\bytecodeconfig.xml";
private HashMap instMap;
private HashMap bytecodeMap;
public VM_InstructionSet() {
instMap = new HashMap();
bytecodeMap = new HashMap();
Init();
}
public void Init() {
LoadBytecodeMappingConfig();
}
/**
* initBytecodeMapping
*/
private void LoadBytecodeMappingConfig() {
/*parse xml configure file*/
String bytecode = "";
String instrName = "";
Class clazz = null;
try {
/*Create DocumentBuilderFactory*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/*Create DocumentBuilder*/
DocumentBuilder builder = factory.newDocumentBuilder();
/*Create Document*/
Document doc = builder.parse(configFileName);
/*Normalize document*/
doc.normalize();
/*Scan node*/
//Parse bytecode mapping
NodeList parentNlists = doc.getElementsByTagName("mapping");
//tracing
Tracer.debug("[Total bytecode mapping entry] " + parentNlists.getLength());
for (int i = 0; i < parentNlists.getLength(); i++) {
Node parentNode = parentNlists.item(i);
//Retrieve node value
NodeList childNlists = parentNode.getChildNodes();
for (int j = 0; j < childNlists.getLength(); j++) {
Node node = childNlists.item(j);
if (node.getNodeName().equalsIgnoreCase("bytecode")) {
Tracer.debug(node.getFirstChild().getNodeValue().trim());
bytecode = node.getFirstChild().getNodeValue().trim();
}
else if (node.getNodeName().equalsIgnoreCase("instruction-name")) {
Tracer.debug(node.getFirstChild().getNodeValue().trim());
instrName = node.getFirstChild().getNodeValue().trim();
}
else if (node.getNodeName().equalsIgnoreCase("si-class")) {
Tracer.debug(node.getFirstChild().getNodeValue().trim());
clazz = Class.forName(node.getFirstChild().getNodeValue().trim());
}
/******************/
}
/////////regiester bytecode mapping//////////
registerBytecodeMapping(bytecode, instrName);
/////////regiester instruction mapping///////
registerInstructionMapping(instrName, clazz);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void registerInstructionMapping(Object instrName, Class clazz) {
//push instruction mapping
instMap.put(instrName, clazz);
}
private void registerBytecodeMapping(Object bytecode, Object instrName) {
//push bytecode mapping
bytecodeMap.put(bytecode, instrName);
}
public Vector Resolve(Stream ins) {
Vector tmpProgram = new Vector();
Tracer.debug("Loading codebyte to program...");
try {
// read the contents into a byte array
byte[] bytecode = new byte[2];
while (ins.read(bytecode) != -1) {
String mapStr = (String) bytecodeMap.get(new String(bytecode));
Tracer.debug("[mapString] " + new String(bytecode) + ":" + mapStr);
if (mapStr == null) {
throw new IllegalByteCodeException();
}
/////////////////////////////////////////////////////////////////////
//Resolve bytecode 'new',for this process,the reference //
//class should be be organized as form "Ljava/lang/Object;" //
//I will first extract the className String "java/lang/Object" //
//through bytecode stream,then parse it in form "java.lang.Object" //
/////////////////////////////////////////////////////////////////////
////////////////////////Start resolve bytecode 'new'/////////////////
if (mapStr.equalsIgnoreCase("new")) {
boolean isMatch = false;
byte[] singleBytecode = new byte[1];
StringBuffer className = new StringBuffer("");
ins.read(singleBytecode);
if (!new String(singleBytecode).equals("L")) {
throw new IllegalByteCodeException();
}
else {
while (ins.read(singleBytecode) != -1) {
if (new String(singleBytecode).equals(";")) {
isMatch = true;
//convert the string to class name
/////////////////////////////////////////////
String classNameStr = className.toString();
classNameStr = classNameStr.replaceAll("/", ".");
Class instNEWClass = (Class) instMap.get(mapStr);
Class params[] = {
java.lang.String.class};
Constructor constructor = instNEWClass.getConstructor(
params);
Object initargs[] = {
classNameStr
};
tmpProgram.add(constructor.newInstance(initargs));
Tracer.debug("[classNameStr] " + classNameStr);
////////////////////////////////////////////////////
break;
}
className.append(new String(singleBytecode));
}
if (!isMatch) {
throw new IllegalByteCodeException();
}
}
}
////////////////////End resolve bytecode 'new'//////////////////
else {
tmpProgram.add( ( (Class) instMap.get(mapStr)).newInstance());
}
}
ins.close();
// return the resolved program
return tmpProgram;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -