String annotation reading/writing

This commit is contained in:
Adam
2015-11-20 15:52:22 -05:00
parent 859f2d1dde
commit 4c0d8de65b
7 changed files with 197 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package net.runelite.deob.attributes;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.runelite.deob.attributes.annotation.Annotation;
public class Annotations extends Attribute
{
private final List<Annotation> annotations = new ArrayList<>();
public Annotations(Attributes attributes)
{
super(attributes, AttributeType.RUNTIMEVISIBLEANNOTATIONS);
}
@Override
public void loadAttribute(DataInputStream is) throws IOException
{
int num_annotations = is.readUnsignedShort();
for (int i = 0; i < num_annotations; ++i)
{
Annotation a = new Annotation(this);
a.load(is);
annotations.add(a);
}
}
@Override
public void writeAttr(DataOutputStream out) throws IOException
{
out.writeShort(annotations.size());
for (Annotation a : annotations)
{
a.write(out);
}
}
}

View File

@@ -5,6 +5,7 @@ public enum AttributeType
CONSTANT_VALUE("ConstantValue", ConstantValue.class),
CODE("Code", Code.class),
EXCEPTIONS("Exceptions", Exceptions.class),
RUNTIMEVISIBLEANNOTATIONS("RuntimeVisibleAnnotations", Annotations.class),
UNKNOWN(null, Unknown.class);
private String name;

View File

@@ -0,0 +1,55 @@
package net.runelite.deob.attributes.annotation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.runelite.deob.ConstantPool;
import net.runelite.deob.attributes.Annotations;
import net.runelite.deob.signature.Type;
public class Annotation
{
private final Annotations annotations;
private Type type;
private List<Element> elements = new ArrayList<>();
public Annotation(Annotations annotations)
{
this.annotations = annotations;
}
public Annotations getAnnotations()
{
return annotations;
}
public void load(DataInputStream is) throws IOException
{
ConstantPool pool = annotations.getAttributes().getClassFile().getPool();
int typeIndex = is.readUnsignedShort();
type = new Type(pool.getUTF8(typeIndex));
int pairs = is.readUnsignedShort();
for (int i = 0; i < pairs; ++i)
{
Element e = new Element(this);
e.load(is);
elements.add(e);
}
}
public void write(DataOutputStream out) throws IOException
{
ConstantPool pool = annotations.getAttributes().getClassFile().getPool();
out.writeShort(pool.makeUTF8(type.toString()));
out.writeShort(elements.size());
for (Element e : elements)
{
e.write(out);
}
}
}

View File

@@ -0,0 +1,45 @@
package net.runelite.deob.attributes.annotation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.runelite.deob.ConstantPool;
import net.runelite.deob.signature.Type;
public class Element
{
private final Annotation annotation;
private Type type;
private String value;
public Element(Annotation annotation)
{
this.annotation = annotation;
}
public void load(DataInputStream is) throws IOException
{
ConstantPool pool = annotation.getAnnotations().getAttributes().getClassFile().getPool();
int typeIndex = is.readShort();
type = new Type(pool.getUTF8(typeIndex));
byte type = is.readByte();
if (type != 's')
throw new RuntimeException("can't parse non string annotation element");
int index = is.readShort(); // pool index to String
value = pool.getUTF8(index);
}
public void write(DataOutputStream out) throws IOException
{
ConstantPool pool = annotation.getAnnotations().getAttributes().getClassFile().getPool();
out.writeShort(pool.makeUTF8(type.toString()));
out.write('s');
out.writeShort(pool.makeUTF8(value));
}
}