Skip to content

Commit

Permalink
添加打印框架
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxun committed Apr 21, 2017
1 parent fa6590f commit ca83f60
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 51 deletions.
48 changes: 48 additions & 0 deletions group17/785396327/4.16/print/ClassFilePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package print;

import clz.ClassFile;
import jvm_1.ClassFileLoader;

/**
* Created by gongxun on 2017/4/21.
*/
public class ClassFilePrinter {
ClassFile clzFile = null;
public ClassFilePrinter(ClassFile clzFile){
this.clzFile = clzFile;
}

public void print(){

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

System.out.println("Super Class Name:"+ clzFile.getSuperClassName());

System.out.println("minor version:" + clzFile.getMinorVersion());

System.out.println("major version:" + clzFile.getMinorVersion());

ConstantPoolPrinter cnstPoolPrinter = new ConstantPoolPrinter(clzFile.getConstantPool());
cnstPoolPrinter.print();




}

public static void main(String[] args){
String path = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path);
String className = "com.coderising.jvm.test.EmployeeV1";

ClassFile clzFile = loader.loadClass(className);

ClassFilePrinter printer = new ClassFilePrinter(clzFile);

printer.print();
}
}
48 changes: 48 additions & 0 deletions group17/785396327/4.16/print/ConstantPoolPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package print;

import constant.*;

/**
* Created by gongxun on 2017/4/21.
*/
public class ConstantPoolPrinter {
ConstantPool pool;
ConstantPoolPrinter(ConstantPool pool){
this.pool = pool;
}
public void print(){
System.out.println("Constant Pool:");

ConstantInfo.Visitor visitor = new ConstantInfo.Visitor() {
@Override
public void visitClassInfo(ClassInfo info) {

}

@Override
public void visitFieldRef(FieldRefInfo info) {

}

@Override
public void visitMethodRef(MethodRefInfo info) {

}

@Override
public void visitNameAndType(NameAndTypeInfo info) {

}

@Override
public void visitString(StringInfo info) {

}

@Override
public void visistUTF8(UTF8Info info) {

}
};
}
}
7 changes: 5 additions & 2 deletions group17/785396327/4.5/clz/ClassFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clz;

import constant.ClassInfo;
import constant.ConstantInfo;
import constant.ConstantPool;
import field.Field;
import method.Method;
Expand Down Expand Up @@ -79,15 +80,17 @@ public void print(){

System.out.println("Super Class Name:"+ getSuperClassName());

System.out.println("Constant pool:");


}

private String getClassName(){
public String getClassName(){
int thisClassIndex = this.clzIndex.getThisClassIndex();
ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex);
return thisClass.getClassName();
}
private String getSuperClassName(){
public String getSuperClassName(){
ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}
Expand Down
1 change: 1 addition & 0 deletions group17/785396327/4.5/constant/ConstantPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public String getUTF8String(int index){
public Object getSize() {
return this.constantInfos.size() -1;
}

}
41 changes: 1 addition & 40 deletions group17/785396327/4.5/constant/MethodRefInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Created by IBM on 2017/4/10.
*/
public class MethodRefInfo extends ConstantInfo implements ConstantInfo.Visitor {
public class MethodRefInfo extends ConstantInfo{
private int type = ConstantInfo.METHOD_INFO;

private int classInfoIndex;
Expand Down Expand Up @@ -55,43 +55,4 @@ public String getMethodName() {
return typeInfo.getName();
}

@Override
public void visitClassInfo(ClassInfo info) {

}

@Override
public void visitFieldRef(FieldRefInfo info) {

}

@Override
public void visitMethodRef(MethodRefInfo info) {
StringBuilder sb = new StringBuilder();
sb.append("\t# = Methodref\t\t\t#")
.append(classInfoIndex)
.append(".#")
.append(nameAndTypeIndex)
.append("\t\t// ")
.append(getClassName() + ".")
.append(((NameAndTypeInfo) getConstantInfo(nameAndTypeIndex)).getName())
.append(".")
.append(((NameAndTypeInfo) getConstantInfo(nameAndTypeIndex)).getTypeInfo());
System.out.println(sb.toString());
}

@Override
public void visitNameAndType(NameAndTypeInfo info) {

}

@Override
public void visitString(StringInfo info) {

}

@Override
public void visistUTF8(UTF8Info info) {

}
}
20 changes: 12 additions & 8 deletions group17/785396327/4.5/constant/NameAndTypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Created by IBM on 2017/4/10.
*/
public class NameAndTypeInfo extends ConstantInfo {
public int type = ConstantInfo.NAME_AND_TYPE_INFO;

public int type = ConstantInfo.NAME_AND_TYPE_INFO;
private int index1;
private int index2;

Expand All @@ -16,15 +15,19 @@ public NameAndTypeInfo(ConstantPool pool) {
public int getIndex1() {
return index1;
}

public void setIndex1(int index1) {
this.index1 = index1;
}

public int getIndex2() {
return index2;
}

public void setIndex2(int index2) {
this.index2 = index2;
}

public int getType() {
return type;
}
Expand All @@ -35,19 +38,20 @@ public void accept(Visitor visitor) {
}


public String getName(){
public String getName() {
ConstantPool pool = this.getConstantPool();
UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1);
UTF8Info utf8Info1 = (UTF8Info) pool.getConstantInfo(index1);
return utf8Info1.getValue();
}

public String getTypeInfo(){
public String getTypeInfo() {
ConstantPool pool = this.getConstantPool();
UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2);
UTF8Info utf8Info2 = (UTF8Info) pool.getConstantInfo(index2);
return utf8Info2.getValue();
}

public String toString(){
return "(" + getName() + "," + getTypeInfo()+")";
public String toString() {
return "(" + getName() + "," + getTypeInfo() + ")";
}

}
1 change: 1 addition & 0 deletions group17/785396327/4.5/constant/NullConstantInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public int getType() {
public void accept(Visitor visitor) {

}

}
2 changes: 1 addition & 1 deletion group17/785396327/4.5/constant/StringInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getType() {

@Override
public void accept(Visitor visitor) {

visitor.visitString(this);
}

public int getIndex() {
Expand Down

0 comments on commit ca83f60

Please sign in to comment.