Skip to content

Commit

Permalink
完成minijvm读取常量池功能
Browse files Browse the repository at this point in the history
  • Loading branch information
626451284 committed Apr 8, 2017
1 parent f35a9ac commit 1240657
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.wdn.coding2017.jvm.clz;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class AccessFlag {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.wdn.coding2017.jvm.clz;

import com.github.wdn.coding2017.jvm.constant.ConstantPool;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassFile {
private int minorVersion;
private int majorVersion;
private ConstantPool constantPool;
private AccessFlag accessFlag;
private ClassIndex classIndex;
public void print() {
}

public int getMinorVersion() {
return minorVersion;
}

public int getMajorVersion() {
return majorVersion;
}

public void setMinorVersion(int minorVersion) {
this.minorVersion = minorVersion;
}

public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

public ConstantPool getConstantPool() {
return constantPool;
}

public void setConstantPool(ConstantPool constantPool) {
this.constantPool = constantPool;
}

public AccessFlag getAccessFlag() {
return accessFlag;
}

public void setAccessFlag(AccessFlag accessFlag) {
this.accessFlag = accessFlag;
}

public ClassIndex getClassIndex() {
return classIndex;
}

public void setClassIndex(ClassIndex classIndex) {
this.classIndex = classIndex;
}

public ClassIndex getClzIndex() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.wdn.coding2017.jvm.clz;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassIndex {
private int thisClassIndex;
private int superClassIndex;
public ClassIndex(int thisClassIndex,int superClassIndex){
this.thisClassIndex = thisClassIndex;
this.superClassIndex = superClassIndex;
}
public int getThisClassIndex() {
return thisClassIndex;
}

public int getSuperClassIndex() {
return superClassIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ClassInfo extends ConstantInfo{
private int nameIndex;
public ClassInfo(ConstantPool constantPool){
super(constantPool);
}
@Override
public int getType() {
return CLASS_INFO;
}
@Override
public String getValue() {
return getConstantPool().getConstantInfo(nameIndex).getValue();
}

public int getNameIndex() {
return nameIndex;
}

public void setNameIndex(int nameIndex) {
this.nameIndex = nameIndex;
}

public int getUtf8Index() {
return 0;
}

public String getClassName() {
return getConstantPool().getConstantInfo(nameIndex).getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public abstract class ConstantInfo {
public static final int UTF8_INFO = 1;
public static final int FLOAT_INFO = 4;
public static final int CLASS_INFO = 7;
public static final int STRING_INFO = 8;
public static final int FIELD_INFO = 9;
public static final int METHOD_INFO = 10;
public static final int NAME_AND_TYPE_INFO = 12;
protected ConstantPool constantPool;
public ConstantInfo(){

}
public ConstantInfo(ConstantPool constantPool){
this.constantPool = constantPool;
}
public abstract int getType();
public abstract String getValue();
public ConstantPool getConstantPool(){
return constantPool;
}
public ConstantInfo getConstantInfo(int index){
return constantPool.getConstantInfo(index);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.wdn.coding2017.jvm.constant;


import java.util.ArrayList;

/**
* Created by Administrator on 2017/4/6 0006.
*/
public class ConstantPool {
public static ArrayList<ConstantInfo> constantPool = new ArrayList<ConstantInfo>();
static{
constantPool.add(new NullConstantInfo());
}
public void put(ConstantInfo info){
constantPool.add(info);
}
public int getSize() {
return constantPool.size()-1;
}

public ConstantInfo getConstantInfo(int i) {
return constantPool.get(i);
}
@Override
public String toString(){
StringBuffer stringBuffer = new StringBuffer();
for (int i = 1; i < constantPool.size(); i++) {
stringBuffer.append("#"+i+"=>"+constantPool.get(i).getValue());
}
return stringBuffer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class FieldRefInfo extends ConstantInfo {
private int classInfoIndex;
private int nameAndTypeIndex;

public FieldRefInfo(ConstantPool constantPool) {
super(constantPool);
}

@Override
public int getType() {
return FIELD_INFO;
}

@Override
public String getValue() {
return getConstantPool().getConstantInfo(classInfoIndex).getValue()+getConstantPool().getConstantInfo(nameAndTypeIndex).getValue();
}

public int getClassInfoIndex() {
return classInfoIndex;
}

public void setClassInfoIndex(int classInfoIndex) {
this.classInfoIndex = classInfoIndex;
}

public int getNameAndTypeIndex() {
return nameAndTypeIndex;
}

public void setNameAndTypeIndex(int nameAndTypeIndex) {
this.nameAndTypeIndex = nameAndTypeIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class MethodRefInfo extends ConstantInfo {
private int classInfoIndex;
private int nameAndTypeIndex;

public MethodRefInfo(ConstantPool constantPool) {
super(constantPool);
}
@Override
public int getType() {
return METHOD_INFO;
}
@Override
public String getValue() {
return getConstantPool().getConstantInfo(classInfoIndex).getValue()+getConstantPool().getConstantInfo(nameAndTypeIndex).getValue();
}

public int getClassInfoIndex() {
return classInfoIndex;
}

public void setClassInfoIndex(int classInfoIndex) {
this.classInfoIndex = classInfoIndex;
}

public int getNameAndTypeIndex() {
return nameAndTypeIndex;
}

public void setNameAndTypeIndex(int nameAndTypeIndex) {
this.nameAndTypeIndex = nameAndTypeIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class NameAndTypeInfo extends ConstantInfo{
private int nameIndex;
private int descriptorIndex;

public NameAndTypeInfo(ConstantPool constantPool) {
super(constantPool);
}

@Override
public int getType() {
return 0;
}
@Override
public String getValue() {
return getConstantPool().getConstantInfo(nameIndex).getValue()+getConstantPool().getConstantInfo(descriptorIndex).getValue();
}

public int getNameIndex() {
return nameIndex;
}

public void setNameIndex(int nameIndex) {
this.nameIndex = nameIndex;
}

public int getDescriptorIndex() {
return descriptorIndex;
}

public void setDescriptorIndex(int descriptorIndex) {
this.descriptorIndex = descriptorIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class NullConstantInfo extends ConstantInfo {
public int getType() {
return 0;
}

public String getValue() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class StringInfo extends ConstantInfo {
public StringInfo(ConstantPool constantPool) {
super(constantPool);
}

private int stringIndex;

@Override
public int getType() {
return 0;
}

@Override
public String getValue() {
return getConstantPool().getConstantInfo(stringIndex).getValue();
}

public int getStringIndex() {
return stringIndex;
}

public void setStringIndex(int stringIndex) {
this.stringIndex = stringIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.wdn.coding2017.jvm.constant;

/**
* Created by Administrator on 2017/4/8 0008.
*/
public class UTF8Info extends ConstantInfo{
private String value;
public UTF8Info(ConstantPool constantPool){
super(constantPool);
}
@Override
public int getType() {
return UTF8_INFO;
}
@Override
public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Loading

0 comments on commit 1240657

Please sign in to comment.