forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from yanghaitao0410/master
Master
- Loading branch information
Showing
23 changed files
with
1,110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="C:/Users/yanght/Downloads/dom4j-1.6.1.jar"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||
<attributes> | ||
<attribute name="owner.project.facets" value="java"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
group06/1454385822/src/com/coding/basic/homework_04/jvm/attr/AccessFlag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.coding.basic.homework_04.jvm.attr; | ||
|
||
public class AccessFlag { | ||
|
||
private int flagValue; | ||
|
||
public AccessFlag(int value) { | ||
this.flagValue = value; | ||
} | ||
|
||
public int getFlagValue() { | ||
return flagValue; | ||
} | ||
|
||
public void setFlagValue(int flagValue) { | ||
this.flagValue = flagValue; | ||
} | ||
|
||
public boolean isPublicClass(){ | ||
return (this.flagValue & 0x0001) != 0; | ||
} | ||
public boolean isFinalClass(){ | ||
return (this.flagValue & 0x0010) != 0; | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
group06/1454385822/src/com/coding/basic/homework_04/jvm/clz/ClassFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.coding.basic.homework_04.jvm.clz; | ||
|
||
import com.coding.basic.homework_04.jvm.attr.AccessFlag; | ||
import com.coding.basic.homework_04.jvm.constant.ConstantPool; | ||
|
||
public class ClassFile { | ||
|
||
private String MagicNumber; | ||
|
||
private int minorVersion; | ||
private int majorVersion; | ||
private ConstantPool constantPool; | ||
private int ConstantNum; | ||
private ClassIndex clzIndex; | ||
|
||
private AccessFlag accessFlag; | ||
|
||
|
||
|
||
public AccessFlag getAccessFlag() { | ||
return accessFlag; | ||
} | ||
|
||
|
||
|
||
public void setAccessFlag(AccessFlag accessFlag) { | ||
this.accessFlag = accessFlag; | ||
} | ||
|
||
|
||
|
||
public ClassIndex getClzIndex() { | ||
return clzIndex; | ||
} | ||
|
||
|
||
|
||
public void setClzIndex(ClassIndex clzIndex) { | ||
this.clzIndex = clzIndex; | ||
} | ||
|
||
|
||
|
||
public int getConstantNum() { | ||
return ConstantNum; | ||
} | ||
|
||
|
||
|
||
public void setConstantNum(int constantNum) { | ||
ConstantNum = constantNum; | ||
} | ||
|
||
|
||
|
||
public void print() { | ||
System.out.println("MagicNumber: "+ MagicNumber); | ||
System.out.println( "minorVersion" +minorVersion); | ||
System.out.println("majorVersion" + majorVersion); | ||
|
||
} | ||
|
||
|
||
|
||
public String getMagicNumber() { | ||
return MagicNumber; | ||
} | ||
|
||
|
||
|
||
public void setMagicNumber(String magicNumber) { | ||
MagicNumber = magicNumber; | ||
} | ||
|
||
|
||
|
||
public int getMinorVersion() { | ||
return minorVersion; | ||
} | ||
|
||
|
||
public void setMinorVersion(int minorVersion) { | ||
this.minorVersion = minorVersion; | ||
} | ||
|
||
|
||
public int getMajorVersion() { | ||
return majorVersion; | ||
} | ||
|
||
|
||
public void setMajorVersion(int majorVersion) { | ||
this.majorVersion = majorVersion; | ||
} | ||
|
||
|
||
public ConstantPool getConstantPool() { | ||
return constantPool; | ||
} | ||
|
||
|
||
public void setConstantPool(ConstantPool constantPool) { | ||
this.constantPool = constantPool; | ||
} | ||
|
||
|
||
} |
99 changes: 99 additions & 0 deletions
99
group06/1454385822/src/com/coding/basic/homework_04/jvm/clz/ClassFileParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.coding.basic.homework_04.jvm.clz; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import com.coding.basic.homework_04.jvm.attr.AccessFlag; | ||
import com.coding.basic.homework_04.jvm.constant.ConstantPool; | ||
import com.coding.basic.homework_04.jvm.info.ClassInfo; | ||
import com.coding.basic.homework_04.jvm.info.FieldRefInfo; | ||
import com.coding.basic.homework_04.jvm.info.MethodRefInfo; | ||
import com.coding.basic.homework_04.jvm.info.NameAndTypeInfo; | ||
import com.coding.basic.homework_04.jvm.info.NullConstantInfo; | ||
import com.coding.basic.homework_04.jvm.info.StringInfo; | ||
import com.coding.basic.homework_04.jvm.info.UTF8Info; | ||
import com.coding.basic.homework_04.jvm.util.ByteCodeIterator; | ||
|
||
public class ClassFileParser { | ||
|
||
private ClassFile classFile; | ||
|
||
private ConstantPool pool; | ||
|
||
public ClassFile parser(byte[] codes) { | ||
ByteCodeIterator iterator = new ByteCodeIterator(codes); | ||
classFile = new ClassFile(); | ||
classFile.setMagicNumber(iterator.nextU4ToHexString()); | ||
classFile.setMinorVersion(iterator.nextU2ToInt()); | ||
classFile.setMajorVersion(iterator.nextU2ToInt()); | ||
classFile.setConstantNum(iterator.nextU2ToInt()); | ||
|
||
System.out.println("Constant Pool Count :" + classFile.getConstantNum()); | ||
|
||
try { | ||
classFile.setConstantPool(parserConstantPool(iterator)); | ||
} catch (UnsupportedEncodingException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
classFile.setAccessFlag(new AccessFlag(iterator.nextU2ToInt())); | ||
|
||
ClassIndex clzIndex = new ClassIndex(); | ||
clzIndex.setThisClassIndex(iterator.nextU2ToInt()); | ||
clzIndex.setSuperClassIndex(iterator.nextU2ToInt()); | ||
|
||
classFile.setClzIndex(clzIndex); | ||
|
||
return classFile; | ||
} | ||
|
||
private ConstantPool parserConstantPool(ByteCodeIterator iterator) throws UnsupportedEncodingException{ | ||
pool = new ConstantPool(); | ||
|
||
|
||
pool.addConstantInfo(new NullConstantInfo()); | ||
for(int i=1; i <= classFile.getConstantNum() - 1; i++){ | ||
int tag = iterator.nextU1toInt(); | ||
|
||
if(tag == 7){ //class info | ||
ClassInfo clzInfo = new ClassInfo(pool); | ||
clzInfo.setUtf8Index(iterator.nextU2ToInt()); | ||
pool.addConstantInfo(clzInfo); | ||
}else if(tag == 1){ //utf8 info | ||
UTF8Info utf8Info = new UTF8Info(pool); | ||
utf8Info.setLength(iterator.nextU2ToInt()); | ||
String string = iterator.nextUxToHexString(utf8Info.getLength()); | ||
utf8Info.setValue(string); | ||
// System.out.println(utf8Info.toString()); | ||
|
||
pool.addConstantInfo(utf8Info); | ||
}else if(tag == 8){ //String info | ||
StringInfo stringInfo = new StringInfo(pool); | ||
stringInfo.setString_index(iterator.nextU2ToInt()); | ||
pool.addConstantInfo(stringInfo); | ||
}else if(tag == 9){ //FieldRef info | ||
FieldRefInfo fieldRefInfo = new FieldRefInfo(pool); | ||
fieldRefInfo.setClz_index(iterator.nextU2ToInt()); | ||
fieldRefInfo.setNameAndType_index(iterator.nextU2ToInt()); | ||
pool.addConstantInfo(fieldRefInfo); | ||
}else if(tag == 10){ //MethodRef info | ||
MethodRefInfo methodRefInfo = new MethodRefInfo(pool); | ||
methodRefInfo.setClass_index(iterator.nextU2ToInt()); | ||
methodRefInfo.setNameAndType_index(iterator.nextU2ToInt()); | ||
pool.addConstantInfo(methodRefInfo); | ||
}else if(tag == 12){ //NameAndType info | ||
NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(pool); | ||
nameAndTypeInfo.setClz_index(iterator.nextU2ToInt()); | ||
nameAndTypeInfo.setDescriptor_index(iterator.nextU2ToInt()); | ||
pool.addConstantInfo(nameAndTypeInfo); | ||
}else{ | ||
throw new RuntimeException("this tag" + tag+"has no already implement yet!!"); | ||
} | ||
} | ||
System.out.println("pool size:" +pool.getSize()); | ||
|
||
System.out.println("finished parser constantPool!!"); | ||
return pool; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
group06/1454385822/src/com/coding/basic/homework_04/jvm/clz/ClassIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.coding.basic.homework_04.jvm.clz; | ||
|
||
public class ClassIndex { | ||
|
||
private int thisClassIndex; | ||
private int superClassIndex; | ||
public int getThisClassIndex() { | ||
return thisClassIndex; | ||
} | ||
public void setThisClassIndex(int thisClassIndex) { | ||
this.thisClassIndex = thisClassIndex; | ||
} | ||
public int getSuperClassIndex() { | ||
return superClassIndex; | ||
} | ||
public void setSuperClassIndex(int superClassIndex) { | ||
this.superClassIndex = superClassIndex; | ||
} | ||
|
||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
group06/1454385822/src/com/coding/basic/homework_04/jvm/constant/ConstantInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.coding.basic.homework_04.jvm.constant; | ||
|
||
import org.omg.CORBA.PRIVATE_MEMBER; | ||
|
||
public class ConstantInfo { | ||
|
||
public static final int CLASS_INFO = 7; | ||
public static final int UTF8_INFO = 1; | ||
public static final int STRING_INFO = 8; | ||
public static final int FIELDREF_INFO = 9; | ||
public static final int METHODREF_INFO = 10; | ||
public static final int NAMEANDTYPE_INFO = 12; | ||
|
||
protected ConstantPool pool; | ||
public ConstantInfo(){ | ||
|
||
} | ||
public ConstantInfo(ConstantPool pool) { | ||
this.pool = pool; | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
group06/1454385822/src/com/coding/basic/homework_04/jvm/constant/ConstantPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.coding.basic.homework_04.jvm.constant; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.coding.basic.homework_04.jvm.info.UTF8Info; | ||
|
||
public class ConstantPool { | ||
private List<ConstantInfo> constantInfos = new ArrayList<ConstantInfo>(); | ||
|
||
public ConstantPool() { | ||
|
||
} | ||
|
||
public void addConstantInfo(ConstantInfo info){ | ||
constantInfos.add(info); | ||
} | ||
|
||
public int getSize() { | ||
return this.constantInfos.size() - 1; | ||
} | ||
|
||
|
||
public ConstantInfo getConstantInfo(int type) { | ||
|
||
return this.constantInfos.get(type); | ||
} | ||
|
||
public String getUTF8String(int index){ | ||
return ((UTF8Info)this.constantInfos.get(index)).getValue(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
group06/1454385822/src/com/coding/basic/homework_04/jvm/info/ClassInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.coding.basic.homework_04.jvm.info; | ||
|
||
import com.coding.basic.homework_04.jvm.constant.ConstantInfo; | ||
import com.coding.basic.homework_04.jvm.constant.ConstantPool; | ||
|
||
public class ClassInfo extends ConstantInfo { | ||
|
||
public ClassInfo(ConstantPool pool) { | ||
super(pool); | ||
// TODO Auto-generated constructor stub | ||
} | ||
public ClassInfo() { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
private int tag = ConstantInfo.CLASS_INFO; | ||
|
||
private int utf8Index; | ||
|
||
public int getTag() { | ||
return tag; | ||
} | ||
|
||
|
||
public int getUtf8Index() { | ||
return utf8Index; | ||
} | ||
|
||
public void setUtf8Index(int name_index) { | ||
this.utf8Index = name_index; | ||
} | ||
public String getClassName() { | ||
int utf8Index = getUtf8Index(); | ||
UTF8Info utf8Info = (UTF8Info)pool.getConstantInfo(utf8Index); | ||
return utf8Info.getValue(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.