added java14 support

This commit is contained in:
Sander Hautvast 2020-07-17 17:33:44 +02:00
parent 0763ad1fd6
commit caf43cfb19
5 changed files with 76 additions and 62 deletions

View file

@ -19,8 +19,8 @@ public class ClassReader extends DataReader {
readConstantPool(in, builder);
skip(in, 6); // u2 access_flags, u2 this_class, u2 super_class
int interfacesCount = readUnsignedShort(in);
skip(in, interfacesCount * 2); // interfaces[]
int interfacesCount = readU16(in);
skip(in, interfacesCount * 2); // interfaces[u2;]
readFields(in, builder);
readMethods(in, builder);
@ -29,7 +29,7 @@ public class ClassReader extends DataReader {
}
private <T> void readConstantPool(DataInputStream in, ClassObject.Builder<T> builder) {
int constantPoolCount = readUnsignedShort(in);
int constantPoolCount = readU16(in);
builder.constantPoolCount(constantPoolCount);
for (int i = 1; i < constantPoolCount; i++) {
builder.constantPoolEntry(readConstantPoolEntry(in));
@ -37,7 +37,7 @@ public class ClassReader extends DataReader {
}
private <T> void readFields(DataInputStream in, ClassObject.Builder<T> builder) {
int fieldInfoCount = readUnsignedShort(in);
int fieldInfoCount = readU16(in);
builder.fieldInfoCount(fieldInfoCount);
for (int i = 0; i < fieldInfoCount; i++) {
builder.fieldInfo(readField(in));
@ -45,7 +45,7 @@ public class ClassReader extends DataReader {
}
private <T> void readMethods(DataInputStream in, ClassObject.Builder<T> builder) {
int methodInfoCount = readUnsignedShort(in);
int methodInfoCount = readU16(in);
builder.methodInfoCount(methodInfoCount);
for (int i = 0; i < methodInfoCount; i++) {
builder.methodInfo(readMethod(in));
@ -53,7 +53,7 @@ public class ClassReader extends DataReader {
}
private <T> Info readField(DataInputStream in) {
Info fieldInfo = new Info(readUnsignedShort(in), readUnsignedShort(in), readUnsignedShort(in), readUnsignedShort(in));
Info fieldInfo = new Info(readU16(in), readU16(in), readU16(in), readU16(in));
for (int i = 0; i < fieldInfo.getAttributesCount(); i++) {
fieldInfo.add(readAttribute(in));
}
@ -62,7 +62,7 @@ public class ClassReader extends DataReader {
}
private <T> Info readMethod(DataInputStream in) {
Info methodInfo = new Info(readUnsignedShort(in), readUnsignedShort(in), readUnsignedShort(in), readUnsignedShort(in));
Info methodInfo = new Info(readU16(in), readU16(in), readU16(in), readU16(in));
for (int i = 0; i < methodInfo.getAttributesCount(); i++) {
methodInfo.add(readAttribute(in));
}
@ -71,8 +71,8 @@ public class ClassReader extends DataReader {
}
private AttributeInfo readAttribute(DataInputStream in) {
int attributeNameIndex = readUnsignedShort(in);
int attributeLength = readInt(in);
int attributeNameIndex = readU16(in);
int attributeLength = readS32(in);
byte[] info;
if (attributeLength > 0) {
info = new byte[attributeLength];
@ -92,19 +92,21 @@ public class ClassReader extends DataReader {
switch (tag) {
case 1: return readUtf8Entry(in);
case 2: throw new IllegalStateException("2: invalid classpool tag");
case 3: return new IntEntry(readInt(in));
case 4: return new FloatEntry(readFloat(in));
case 5: return new LongEntry(readLong(in));
case 6: return new DoubleEntry(readDouble(in));
case 7: return new ClassEntry(readUnsignedShort(in));
case 8: return new StringEntry(readUnsignedShort(in));
case 9: return new FieldRefEntry(readShort(in), readShort(in));
case 10: return new MethodRefEntry(readUnsignedShort(in), readUnsignedShort(in));
case 11: return new InterfaceMethodRefEntry(readUnsignedShort(in), readUnsignedShort(in));
case 12: return new NameAndTypeEntry(readUnsignedShort(in), readUnsignedShort(in));
case 15: return new MethodHandleEntry(readUnsignedShort(in), readUnsignedShort(in));
case 16: return new MethodTypeEntry(readUnsignedShort(in));
case 18: return new InvokeDynamicEntry(readUnsignedShort(in), readUnsignedShort(in));
case 3: return new IntEntry(readS32(in));
case 4: return new FloatEntry(readF32(in));
case 5: return new LongEntry(readS64(in));
case 6: return new DoubleEntry(readF64(in));
case 7: return new ClassEntry(readU16(in));
case 8: return new StringEntry(readU16(in));
case 9: return new FieldRefEntry(readU16(in), readU16(in));
case 10: return new MethodRefEntry(readU16(in), readU16(in));
case 11: return new InterfaceMethodRefEntry(readU16(in), readU16(in));
case 12: return new NameAndTypeEntry(readU16(in), readU16(in));
case 15: return new MethodHandleEntry(readU16(in), readU16(in));
case 16: return new MethodTypeEntry(readU16(in));
case 18: return new InvokeDynamicEntry(readU16(in), readU16(in));
case 19: return new ModuleEntry(readU16(in));
case 20: return new PackageEntry(readU16(in));
default: throw new IllegalStateException("invalid classpool");
}
}

View file

@ -9,29 +9,7 @@ import java.nio.charset.StandardCharsets;
public class DataReader {
protected float readFloat(DataInputStream in) {
try {
return in.readFloat();
} catch (IOException e) {
throw new RuntimeException();
}
}
protected Utf8Entry readUtf8Entry(DataInputStream in) {
short length = readShort(in);
return new Utf8Entry(readString(in, length));
}
protected String readString(DataInputStream in, short length) {
try {
byte[] bytes = in.readNBytes(length);
return new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException();
}
}
protected long readLong(DataInputStream in) {
protected long readS64(DataInputStream in) {
try {
return in.readLong();
} catch (IOException e) {
@ -39,7 +17,15 @@ public class DataReader {
}
}
protected double readDouble(DataInputStream in) {
protected float readF32(DataInputStream in) {
try {
return in.readFloat();
} catch (IOException e) {
throw new RuntimeException();
}
}
protected double readF64(DataInputStream in) {
try {
return in.readDouble();
} catch (IOException e) {
@ -47,15 +33,7 @@ public class DataReader {
}
}
protected short readShort(DataInputStream in) {
try {
return in.readShort();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected int readUnsignedShort(DataInputStream in) {
protected int readU16(DataInputStream in) {
try {
return in.readUnsignedShort();
} catch (IOException e) {
@ -63,7 +41,7 @@ public class DataReader {
}
}
protected int readInt(DataInputStream in) {
protected int readS32(DataInputStream in) {
try {
return in.readInt();
} catch (IOException e) {
@ -71,6 +49,20 @@ public class DataReader {
}
}
protected Utf8Entry readUtf8Entry(DataInputStream in) {
int length = readU16(in);
return new Utf8Entry(readString(in, length));
}
protected String readString(DataInputStream in, int length) {
try {
byte[] bytes = in.readNBytes(length);
return new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException();
}
}
protected byte readByte(DataInputStream in) {
try {
return in.readByte();

View file

@ -1,19 +1,19 @@
package nl.sander.jsontoy2.java.constantpool;
public class FieldRefEntry extends ConstantPoolEntry {
private final short classIndex;
private final short nameAndTypeIndex;
private final int classIndex;
private final int nameAndTypeIndex;
public FieldRefEntry(short classIndex, short nameAndTypeIndex) {
public FieldRefEntry(int classIndex, int nameAndTypeIndex) {
this.classIndex = classIndex;
this.nameAndTypeIndex = nameAndTypeIndex;
}
public short getClassIndex() {
public int getClassIndex() {
return classIndex;
}
public short getNameAndTypeIndex() {
public int getNameAndTypeIndex() {
return nameAndTypeIndex;
}

View file

@ -0,0 +1,11 @@
package nl.sander.jsontoy2.java.constantpool;
import nl.sander.jsontoy2.java.constantpool.ConstantPoolEntry;
public class ModuleEntry extends ConstantPoolEntry {
private final int nameIndex;
public ModuleEntry(int nameIndex) {
this.nameIndex = nameIndex;
}
}

View file

@ -0,0 +1,9 @@
package nl.sander.jsontoy2.java.constantpool;
public class PackageEntry extends ConstantPoolEntry {
private final int nameIndex;
public PackageEntry(int nameIndex) {
this.nameIndex = nameIndex;
}
}