案例介绍
本案例主要介绍通过java代码从class文件中解析;class文件、常量池、属性表;
作为类(或者接口)信息的载体,每个class文件都完整地定义了一个类。为了使java程序可以“编写一次,处处运行”,Java虚拟机规范对class文件格式进行了严格的规定。但是另外一方面,对于从哪里加载class文件,给了足够多的自由。Java虚拟机实现可以从文件系统读取和从JAR(或ZIP)压缩包中提取clss文件。除此之外,也可以通过网络下载、从数据库加载,甚至是在运行中直接生成class文件。Java虚拟机规范中所指的class文件,并非特指位于磁盘中的.class文件,而是泛指任何格式符号规范的class数据。
环境准备
1、jdk 1.8.0
2、IntelliJ IDEA Community Edition 2018.3.1 x64
配置信息
1、调试配置
2.1、配置位置:Run/Debug Configurations -> program arguments
2.2、配置内容:-Xjre “C:\Program Files\Java\jdk1.8.0_161\jre” java.lang.String
代码示例
itstack-demo-jvm-03
├── pom.xml
└── src
└── main
│ └── java
│ └── org.itstack.demo.jvm
│ ├── classfile
│ │ ├── attributes {BootstrapMethods/Code/ConstantValue...}
│ │ ├── constantpool {CONSTANT_TAG_CLASS/CONSTANT_TAG_FIELDREF/CONSTANT_TAG_METHODREF...}
│ │ ├── ClassFile.java
│ │ ├── ClassReader.java
│ │ └── MemberInfo.java
│ ├── classpath
│ │ ├── impl
│ │ │ ├── CompositeEntry.java
│ │ │ ├── DirEntry.java
│ │ │ ├── WildcardEntry.java
│ │ │ └── ZipEntry.java
│ │ ├── Classpath.java
│ │ └── Entry.java
│ ├── Cmd.java
│ └── Main.java
└── test
└── java
└── org.itstack.demo.test
└── HelloWorld.java
代码篇幅较长,不一一列举
AttributeInfo.java
package org.itstack.demo.jvm.classfile.attributes;
import org.itstack.demo.jvm.classfile.ClassReader;
import org.itstack.demo.jvm.classfile.attributes.impl.*;
import org.itstack.demo.jvm.classfile.constantpool.ConstantPool;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/26
*/
public interface AttributeInfo {
void readInfo(ClassReader reader);
static AttributeInfo[] readAttributes(ClassReader reader, ConstantPool constantPool) {
int attributesCount = reader.readU2ToInt();
AttributeInfo[] attributes = new AttributeInfo[attributesCount];
for (int i = 0; i < attributesCount; i++) {
attributes[i] = readAttribute(reader, constantPool);
}
return attributes;
}
static AttributeInfo readAttribute(ClassReader reader, ConstantPool constantPool) {
int attrNameIdx = reader.readU2ToInt();
String attrName = constantPool.getUTF8(attrNameIdx);
int attrLen = reader.readU4ToInt();
AttributeInfo attrInfo = newAttributeInfo(attrName, attrLen, constantPool);
attrInfo.readInfo(reader);
return attrInfo;
}
static AttributeInfo newAttributeInfo(String attrName, int attrLen, ConstantPool constantPool) {
switch (attrName) {
case "BootstrapMethods":
return new BootstrapMethodsAttribute();
case "Code":
return new CodeAttribute(constantPool);
case "ConstantValue":
return new ConstantValueAttribute();
case "Deprecated":
return new DeprecatedAttribute();
case "EnclosingMethod":
return new EnclosingMethodAttribute(constantPool);
case "Exceptions":
return new ExceptionsAttribute();
case "InnerClasses":
return new InnerClassesAttribute();
case "LineNumberTable":
return new LineNumberTableAttribute();
case "LocalVariableTable":
return new LocalVariableTableAttribute();
case "LocalVariableTypeTable":
return new LocalVariableTypeTableAttribute();
// case "MethodParameters":
// case "RuntimeInvisibleAnnotations":
// case "RuntimeInvisibleParameterAnnotations":
// case "RuntimeInvisibleTypeAnnotations":
// case "RuntimeVisibleAnnotations":
// case "RuntimeVisibleParameterAnnotations":
// case "RuntimeVisibleTypeAnnotations":
case "Signature":
return new SignatureAttribute(constantPool);
case "SourceFile":
return new SourceFileAttribute(constantPool);
// case "SourceDebugExtension":
// case "StackMapTable":
case "Synthetic":
return new SyntheticAttribute();
default:
return new UnparsedAttribute(attrName, attrLen);
}
}
}
ConstantInfo.java
package org.itstack.demo.jvm.classfile.constantpool;
import org.itstack.demo.jvm.classfile.ClassReader;
import org.itstack.demo.jvm.classfile.constantpool.impl.*;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/26
*/
public interface ConstantInfo {
int CONSTANT_TAG_CLASS = 7;
int CONSTANT_TAG_FIELDREF = 9;
int CONSTANT_TAG_METHODREF = 10;
int CONSTANT_TAG_INTERFACEMETHODREF = 11;
int CONSTANT_TAG_STRING = 8;
int CONSTANT_TAG_INTEGER = 3;
int CONSTANT_TAG_FLOAT = 4;
int CONSTANT_TAG_LONG = 5;
int CONSTANT_TAG_DOUBLE = 6;
int CONSTANT_TAG_NAMEANDTYPE = 12;
int CONSTANT_TAG_UTF8 = 1;
int CONSTANT_TAG_METHODHANDLE = 15;
int CONSTANT_TAG_METHODTYPE = 16;
int CONSTANT_TAG_INVOKEDYNAMIC = 18;
void readInfo(ClassReader reader);
int tag();
static ConstantInfo readConstantInfo(ClassReader reader, ConstantPool constantPool) {
int tag = reader.readU1ToInt();
ConstantInfo constantInfo = newConstantInfo(tag, constantPool);
constantInfo.readInfo(reader);
return constantInfo;
}
static ConstantInfo newConstantInfo(int tag, ConstantPool constantPool) {
switch (tag) {
case CONSTANT_TAG_INTEGER:
return new ConstantIntegerInfo();
case CONSTANT_TAG_FLOAT:
return new ConstantFloatInfo();
case CONSTANT_TAG_LONG:
return new ConstantLongInfo();
case CONSTANT_TAG_DOUBLE:
return new ConstantDoubleInfo();
case CONSTANT_TAG_UTF8:
return new ConstantUtf8Info();
case CONSTANT_TAG_STRING:
return new ConstantStringInfo(constantPool);
case CONSTANT_TAG_CLASS:
return new ConstantClassInfo(constantPool);
case CONSTANT_TAG_FIELDREF:
return new ConstantFieldRefInfo(constantPool);
case CONSTANT_TAG_METHODREF:
return new ConstantMethodRefInfo(constantPool);
case CONSTANT_TAG_INTERFACEMETHODREF:
return new ConstantInterfaceMethodRefInfo(constantPool);
case CONSTANT_TAG_NAMEANDTYPE:
return new ConstantNameAndTypeInfo();
case CONSTANT_TAG_METHODTYPE:
return new ConstantMethodTypeInfo();
case CONSTANT_TAG_METHODHANDLE:
return new ConstantMethodHandleInfo();
case CONSTANT_TAG_INVOKEDYNAMIC:
return new ConstantInvokeDynamicInfo();
default:
throw new ClassFormatError("constant pool tag");
}
}
}
ClassFile.java
package org.itstack.demo.jvm.classfile;
import org.itstack.demo.jvm.classfile.attributes.AttributeInfo;
import org.itstack.demo.jvm.classfile.constantpool.ConstantPool;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/26
*/
public class ClassFile {
private int minorVersion;
private int majorVersion;
private ConstantPool constantPool;
private int accessFlags;
private int thisClassIdx;
private int supperClassIdx;
private int[] interfaces;
private MemberInfo[] fields;
private MemberInfo[] methods;
private AttributeInfo[] attributes;
public ClassFile(byte[] classData) {
ClassReader reader = new ClassReader(classData);
this.readAndCheckMagic(reader);
this.readAndCheckVersion(reader);
this.constantPool = this.readConstantPool(reader);
this.accessFlags = reader.readU2ToInt();
this.thisClassIdx = reader.readU2ToInt();
this.supperClassIdx = reader.readU2ToInt();
this.interfaces = reader.readUInt16s();
this.fields = MemberInfo.readMembers(reader, constantPool);
this.methods = MemberInfo.readMembers(reader, constantPool);
this.attributes = AttributeInfo.readAttributes(reader, constantPool);
}
private void readAndCheckMagic(ClassReader reader) {
String magic = reader.readU4ToHexStr();
if (!"cafebabe".equals(magic)) {
throw new ClassFormatError("magic!");
}
}
private void readAndCheckVersion(ClassReader reader) {
this.minorVersion = reader.readU2ToInt();
this.majorVersion = reader.readU2ToInt();
switch (this.majorVersion) {
case 45:
return;
case 46:
case 47:
case 48:
case 49:
case 50:
case 51:
case 52:
if (this.minorVersion == 0)
return;
}
throw new UnsupportedClassVersionError();
}
private ConstantPool readConstantPool(ClassReader reader) {
return new ConstantPool(reader);
}
public int minorVersion(){
return this.minorVersion;
}
public int majorVersion(){
return this.majorVersion;
}
public ConstantPool constantPool(){
return this.constantPool;
}
public int accessFlags() {
return this.accessFlags;
}
public MemberInfo[] fields() {
return this.fields;
}
public MemberInfo[] methods() {
return this.methods;
}
public String className() {
return this.constantPool.getClassName(this.thisClassIdx);
}
public String superClassName() {
if (this.supperClassIdx <= 0) return "";
return this.constantPool.getClassName(this.supperClassIdx);
}
public String[] interfaceNames() {
String[] interfaceNames = new String[this.interfaces.length];
for (int i = 0; i < this.interfaces.length; i++) {
interfaceNames[i] = this.constantPool.getClassName(interfaces[i]);
}
return interfaceNames;
}
}
ClassReader.java
package org.itstack.demo.jvm.classfile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/26
*/
public class ClassReader {
private byte[] data;
private int cursor; //游标;读取后记录位置
public ClassReader(byte[] data) {
this.data = data;
}
public int readU1ToInt() {
return ReaderUtil.byteToInt(new byte[]{data[cursor++]});
}
public int readU2ToInt() {
return ReaderUtil.byteToInt(new byte[]{data[cursor++], data[cursor++]});
}
public int readU4ToInt() {
return ReaderUtil.byteToInt(new byte[]{data[cursor++], data[cursor++], data[cursor++], data[cursor++]});
}
public float readU4ToFloat() {
byte[] bytes = readBytes(4);
return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getFloat();
}
//16进制字符串
public String readU4ToHexStr() {
return ReaderUtil.byteToHexString((new byte[]{data[cursor++], data[cursor++], data[cursor++], data[cursor++]}));
}
public long read2U4ToLong() {
byte[] bytes = readBytes(8);
return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getLong();
}
public double read2U4Double() {
byte[] bytes = readBytes(8);
return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getDouble();
}
public int[] readUInt16s() {
int count = readU2ToInt();
int[] result = new int[count];
for (int i = 0; i < count; i++) {
result[i] = readU2ToInt();
}
return result;
}
public void back(int n) {
this.cursor -= n;
}
public byte[] readBytes(int len) {
if (cursor + len >= data.length) {
throw new ArrayIndexOutOfBoundsException();
}
byte[] dataCopy = Arrays.copyOfRange(this.data, cursor, cursor + len);
cursor += len;
return dataCopy;
}
static class ReaderUtil {
static int byteToInt(byte[] codes) {
String str = byteToHexString(codes);
return Integer.valueOf(str, 16);
}
static String byteToHexString(byte[] codes) {
StringBuilder sb = new StringBuilder();
for (byte b : codes) {
int value = b & 0xFF;
String strHex = Integer.toHexString(value);
if (strHex.length() < 2) {
strHex = "0" + strHex;
}
sb.append(strHex);
}
return sb.toString();
}
}
}
MemberInfo.java
package org.itstack.demo.jvm.classfile;
import org.itstack.demo.jvm.classfile.attributes.AttributeInfo;
import org.itstack.demo.jvm.classfile.attributes.impl.CodeAttribute;
import org.itstack.demo.jvm.classfile.attributes.impl.ConstantValueAttribute;
import org.itstack.demo.jvm.classfile.constantpool.ConstantPool;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/26
*/
public class MemberInfo {
private ConstantPool constantPool;
private int accessFlags;
private int nameIdx;
private int descriptorIdx;
private AttributeInfo[] attributes;
public MemberInfo(ClassReader reader, ConstantPool constantPool) {
this.constantPool = constantPool;
this.accessFlags = reader.readU2ToInt();
this.nameIdx = reader.readU2ToInt();
this.descriptorIdx = reader.readU2ToInt();
this.attributes = AttributeInfo.readAttributes(reader, constantPool);
}
public static MemberInfo[] readMembers(ClassReader reader, ConstantPool constantPool) {
int fieldCount = reader.readU2ToInt();
MemberInfo[] fields = new MemberInfo[fieldCount];
for (int i = 0; i < fieldCount; i++) {
fields[i] = new MemberInfo(reader, constantPool);
}
return fields;
}
public int accessFlags() {
return this.accessFlags;
}
public String name() {
return this.constantPool.getUTF8(this.nameIdx);
}
public String descriptor() {
return this.constantPool.getUTF8(this.descriptorIdx);
}
public CodeAttribute codeAttribute() {
for (AttributeInfo attrInfo : attributes) {
if (attrInfo instanceof CodeAttribute) return (CodeAttribute) attrInfo;
}
return null;
}
public ConstantValueAttribute ConstantValueAttribute() {
for (AttributeInfo attrInfo : attributes) {
if (attrInfo instanceof ConstantValueAttribute) return (ConstantValueAttribute) attrInfo;
}
return null;
}
}
Main.java
package org.itstack.demo.jvm;
import org.itstack.demo.jvm.classfile.ClassFile;
import org.itstack.demo.jvm.classfile.MemberInfo;
import org.itstack.demo.jvm.classpath.Classpath;
import java.util.Arrays;
/**
* http://www.itstack.org
* create by fuzhengwei on 2019/4/24
*/
public class Main {
public static void main(String[] args) {
Cmd cmd = Cmd.parse(args);
if (!cmd.ok || cmd.helpFlag) {
System.out.println("Usage: <main class> [-options] class [args...]");
return;
}
if (cmd.versionFlag) {
//注意案例测试都是基于1.8,另外jdk1.9以后使用模块化没有rt.jar
System.out.println("java version \"1.8.0\"");
return;
}
startJVM(cmd);
}
private static void startJVM(Cmd cmd) {
Classpath classpath = new Classpath(cmd.jre, cmd.classpath);
System.out.printf("classpath:%s class:%s args:%s\n",
classpath, cmd.getMainClass(), cmd.getAppArgs());
//获取className
String className = cmd.getMainClass().replace(".", "/");
ClassFile classFile = loadClass(className, classpath);
assert classFile != null;
printClassInfo(classFile);
}
private static ClassFile loadClass(String className, Classpath classpath) {
try {
byte[] classData = classpath.readClass(className);
return new ClassFile(classData);
} catch (Exception e) {
System.out.println("Could not find or load main class " + className);
return null;
}
}
private static void printClassInfo(ClassFile cf) {
System.out.println("version: " + cf.majorVersion() + "." + cf.minorVersion());
System.out.println("constants count:" + cf.constantPool().getSiz());
System.out.format("access flags:0x%x\n", cf.accessFlags());
System.out.println("this class:" + cf.className());
System.out.println("super class:" + cf.superClassName());
System.out.println("interfaces:" + Arrays.toString(cf.interfaceNames()));
System.out.println("fields count:" + cf.fields().length);
for (MemberInfo memberInfo : cf.fields()) {
System.out.format(" %s\n", memberInfo.name());
}
System.out.println("methods count: " + cf.methods().length);
for (MemberInfo memberInfo : cf.methods()) {
System.out.format(" %s\n", memberInfo.name());
}
}
}
测试结果
classpath:org.itstack.demo.jvm.classpath.Classpath@4bf558aa class:java.lang.String args:null
version: 52.0
constants count:540
access flags:0x31
this class:java/lang/String
super class:java/lang/Object
interfaces:[java/io/Serializable, java/lang/Comparable, java/lang/CharSequence]
fields count:5
value
hash
serialVersionUID
serialPersistentFields
CASE_INSENSITIVE_ORDER
methods count: 94
<init>
<init>
<init>
<init>
<init>
<init>
<init>
checkBounds
<init>
<init>
<init>
<init>
<init>
<init>
<init>
<init>
<init>
length
isEmpty
charAt
codePointAt
codePointBefore
codePointCount
offsetByCodePoints
getChars
getChars
getBytes
getBytes
getBytes
getBytes
equals
contentEquals
nonSyncContentEquals
contentEquals
equalsIgnoreCase
compareTo
compareToIgnoreCase
regionMatches
regionMatches
startsWith
startsWith
endsWith
hashCode
indexOf
indexOf
indexOfSupplementary
lastIndexOf
lastIndexOf
lastIndexOfSupplementary
indexOf
indexOf
indexOf
indexOf
lastIndexOf
lastIndexOf
lastIndexOf
lastIndexOf
substring
substring
subSequence
concat
replace
matches
contains
replaceFirst
replaceAll
replace
split
split
join
join
toLowerCase
toLowerCase
toUpperCase
toUpperCase
trim
toString
toCharArray
format
format
valueOf
valueOf
valueOf
copyValueOf
copyValueOf
valueOf
valueOf
valueOf
valueOf
valueOf
valueOf
intern
compareTo
<clinit>