Skip to content

Commit

Permalink
jvm第二次作业实现
Browse files Browse the repository at this point in the history
  • Loading branch information
10094714 committed Apr 12, 2017
1 parent 87a5322 commit 09a4383
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@

public class ClassFile {

//Class文件的次版本号
private int minorVersion;
//Class文件的主版本号,Java SE 6.0对应的虚拟机支持Java SE 5.0的编译器编译的Class文件结构,反之则不行
private int majorVersion;

/**
* Class文件中常量池主要存储了字 面量以及符号引用,其中字面量主要包括字符串,
* final常量的值或者某个属性的初始值等等,而符号引用主要存储类和接口的全限定名称,
* 字段的名称以及描 述符,方法的名称以及描述符
* CONSTANT_Utf8_info,CONSTANT_Float_info,CONSTANT_Double_info
*/
private ConstantPool pool;

//表示类或者接口的访问信息
private AccessFlag accessFlag;
//类的常量池索引、超类的索引
private ClassIndex clzIndex;
private ConstantPool pool;

//字段表的信息
private List<Field> fields = new ArrayList<Field>();
//方法表
private List<Method> methods = new ArrayList<Method>();

public ClassIndex getClzIndex() {
Expand Down Expand Up @@ -78,12 +92,12 @@ public List<Method> getMethods() {

public void print() {

if (this.accessFlag.isPublicClass()) {
System.out.println("Access flag : public ");
}
System.out.println("Class Name:" + getClassName());

System.out.println("Super Class Name:" + getSuperClassName());
// if (this.accessFlag.isPublicClass()) {
// System.out.println("Access flag : public ");
// }
// System.out.println("Class Name:" + getClassName());
//
// System.out.println("Super Class Name:" + getSuperClassName());

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_Class_info {
* u1 tag;
* u2 name_index;
* }
*/
public class ClassInfo extends ConstantInfo {

private int type = ConstantInfo.CLASS_INFO;
private int utf8Index;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_Fieldref_info {
* u1 tag;
* u2 class_index;
* u2 name_and_type_index;
* }
*/
public class FieldRefInfo extends ConstantInfo{

private int type = ConstantInfo.FIELD_INFO;
private int classInfoIndex;
private int nameAndTypeIndex;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_Methodref_info {
* u1 tag;
* u2 class_index;
* u2 name_and_type_index;
*/
public class MethodRefInfo extends ConstantInfo {


private int type = ConstantInfo.METHOD_INFO;

private int classInfoIndex;
private int nameAndTypeIndex;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_NameAndType_info {
* u1 tag;
* u2 name_index;
* u2 descriptor_index;
* }
*/
public class NameAndTypeInfo extends ConstantInfo {

public int type = ConstantInfo.NAME_AND_TYPE_INFO;

private int index1;
private int index2;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_String_info {
* u1 tag;
* u2 string_index;
*/
public class StringInfo extends ConstantInfo {

private int type = ConstantInfo.STRING_INFO;
private int index;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.github.chaoswang.learning.java.jvm.constant;

/**
* CONSTANT_Utf8_info {
* u1 tag;
* u2 length;
* u1 bytes[length];
* }
*/
public class UTF8Info extends ConstantInfo {

private int type = ConstantInfo.UTF8_INFO;
private int length;
private String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,72 @@
import com.github.chaoswang.learning.java.jvm.clz.AccessFlag;
import com.github.chaoswang.learning.java.jvm.clz.ClassFile;
import com.github.chaoswang.learning.java.jvm.clz.ClassIndex;
import com.github.chaoswang.learning.java.jvm.constant.ClassInfo;
import com.github.chaoswang.learning.java.jvm.constant.ConstantPool;
import com.github.chaoswang.learning.java.jvm.constant.UTF8Info;

public class ClassFileParser {



public ClassFile parse(byte[] codes) {

return null;
ByteCodeIterator codeIterator = new ByteCodeIterator(codes);
ClassFile classFile = new ClassFile();
//跳过魔数
codeIterator.getBytes(4);
classFile.setMinorVersion(codeIterator.nextU2ToInt());
classFile.setMajorVersion(codeIterator.nextU2ToInt());

return classFile;
}


private AccessFlag parseAccessFlag(ByteCodeIterator iter) {

return null;
//得跳过常量池,常量池的长度是多少?
AccessFlag accessFlag = new AccessFlag(iter.nextU2ToInt());
return accessFlag;
}

private ClassIndex parseClassInfex(ByteCodeIterator iter) {
private ClassIndex parseClassIndex(ByteCodeIterator iter) {

return null;

}

private ConstantPool parseConstantPool(ByteCodeIterator iter) {

return null;
ConstantPool pool = new ConstantPool();
ClassInfo classInfo = new ClassInfo(pool);
//classInfo tag值
iter.nextU1toInt();
classInfo.setUtf8Index(iter.nextU2ToInt());
pool.addConstantInfo(classInfo);

UTF8Info utf8Info = new UTF8Info(pool);
//UTF8Info tag值
iter.nextU1toInt();
//UTF8Info length值
int length = iter.nextU2ToInt();
utf8Info.setLength(length);
utf8Info.setValue(iter.nextUxToHexString(length));
pool.addConstantInfo(utf8Info);

classInfo = new ClassInfo(pool);
//classInfo tag值
iter.nextU1toInt();
classInfo.setUtf8Index(iter.nextU2ToInt());
pool.addConstantInfo(classInfo);

for(int i=0;i<8;i++){
utf8Info = new UTF8Info(pool);
//UTF8Info tag值
iter.nextU1toInt();
//UTF8Info length值
length = iter.nextU2ToInt();
utf8Info.setLength(length);
utf8Info.setValue(iter.nextUxToHexString(length));
pool.addConstantInfo(utf8Info);
}

return pool;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
public class ClassFileloaderTest {

private static final String FULL_QUALIFIED_CLASS_NAME = "com/github/chaoswang/learning/java/jvm/EmployeeV1";
static String path1 = "E:\\workspace_jee\\coding2017\\target\\test-classes";
// static String path1 = "E:\\workspace_jee\\coding2017\\target\\test-classes";
static String path1 = "D:\\project\\workspace_1\\d-learning\\target\\test-classes";
static String path2 = "C:\temp";

static ClassFile clzFile = null;
// static {
// ClassFileLoader loader = new ClassFileLoader();
// loader.addClassPath(path1);
// String className = "com.github.chaoswang.learning.java.jvm.EmployeeV1";
//
// clzFile = loader.loadClass(className);
// clzFile.print();
// }
static {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.github.chaoswang.learning.java.jvm.EmployeeV1";

clzFile = loader.loadClass(className);
clzFile.print();
}

@Before
public void setUp() throws Exception {
Expand Down

0 comments on commit 09a4383

Please sign in to comment.