Search in sources :

Example 16 with Annotation

use of net.runelite.asm.attributes.annotation.Annotation in project runelite by runelite.

the class Inject method injectInterface.

private java.lang.Class injectInterface(ClassFile cf, ClassFile other) {
    Annotations an = cf.getAnnotations();
    if (an == null) {
        return null;
    }
    Annotation a = an.find(DeobAnnotations.IMPLEMENTS);
    if (a == null) {
        return null;
    }
    String ifaceName = API_PACKAGE_BASE + a.getElement().getString();
    java.lang.Class<?> apiClass;
    try {
        apiClass = java.lang.Class.forName(ifaceName);
    } catch (ClassNotFoundException ex) {
        logger.trace("Class {} implements nonexistent interface {}, skipping interface injection", cf.getName(), ifaceName);
        return null;
    }
    // to internal name
    String ifaceNameInternal = ifaceName.replace('.', '/');
    Class clazz = new Class(ifaceNameInternal);
    Interfaces interfaces = other.getInterfaces();
    interfaces.addInterface(clazz);
    return apiClass;
}
Also used : Interfaces(net.runelite.asm.Interfaces) Annotations(net.runelite.asm.attributes.Annotations) DeobAnnotations(net.runelite.deob.DeobAnnotations) Class(net.runelite.asm.pool.Class) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Example 17 with Annotation

use of net.runelite.asm.attributes.annotation.Annotation in project runelite by runelite.

the class InjectHookMethod method findHookLocations.

private List<Integer> findHookLocations(Annotation hook, Method vanillaMethod) throws InjectionException {
    Instructions instructions = vanillaMethod.getCode().getInstructions();
    boolean end = hook.getElements().size() == 2 && hook.getElements().get(1).getValue().equals(true);
    if (end) {
        // find return
        List<Instruction> returns = instructions.getInstructions().stream().filter(i -> i instanceof ReturnInstruction).collect(Collectors.toList());
        List<Integer> indexes = new ArrayList<>();
        for (Instruction ret : returns) {
            int idx = instructions.getInstructions().indexOf(ret);
            assert idx != -1;
            indexes.add(idx);
        }
        return indexes;
    }
    if (!vanillaMethod.getName().equals("<init>")) {
        return Arrays.asList(0);
    }
    // Find index after invokespecial
    for (int i = 0; i < instructions.getInstructions().size(); ++i) {
        Instruction in = instructions.getInstructions().get(i);
        if (in.getType() == InstructionType.INVOKESPECIAL) {
            // one after
            return Arrays.asList(i + 1);
        }
    }
    throw new IllegalStateException("constructor with no invokespecial");
}
Also used : Annotations(net.runelite.asm.attributes.Annotations) Arrays(java.util.Arrays) DeobAnnotations(net.runelite.deob.DeobAnnotations) Logger(org.slf4j.Logger) InstructionType(net.runelite.asm.attributes.code.InstructionType) ReturnInstruction(net.runelite.asm.attributes.code.instruction.types.ReturnInstruction) LoggerFactory(org.slf4j.LoggerFactory) Type(net.runelite.asm.Type) ALoad(net.runelite.asm.attributes.code.instructions.ALoad) Collectors(java.util.stream.Collectors) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) Annotation(net.runelite.asm.attributes.annotation.Annotation) Method(net.runelite.asm.Method) Instructions(net.runelite.asm.attributes.code.Instructions) Signature(net.runelite.asm.signature.Signature) Instruction(net.runelite.asm.attributes.code.Instruction) ReturnInstruction(net.runelite.asm.attributes.code.instruction.types.ReturnInstruction) ArrayList(java.util.ArrayList) Instructions(net.runelite.asm.attributes.code.Instructions) ReturnInstruction(net.runelite.asm.attributes.code.instruction.types.ReturnInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 18 with Annotation

use of net.runelite.asm.attributes.annotation.Annotation in project runelite by runelite.

the class Field method accept.

public void accept(FieldVisitor visitor) {
    for (Annotation annotation : annotations.getAnnotations()) {
        AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true);
        annotation.accept(av);
    }
    visitor.visitEnd();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Example 19 with Annotation

use of net.runelite.asm.attributes.annotation.Annotation in project runelite by runelite.

the class Annotations method addAnnotation.

public Annotation addAnnotation(Type type, String name, Object value) {
    Annotation annotation = new Annotation(this);
    annotation.setType(type);
    addAnnotation(annotation);
    Element element = new Element(annotation);
    element.setName(name);
    element.setValue(value);
    annotation.addElement(element);
    return annotation;
}
Also used : Element(net.runelite.asm.attributes.annotation.Element) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Example 20 with Annotation

use of net.runelite.asm.attributes.annotation.Annotation in project runelite by runelite.

the class MixinInjector method findShadowFields.

/**
 * Find fields which are marked @Shadow, and what they shadow
 *
 * @param mixinClasses
 * @throws InjectionException
 */
private void findShadowFields(Map<Class<?>, List<ClassFile>> mixinClasses) throws InjectionException {
    // Injected static fields take precedence when looking up shadowed fields
    for (Class<?> mixinClass : mixinClasses.keySet()) {
        ClassFile mixinCf;
        try {
            mixinCf = loadClass(mixinClass);
        } catch (IOException ex) {
            throw new InjectionException(ex);
        }
        for (Field field : mixinCf.getFields()) {
            Annotation shadow = field.getAnnotations().find(SHADOW);
            if (shadow != null) {
                if (!field.isStatic()) {
                    throw new InjectionException("Can only shadow static fields");
                }
                // shadow this field
                String shadowName = shadow.getElement().getString();
                Field injectedField = injectedFields.get(shadowName);
                if (injectedField != null) {
                    // Shadow a field injected by a mixin
                    shadowFields.put(field.getPoolField(), injectedField);
                } else {
                    // Shadow a field already in the gamepack
                    Field shadowField = findDeobField(shadowName);
                    if (shadowField == null) {
                        throw new InjectionException("Shadow of nonexistent field " + shadowName);
                    }
                    Field obShadow = inject.toObField(shadowField);
                    assert obShadow != null;
                    shadowFields.put(field.getPoolField(), obShadow);
                }
            }
        }
    }
}
Also used : GetField(net.runelite.asm.attributes.code.instructions.GetField) Field(net.runelite.asm.Field) PutField(net.runelite.asm.attributes.code.instructions.PutField) ClassFile(net.runelite.asm.ClassFile) IOException(java.io.IOException) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Aggregations

Annotation (net.runelite.asm.attributes.annotation.Annotation)21 ClassFile (net.runelite.asm.ClassFile)10 Method (net.runelite.asm.Method)8 Annotations (net.runelite.asm.attributes.Annotations)8 Field (net.runelite.asm.Field)7 Element (net.runelite.asm.attributes.annotation.Element)6 Signature (net.runelite.asm.signature.Signature)6 DeobAnnotations (net.runelite.deob.DeobAnnotations)6 IOException (java.io.IOException)5 Type (net.runelite.asm.Type)5 List (java.util.List)4 GetField (net.runelite.asm.attributes.code.instructions.GetField)4 PutField (net.runelite.asm.attributes.code.instructions.PutField)4 ArrayList (java.util.ArrayList)3 ClassGroup (net.runelite.asm.ClassGroup)3 Instruction (net.runelite.asm.attributes.code.Instruction)3 Class (net.runelite.asm.pool.Class)3 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2