classfile

This commit is contained in:
Imgaojp 2017-02-18 14:26:07 +08:00
parent cacf9b8226
commit 4f24288e20
18 changed files with 931 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrCode implements AttributeInfo {
char maxStack, maxLocals;
byte[] code;
ConstantPool constantPool;
AttributeInfo[] attributes;
ExceptionTableEntry[] exceptionTableEntries;
public AttrCode(ConstantPool constantPool) {
this.constantPool = constantPool;
}
@Override
public void readInfo(ClassReader classReader) {
maxStack = classReader.readUint16();
maxLocals = classReader.readUint16();
int codeLength = classReader.readUint32();
code = classReader.readBytes(codeLength);
exceptionTableEntries = readExceptionTable(classReader);
attributes = ClassFile.readAttributes(classReader, constantPool);
}
public ExceptionTableEntry[] readExceptionTable(ClassReader classReader) {
char exceptionTableLength = classReader.readUint16();
ExceptionTableEntry[] exceptionTableEntries = new ExceptionTableEntry[exceptionTableLength];
for (int i = 0; i < exceptionTableLength; i++) {
exceptionTableEntries[i] = new ExceptionTableEntry(classReader.readUint16(), classReader.readUint16(), classReader.readUint16(), classReader.readUint16());
}
return exceptionTableEntries;
}
class ExceptionTableEntry {
char startPc, endPc, handlerPc, catchType;
ExceptionTableEntry(char startPc, char endPc, char handlerPc, char catchType) {
this.startPc = startPc;
this.endPc = endPc;
this.handlerPc = handlerPc;
this.catchType = catchType;
}
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrConstantValue implements AttributeInfo {
char constantValueIndex;
@Override
public void readInfo(ClassReader classReader) {
constantValueIndex = classReader.readUint16();
}
public char getConstantValueIndex() {
return constantValueIndex;
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrDeprecated extends AttrMarker {
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrExceptions implements AttributeInfo {
char[] exceptionIndexTable;
@Override
public void readInfo(ClassReader classReader) {
exceptionIndexTable = classReader.readUint16s();
}
public char[] getExceptionIndexTable() {
return exceptionIndexTable;
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrLineNumberTable implements AttributeInfo {
LineNumberTableEntry[] lineNumberTable;
@Override
public void readInfo(ClassReader classReader) {
char lineNumberTableLength = classReader.readUint16();
lineNumberTable = new LineNumberTableEntry[lineNumberTableLength];
for (int i = 0; i < lineNumberTableLength; i++) {
lineNumberTable[i] = new LineNumberTableEntry(classReader.readUint16(), classReader.readUint16());
}
}
class LineNumberTableEntry {
char startPc, lineNumber;
LineNumberTableEntry(char startPc, char lineNumber) {
this.startPc = startPc;
this.lineNumber = lineNumber;
}
public char getStartPc() {
return startPc;
}
public void setStartPc(char startPc) {
this.startPc = startPc;
}
public char getLineNumber() {
return lineNumber;
}
public void setLineNumber(char lineNumber) {
this.lineNumber = lineNumber;
}
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrLocalVariableTable implements AttributeInfo {
LocalVariableTableEntry[] localVariableTable;
@Override
public void readInfo(ClassReader classReader) {
char localVariableTableLength = classReader.readUint16();
localVariableTable = new LocalVariableTableEntry[localVariableTableLength];
for (int i = 0; i < localVariableTableLength; i++) {
localVariableTable[i] = new LocalVariableTableEntry(classReader.readUint16(), classReader.readUint16(), classReader.readUint16(), classReader.readUint16(), classReader.readUint16());
}
}
class LocalVariableTableEntry {
char startPc, length, nameIndex, descriptorIndex, index;
LocalVariableTableEntry(char startPc, char length, char nameIndex, char descriptorIndex, char index) {
this.startPc = startPc;
this.length = length;
this.nameIndex = nameIndex;
this.descriptorIndex = descriptorIndex;
this.index = index;
}
public char getStartPc() {
return startPc;
}
public void setStartPc(char startPc) {
this.startPc = startPc;
}
public char getLength() {
return length;
}
public void setLength(char length) {
this.length = length;
}
public char getNameIndex() {
return nameIndex;
}
public void setNameIndex(char nameIndex) {
this.nameIndex = nameIndex;
}
public char getDescriptorIndex() {
return descriptorIndex;
}
public void setDescriptorIndex(char descriptorIndex) {
this.descriptorIndex = descriptorIndex;
}
public char getIndex() {
return index;
}
public void setIndex(char index) {
this.index = index;
}
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrMarker implements AttributeInfo {
@Override
public void readInfo(ClassReader classReader) {
//read nothing
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrSourceFile implements AttributeInfo {
ConstantPool constantPool;
char sourceFileIndex;
public AttrSourceFile(ConstantPool constantPool) {
this.constantPool = constantPool;
}
@Override
public void readInfo(ClassReader classReader) {
sourceFileIndex = classReader.readUint16();
}
public String getFileName() {
return constantPool.getUTF8(sourceFileIndex);
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrSynthetic extends AttrMarker {
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class AttrUnparsed implements AttributeInfo {
String name;
int length;
byte[] info;
public AttrUnparsed(String attrName, int attrlen) {
name = attrName;
length = attrlen;
}
@Override
public void readInfo(ClassReader classReader) {
info = classReader.readBytes(length);
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantClassInfo extends ConstantInfo {
ConstantPool constantPool;
char nameIndex;
public ConstantClassInfo(ConstantPool constantPool) {
this.constantPool = constantPool;
}
@Override
public void readInfo(ClassReader classReader) {
nameIndex = classReader.readUint16();
}
public String getName() {
return constantPool.getUTF8(nameIndex);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantFieldrefInfo extends ConstantMemberrefInfo {
public ConstantFieldrefInfo(ConstantPool constantPool) {
super(constantPool);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantInterfaceMemberrefInfo extends ConstantMemberrefInfo {
public ConstantInterfaceMemberrefInfo(ConstantPool constantPool) {
super(constantPool);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantMemberrefInfo extends ConstantInfo {
ConstantPool constantPool;
char classIndex, nameAndTypeIndex;
public ConstantMemberrefInfo() {
}
public ConstantMemberrefInfo(ConstantPool constantPool) {
this.constantPool = constantPool;
}
@Override
public void readInfo(ClassReader classReader) {
classIndex = classReader.readUint16();
nameAndTypeIndex = classReader.readUint16();
}
public String className() {
return constantPool.getClassName(classIndex);
}
public String[] nameAndDescriptor() {
return constantPool.getNameAndType(nameAndTypeIndex);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantMethodrefInfo extends ConstantMemberrefInfo {
public ConstantMethodrefInfo(ConstantPool constantPool) {
super(constantPool);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantNameAndTypeInfo extends ConstantInfo {
char nameIndex, descriptorIndex;
@Override
public void readInfo(ClassReader classReader) {
nameIndex = classReader.readUint16();
descriptorIndex = classReader.readUint16();
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantStringInfo extends ConstantInfo {
private ConstantPool constantPool;
private char stringIndex;
public ConstantStringInfo(ConstantPool constantPool) {
this.constantPool = constantPool;
}
@Override
public void readInfo(ClassReader classReader) {
stringIndex = classReader.readUint16();
}
public String getString() {
return constantPool.getUTF8(stringIndex);
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.gabongao.jvm.classfile;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/18.
*/
public class ConstantUtf8Info extends ConstantInfo {
private String string;
@Override
public void readInfo(ClassReader classReader) {
int length = classReader.readUint16();
byte[] bytes = classReader.readBytes(length);
string = decodeMUTF8(bytes);
}
private String decodeMUTF8(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getString() {
return string;
}
}