Finishing touches to annotations

This commit is contained in:
Lucwousin
2020-07-19 03:21:06 +02:00
parent 663bbc80db
commit 86f6886d8c
3 changed files with 39 additions and 15 deletions

View File

@@ -26,6 +26,7 @@ package net.runelite.asm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.AnnotationVisitor;
@@ -59,6 +60,12 @@ public class Annotation extends AnnotationVisitor implements Comparable<Annotati
this(type, true); this(type, true);
} }
public Annotation(Type type, Object val)
{
this(type);
this.setElement(val);
}
public Annotation(Type type, boolean visible) public Annotation(Type type, boolean visible)
{ {
super(Opcodes.ASM5); super(Opcodes.ASM5);
@@ -92,6 +99,11 @@ public class Annotation extends AnnotationVisitor implements Comparable<Annotati
setElement("value", value); setElement("value", value);
} }
public Object remove(String name)
{
return data.remove(name);
}
public Object get(String name) public Object get(String name)
{ {
return data.get(name); return data.get(name);
@@ -192,4 +204,14 @@ public class Annotation extends AnnotationVisitor implements Comparable<Annotati
{ {
return this.type.toString().compareTo(that.type.toString()); return this.type.toString().compareTo(that.type.toString());
} }
@Override
public String toString()
{
StringBuilder str = new StringBuilder('@' + type.toAsmString().replace('/', '.') + '(');
for (Map.Entry<String, Object> e : data.entrySet())
str.append(e.getKey()).append('=').append(e.getValue().toString());
str.append(')');
return str.toString();
}
} }

View File

@@ -38,7 +38,7 @@ public interface Annotated
default void addAnnotation(Type type, Object val) default void addAnnotation(Type type, Object val)
{ {
addAnnotation(type, val); getAnnotations().put(type, new Annotation(type, val));
} }
default void addAnnotation(Type type, String key, Object val) default void addAnnotation(Type type, String key, Object val)

View File

@@ -41,26 +41,25 @@ public class DeobAnnotations
public static final Type IMPLEMENTS = new Type("Lnet/runelite/mapping/Implements;"); public static final Type IMPLEMENTS = new Type("Lnet/runelite/mapping/Implements;");
public static final Type OBFUSCATED_GETTER = new Type("Lnet/runelite/mapping/ObfuscatedGetter;"); public static final Type OBFUSCATED_GETTER = new Type("Lnet/runelite/mapping/ObfuscatedGetter;");
public static final Type OBFUSCATED_SIGNATURE = new Type("Lnet/runelite/mapping/ObfuscatedSignature;"); public static final Type OBFUSCATED_SIGNATURE = new Type("Lnet/runelite/mapping/ObfuscatedSignature;");
public static final Type HOOK = new Type("Lnet/runelite/mapping/Hook;");
public static Signature getObfuscatedSignature(Method m) public static Signature getObfuscatedSignature(Method m)
{ {
String str = getStringValue(m, OBFUSCATED_SIGNATURE); Object val = get(m, OBFUSCATED_SIGNATURE, "descriptor");
if (str == null) if (val == null)
return null; return null;
return new Signature(str); return new Signature((String) val);
} }
public static Type getObfuscatedType(Field f) public static Type getObfuscatedType(Field f)
{ {
String str = getStringValue(f, OBFUSCATED_SIGNATURE); Object val = get(f, OBFUSCATED_SIGNATURE, "descriptor");
if (str == null) if (val == null)
return null; return null;
return new Type(str); return new Type((String) val);
} }
@Nullable @Nullable
@@ -88,14 +87,10 @@ public class DeobAnnotations
if (a == null) if (a == null)
return null; return null;
Object v = a.getValue();
if (v == null)
return null;
if (field.getType().equals(Type.INT)) if (field.getType().equals(Type.INT))
return (Integer) v; return (Integer) a.get("intValue");
else if (field.getType().equals(Type.LONG)) if (field.getType().equals(Type.LONG))
return (Long) v; // very long v return (Long) a.get("longValue"); // very long v
throw new IllegalArgumentException("Field with getter but not a long or an int?"); throw new IllegalArgumentException("Field with getter but not a long or an int?");
} }
@@ -106,6 +101,13 @@ public class DeobAnnotations
return a == null ? null : (String) a.get("garbageValue"); return a == null ? null : (String) a.get("garbageValue");
} }
@Nullable
public static Object get(Annotated an, Type type, String name)
{
final var a = an.findAnnotation(type);
return a == null ? null : a.get(name);
}
@Nullable @Nullable
public static String getStringValue(Annotated an, Type type) public static String getStringValue(Annotated an, Type type)
{ {