Read the class file and parse the data stream.
Package readclass;
Import Java.io.DataInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import ReadClass.util.UtilMap;
/**
* Simulated virtual machine parsing class file
*
* @author Administrator
*
*/
public class Mypraseclassfile
{
Static char[] Xindex ={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
public static void Main (string[] args)
{
map<string,list> Resultmap = new hashmap<string,list> (); Used to record values in the constants
map<string,classbean> Tempmap = new hashmap<string,classbean> ();
Try
{
DataInputStream stream = new DataInputStream (MyPraseClassFile.class.getResourceAsStream ("exampleclass/ Exampleclass.class "));
Byte[] B = new Byte[4];
int index = 0;
Stream.read (b);
Classbean bean = null;
System.out.println ("Magic----->" +convert0x (b,index,4));
System.out.println ("minor_version----->" +stream.readshort ());
System.out.println ("major_version----->" +stream.readshort ());
int Constantscountt =stream.readshort ();
System.out.println ("Constants_pool_count----->" +constantscountt);
List List = null; //
for (int count =1;count<constantscountt;count++)//traversal constant pool
{
Read constant type
byte Tempbyte = Stream.readbyte ();
List = Resultmap.get (string.valueof (tempbyte));
if (null = = list)
{
List = new ArrayList ();
Resultmap.put (String.valueof (tempbyte), list);
}
Bean = new Classbean ();
Tempmap.put (String.valueof (count), Bean); Used for subsequent lookups
List.add (Bean);
Bean.setindex (count);
Bean.settag (Tempbyte);
Switch (tempbyte)
{
Case 1:short length = Stream.readshort (); b = new Byte[length]; Stream.read (b); Bean.setvalue (New String (b)); Break
Case 3:bean.setvalue (string.valueof (Stream.readint ())); Break
Case 4:bean.setvalue (string.valueof (Stream.readfloat ())); Break
Case 5:bean.setvalue (string.valueof (Stream.readlong ())); count++; Break
Case 6:bean.setvalue (string.valueof (Stream.readdouble ())); count++; Break
Case 7:bean.setrefutf (Stream.readshort ()); Break
Case 8:bean.setrefutf (Stream.readshort ()); Break
Case 9:bean.setrefclass (Stream.readshort ()); Bean.setreftypeandname (Stream.readshort ()); Break
Case 10:bean.setrefclass (Stream.readshort ()); Bean.setreftypeandname (Stream.readshort ()); Break
Case 11:bean.setrefclass (Stream.readshort ()); Bean.setreftypeandname (Stream.readshort ()); Break
Case 12:bean.setrefclass (Stream.readshort ()); Bean.setrefutf (Stream.readshort ()); Break
DEFAULT:SYSTEM.OUT.PRINTLN ("Exception data! ") ;
}
}
}
catch (FileNotFoundException e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
Iterator<string> it = Resultmap.keyset (). Iterator ();
while (It.hasnext ())
{
String key = It.next ();
System.out.println (Utilmap.getconstantmap (). Get (Key) + "----->");
List List = Resultmap.get (key);
for (int i = 0; I<list.size (); i++)
{
Classbean Classbean = (Classbean) list.get (i);
if (null! = Classbean.getvalue ())
{
System.out.println (Classbean.getvalue ());
Continue
}
Switch (Classbean.gettag ())
{
Case 7:system.out.println (Getutfvalue (Tempmap,classbean.getrefutf ())); Break
Case 8:system.out.println (Getutfvalue (Tempmap,classbean.getrefutf ()));
Case 9:system.out.println (Getclassvalue (Tempmap,classbean.getrefclass ()) + "| |" +getreftypeandname (Tempmap,classbean.getreftypeandname ())); break;
Case 10:system.out.println (Getclassvalue (Tempmap,classbean.getrefclass ()) + "| |" +getreftypeandname (Tempmap,classbean.getreftypeandname ())); break;
Case 11:system.out.println (Getclassvalue (Tempmap,classbean.getrefclass ()) + "| |" +getreftypeandname (Tempmap,classbean.getreftypeandname ())); break;
}
}
}
}
/**
* Find the corresponding UTF value directly based on the value of the index
* @param tempmap
*/
private static String Getutfvalue (map<string,classbean> tempmap,int ref)
{
Return Tempmap.get (string.valueof (ref)). GetValue (); Point to UTF value
}
/**
* Find class based on class index, then find the corresponding UTF
* @return
*/
private static String Getclassvalue (map<string,classbean> tempmap,int ref)
{
Classbean bean = Tempmap.get (string.valueof (ref));
if (null! = Bean.getvalue ())
{
return Bean.getvalue ();
}
String value = Tempmap.get (string.valueof (Bean.getrefutf ())). GetValue ();
Bean.setvalue (value);
return value;
}
/**
* Find the corresponding value of the same (class index) according to the Reftypeandname index
* @param tempmap
* @param ref
* @return
*/
private static String Getreftypeandname (map<string,classbean> tempmap,int ref)
{
Classbean bean = Tempmap.get (string.valueof (ref));
if (null! = Bean.getvalue ())
{
return Bean.getvalue ();
}
String value = Tempmap.get (string.valueof (Bean.getrefclass ())). GetValue ();
Bean.setvalue (value);
return value;
}
/**
* Converted to 16 binary
* @param b
* @return
*/
private static String convert0x (byte[] b,int start,int length)
{
if (null = = B | | b.length ==0)
{
return null;
}
StringBuilder sql = new StringBuilder ();
for (int i = start;i<start+length;i++)
{
byte temp = b[i];
Sql.append (xindex[(temp&0xf0) >>4]+ "" +xindex[temp&0x0f]);
}
return sql.tostring ();
}
}
Package readclass.util;
Import Java.util.HashMap;
Import Java.util.Map;
public class Utilmap
{
private static map<string,string> Constantmap = new hashmap<string,string> ();
Static
{
Constantmap.put ("1", "constant_utf-8");
Constantmap.put ("3", "Constant_integer-info");
Constantmap.put ("4", "Constant_float-info");
Constantmap.put ("5", "Constant_double-info");
Constantmap.put ("6", "Constant_long-info");
Constantmap.put ("7", "Constant_class-info");
Constantmap.put ("8", "Constant_string-info");
Constantmap.put ("9", "Constant_fieldref-info");
Constantmap.put ("Ten", "Constant_methodref-info");
Constantmap.put ("One", "constant_interfaceref-info");
Constantmap.put ("n", "Constant_nameandtyperef-info");
}
public static map<string, string> Getconstantmap () {
return constantmap;
}
public static void Setconstantmap (Map<string, string> constantmap) {
Utilmap.constantmap = Constantmap;
}
}
Package readclass;
public class Classbean
{
private int index;//Current owning start index (long,double longer than normal)
private int tag; 1,3,4,5,6,7,8,9,10,11,12
private int refutf; Point to a specific string
private int refclass; Pointer to the class index
The type index that the private int reftypeandname;//points to
private String value; The final value
public int GetIndex ()
{
return index;
}
public int Getrefutf () {
return REFUTF;
}
public void Setrefutf (int refutf) {
This.refutf = Refutf;
}
public void Setindex (int index)
{
This.index = index;
}
public int Gettag ()
{
return tag;
}
public void Settag (int tag)
{
This.tag = tag;
}
public int Getrefclass ()
{
return refclass;
}
public void Setrefclass (int refclass)
{
This.refclass = Refclass;
}
public int Getreftypeandname ()
{
return reftypeandname;
}
public void Setreftypeandname (int reftypeandname)
{
This.reftypeandname = Reftypeandname;
}
Public String GetValue ()
{
return value;
}
public void SetValue (String value)
{
This.value = value;
}
}
Package readclass.exampleclass;
/**
*
* @author Administrator
*
*/
public class ExampleClass implements Comparable<object>
{
Private Long ID;
Private String name =new stringbuffer ("$"). ToString ();
private static Long staticid;
private int k = 0;
Private Double AAA =0.23d;
private static final String constants= "CONSTANTS";
Public Long getId () {
return ID;
}
public void SetId (Long id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Long Getstaticid () {
return staticid;
}
public void Setstaticid (Long staticid) {
This.staticid = Staticid;
}
Public Double getaaa () {
return AAA;
}
public void Setaaa (Double aAA) {
AAA = AAA;
}
public static String getconstants () {
return CONSTANTS;
}
@Override
public int compareTo (Object o) {
TODO auto-generated Method Stub
return 0;
}
}
The execution output is as follows:
Magic----->cafebabe
Minor_version----->0
Major_version----->50
Constants_pool_count----->80
Constant_methodref-info----->
java/lang/object| | <init>
java/lang/stringbuffer| | <init>
java/lang/stringbuffer| | Tostring
java/lang/double| | ValueOf
Constant_utf-8----->
Readclass/exampleclass/exampleclass
Java/lang/object
Java/lang/comparable
Id
Ljava/lang/long;
Name
ljava/lang/string;
Staticid
K
I
Aaa
ljava/lang/double;
CONSTANTS
Constantvalue
Constants
<init>
() V
Code
Java/lang/stringbuffer
22
(ljava/lang/string;) V
Tostring
() ljava/lang/string;
Java/lang/double
ValueOf
D ljava/lang/double;
Linenumbertable
Localvariabletable
This
Lreadclass/exampleclass/exampleclass;
GetId
() Ljava/lang/long;
SetId
(Ljava/lang/long;) V
GetName
SetName
Getstaticid
Setstaticid
Getaaa
() ljava/lang/double;
Setaaa
(ljava/lang/double;) V
Aaa
GetConstants
CompareTo
(Ljava/lang/object;) I
O
Ljava/lang/object;
SourceFile
Exampleclass.java
Signature
Ljava/lang/object; ljava/lang/comparable<ljava/lang/object;>;
Constant_class-info----->
Readclass/exampleclass/exampleclass
Java/lang/object
Java/lang/comparable
Java/lang/stringbuffer
Java/lang/double
Constant_long-info----->
0.23
Constant_fieldref-info----->
readclass/exampleclass/exampleclass| | Name
readclass/exampleclass/exampleclass| | K
readclass/exampleclass/exampleclass| | Aaa
readclass/exampleclass/exampleclass| | Id
readclass/exampleclass/exampleclass| | Staticid
Constant_string-info----->
Constants
22
Constant_nameandtyperef-info----->
<init>
<init>
Tostring
Name
K
ValueOf
Aaa
Id
Staticid
Java class file parsing (i)