Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/class-parser comment work #3

Open
wants to merge 2 commits into
base: feature/class-parser
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lycoris
Submodule Lycoris added at 8f1a91
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public enum AccessFlag {

private char value;

// set up flags reading and their properties
AccessFlag(char value) {
this.value = value;
}

public char getValue() { return value; }
public char setValue(char value) { this.value = value; return value; }


// map values
public static AccessFlag fromValue(final char value) {
for (final AccessFlag flag : AccessFlag.values()) {
if (flag.getValue() == value)
Expand Down Expand Up @@ -65,6 +66,7 @@ public void removeAccessFlag(final AccessFlag flag) {
}
}

// setup decoding attributes to class
private final int magic;
private final char minorVersion;
private final char majorVersion;
Expand Down Expand Up @@ -106,16 +108,22 @@ public ClassWrapper(final int magic, final char minorVersion, final char majorVe
public MethodInfo[] getMethods() { return methods; }
public AttributeInfo[] getAttributes() { return attributes; }


// reading class info header
public static ClassWrapper decode(ByteBuffer buffer) {
int magic = buffer.getInt();
char minorVersion = buffer.getChar();
char majorVersion = buffer.getChar();

// start decoding info
ConstantPool[] constantPool = new ConstantPool[buffer.getChar() - 1];
for (int i = 0; i < constantPool.length; ++i)
constantPool[i] = ConstantPool.decode(buffer);

AccessFlag flag = AccessFlag.fromValue(buffer.getChar());
char thisClass = buffer.getChar();
char superClass = buffer.getChar();

char[] interfaces = new char[buffer.getChar()];
for (int i = 0; i < interfaces.length; ++i)
interfaces[i] = buffer.getChar();
Expand All @@ -128,15 +136,17 @@ public static ClassWrapper decode(ByteBuffer buffer) {
AttributeInfo[] attributes = new AttributeInfo[buffer.getChar()];
for (int i = 0; i < attributes.length; ++i)
attributes[i] = AttributeInfo.decode(constantPool, buffer);
return new ClassWrapper(magic, minorVersion, majorVersion, constantPool, flag, thisClass, superClass, interfaces, fields, methods, attributes);

return new ClassWrapper(magic, minorVersion, majorVersion, constantPool, flag, thisClass, superClass, interfaces, fields, methods, attributes);
}

// error edge case
public static ClassWrapper decode(Path path) throws IOException {
byte[] bytes = Files.readAllBytes(path);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return decode(buffer);
}

// create and encode to bytebuffer
@Override
public void encode(ByteBuffer buffer) {
buffer.putInt(magic);
Expand Down