Example usage for org.objectweb.asm.tree ClassNode visitMethod

List of usage examples for org.objectweb.asm.tree ClassNode visitMethod

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode visitMethod.

Prototype

@Override
    public MethodVisitor visitMethod(final int access, final String name, final String descriptor,
            final String signature, final String[] exceptions) 

Source Link

Usage

From source file:com.github.jasmo.obfuscate.ScrambleStrings.java

License:Open Source License

private void createStaticConstructor(ClassNode owner) throws UnsupportedEncodingException {
    MethodNode original = BytecodeHelper.getMethod(owner, "<clinit>", "()V");
    MethodVisitor mv = owner.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    // generate instructions
    InstructionAdapter builder = new InstructionAdapter(mv);
    builder.iconst(stringList.size());/*  w  w w . ja  va  2 s.c om*/
    builder.newarray(Type.getType(String.class));
    for (int i = 0; i < stringList.size(); i++) {
        builder.dup();
        builder.iconst(i);
        builder.aconst(Base64.getEncoder().encodeToString(stringList.get(i).getBytes("UTF-8")));
        builder.astore(InstructionAdapter.OBJECT_TYPE);
    }
    builder.putstatic(unscrambleClass.name, FIELD_NAME, "[Ljava/lang/String;");
    // merge with original if it exists
    if (original != null) {
        // original should already end with RETURN
        owner.methods.remove(original);
        original.instructions.accept(builder);
    } else {
        builder.areturn(Type.VOID_TYPE);
    }
}

From source file:me.themallard.bitmmo.api.transformer.Transformer.java

License:Open Source License

protected final void createGetter(ClassNode cn, FieldNode fn, String name) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, name, "()" + fn.desc, null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from ww w  . j  a  v  a  2s.  c om*/
    mv.visitFieldInsn(Opcodes.GETFIELD, cn.name, fn.name, fn.desc);
    mv.visitInsn(Type.getType(fn.desc).getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.api.transformer.Transformer.java

License:Open Source License

protected final void createSetter(ClassNode cn, FieldNode fn, String name) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, name, "(" + fn.desc + ")V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from   w ww  .j av  a 2  s  .c  o m*/
    mv.visitVarInsn(Type.getType(fn.desc).getOpcode(Opcodes.ILOAD), 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, cn.name, fn.name, fn.desc);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.impl.plugin.chathook.ChatHook.java

License:Open Source License

private void createSendMessage(ClassNode cn) {
    Pattern p = new PatternBuilder().add(new InstructionElement(INVOKESTATIC), new AnyElement(),
            new LdcElement(new LdcInsnNode("Chat")), new InstructionElement(INVOKEVIRTUAL)).build();

    MethodInsnNode newBuilder = null;/* w  ww .j a v a  2s  .  c  om*/
    MethodInsnNode setChatText = null;
    MethodInsnNode ebola1 = null;
    MethodInsnNode ebola2 = null;
    MethodInsnNode ebola3 = null;
    MethodInsnNode ebola4 = null;
    MethodInsnNode ebola5 = null;
    MethodInsnNode ebola6 = null;
    MethodInsnNode ebola7 = null;

    for (MethodNode mn : cn.methods) {
        if (!p.contains(mn.instructions))
            continue;

        int offset = p.getOffset(mn.instructions);

        newBuilder = (MethodInsnNode) mn.instructions.get(offset - 9); // newbuilder
        // then var insn of parameter, ldc or w/e
        setChatText = (MethodInsnNode) mn.instructions.get(offset - 7); // setchattext
        ebola1 = (MethodInsnNode) mn.instructions.get(offset - 6); // U a
        ebola2 = (MethodInsnNode) mn.instructions.get(offset - 5); // U p
        ebola3 = (MethodInsnNode) mn.instructions.get(offset - 4); // Player
        // r
        ebola4 = (MethodInsnNode) mn.instructions.get(offset - 3); // setMapID
        ebola5 = (MethodInsnNode) mn.instructions.get(offset - 2); // build
        // store that crap
        ebola6 = (MethodInsnNode) mn.instructions.get(offset); // i e
        // load crap again
        // LDC "Chat"
        ebola7 = (MethodInsnNode) mn.instructions.get(offset + 3); // i a
    }

    {
        MethodVisitor mv = cn.visitMethod(ACC_PUBLIC, "sendChatMessage", "(Ljava/lang/String;)V", null, null);
        mv.visitMethodInsn(INVOKESTATIC, newBuilder.owner, newBuilder.name, newBuilder.desc, newBuilder.itf);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, setChatText.owner, setChatText.name, setChatText.desc,
                setChatText.itf);
        mv.visitMethodInsn(INVOKESTATIC, ebola1.owner, ebola1.name, ebola1.desc, ebola1.itf);
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola2.owner, ebola2.name, ebola2.desc, ebola2.itf);
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola3.owner, ebola3.name, ebola3.desc, ebola3.itf);
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola4.owner, ebola4.name, ebola4.desc, ebola4.itf);
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola5.owner, ebola5.name, ebola5.desc, ebola5.itf);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitMethodInsn(INVOKESTATIC, ebola6.owner, ebola6.name, ebola6.desc, ebola6.itf);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitLdcInsn("Chat");
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola7.owner, ebola7.name, ebola7.desc, ebola7.itf);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
}

From source file:me.themallard.bitmmo.impl.plugin.chathook.ChatHook.java

License:Open Source License

private void createAddMessage(ClassNode cn) {
    Pattern p = new PatternBuilder().add(new InstructionElement(ALOAD), new LdcElement(new LdcInsnNode("")),
            new InstructionElement(ALOAD), new InstructionElement(INVOKEVIRTUAL)).build();

    MethodInsnNode ebola1 = null;/*from   w ww. j a  v a2  s.c om*/

    for (MethodNode mn : cn.methods) {
        if (!p.contains(mn.instructions))
            continue;

        int offset = p.getOffset(mn.instructions);

        ebola1 = (MethodInsnNode) mn.instructions.get(offset + 3);
    }

    {
        MethodVisitor mv = cn.visitMethod(ACC_PUBLIC, "addChatMessage", "(Ljava/lang/String;)V", null, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn("");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, ebola1.owner, ebola1.name, ebola1.desc, ebola1.itf);
        mv.visitInsn(RETURN);
        mv.visitEnd();
    }
}

From source file:me.themallard.bitmmo.impl.plugin.chathook.ChatHook.java

License:Open Source License

public void createIsVisible(ClassNode cn) {
    // again too lazy to make actual work
    // steal it from original code and hope that it works ;)
    InsnList method = null;//w w w .  j  a  v a 2  s.  c  o m

    for (MethodNode mn : cn.methods) {
        if (mn.desc.equals("()Z")) {
            method = mn.instructions;
            break;
        }
    }

    MethodVisitor mv = cn.visitMethod(ACC_PUBLIC, "isVisible", "()Z", null, null);
    method.accept(mv);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.impl.plugin.inputactiontracker.InputActionTrackerPlugin.java

License:Open Source License

private void createIsKeyDown(ClassNode cn) {
    // again too lazy to make actual work
    // steal it from original code and hope that it works ;)

    for (MethodNode mn : cn.methods) {
        if (mn.desc.equals("(LHTMud/InputActionTracker$ActionType;)Z")) {
            InsnList method = mn.instructions;

            MethodVisitor mv = cn.visitMethod(ACC_PUBLIC, "isKeyDown", mn.desc, null, null);
            method.accept(mv);//from   ww  w. jav a2 s  .  c  om
            mv.visitEnd();
            break;
        }
    }
}

From source file:me.themallard.bitmmo.impl.plugin.playerhook.PlayerPlugin.java

License:Open Source License

@Override
public void run(ClassNode cn) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, "getPosition",
            "()" + "Lme/themallard/bitmmo/impl/plugin/position/IPosition;", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);//from w w w  .j a  va2  s  . c o m
    mv.visitFieldInsn(Opcodes.GETFIELD, "Entity", "position", "LPosition;");
    mv.visitInsn(
            Type.getType("Lme/themallard/bitmmo/impl/plugin/position/IPosition;").getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java

License:Open Source License

public static byte[] writeMethods(String name, byte[] bytes, Multimap<String, MethodWriter> writers) {
    if (writers.containsKey(name)) {
        ClassNode cnode = createClassNode(bytes);

        for (MethodWriter mw : writers.get(name)) {
            MethodNode mv = findMethod(mw.method, cnode);
            if (mv == null) {
                mv = (MethodNode) cnode.visitMethod(mw.access, mw.method.s_name, mw.method.s_desc, null,
                        mw.exceptions);/* ww  w. j  a v  a 2s  . c om*/
            }

            mv.access = mw.access;
            mv.instructions.clear();
            mw.write(mv);
        }

        bytes = createBytes(cnode, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    }
    return bytes;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ComponentInjector.java

License:Open Source License

private Class<? extends T> construct(List<Class<? extends Component>> components) {

    // Map components to specified wrapped interfaces
    Map<Class<?>, Class<? extends Component>> intfComponentMap = new HashMap<>();
    for (Class<? extends Component> component : components) {
        for (Passthrough pt : component.getAnnotationsByType(Passthrough.class)) {
            Class<?> intf;/*  w  w  w.ja v  a  2  s  .  c  om*/
            try {
                intf = Class.forName(pt.value());
            } catch (ClassNotFoundException exec) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Invalid passthrough \"%s\" on component %s, the specified interface doesn't exist.",
                        pt.value(), component);
            }
            if (!intf.isAssignableFrom(component)) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Invalid passthrough \"%s\" on component %s, the specified interface isn't implemented.",
                        pt.value(), component);
            }
            if (intfComponentMap.containsKey(intf)) {
                throw new ClassLoaderUtil.ClassLoaderException(
                        "Duplicate Passthrough interface found: %s (%s, %s)", pt.value(), component,
                        intfComponentMap.get(intf));
            }
            intfComponentMap.put(intf, component);
        }
    }

    // Create new ClassNode from cached bytes
    ClassNode clazzNode = new ClassNode();
    String name = Type.getInternalName(baseClazz);
    String classname = name + "_$$_NOVA_" + cache.size();

    // Inject block field
    clazzNode.visit(V1_8, ACC_PUBLIC | ACC_SUPER, classname, null, name,
            intfComponentMap.keySet().stream().map(Type::getInternalName).toArray(s -> new String[s]));
    clazzNode.visitField(ACC_PRIVATE | ACC_FINAL, "$$_provider", Type.getDescriptor(ComponentProvider.class),
            null, null).visitEnd();

    // Add constructors
    for (Constructor<?> constructor : baseClazz.getConstructors()) {
        int mod = constructor.getModifiers();
        String descr = Type.getConstructorDescriptor(constructor);

        if (Modifier.isFinal(mod) || Modifier.isPrivate(mod)) {
            continue;
        }
        MethodVisitor mv = clazzNode.visitMethod(mod, "<init>", descr, null,
                ASMHelper.getExceptionTypes(constructor));

        // Call super constructor
        mv.visitCode();
        // load this
        mv.visitVarInsn(ALOAD, 0);
        Class<?>[] parameters = constructor.getParameterTypes();
        for (int i = 0; i < constructor.getParameterCount(); i++) {
            // variables
            mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1);
        }
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(baseClazz), "<init>", descr, false);
        // return
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    // Add methods
    for (Class<?> intf : intfComponentMap.keySet()) {
        // Create class constant
        Type clazzConst = Type.getType(intf.getClass());

        for (Method m : intf.getMethods()) {
            boolean isVoid = m.getReturnType() == null;
            String descr = Type.getMethodDescriptor(m);

            MethodVisitor mv = clazzNode.visitMethod(ACC_PUBLIC, m.getName(), descr, null,
                    ASMHelper.getExceptionTypes(m));
            mv.visitCode();

            // load block instance
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, classname, "$$_provider", Type.getDescriptor(ComponentProvider.class));
            mv.visitLdcInsn(clazzConst);
            // load component instance
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(ComponentProvider.class), "get",
                    Type.getMethodDescriptor(Type.getType(Component.class), Type.getType(Class.class)), false);

            // add parameters
            Class<?>[] parameters = m.getParameterTypes();
            for (int i = 0; i < m.getParameterCount(); i++) {
                mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1);
            }

            // invoke
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(intf), m.getName(), descr, true);
            mv.visitInsn(isVoid ? RETURN : Type.getType(m.getReturnType()).getOpcode(IRETURN));
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }

    clazzNode.visitEnd();

    return ASMHelper.defineClass(clazzNode, ClassWriter.COMPUTE_MAXS, baseClazz.getProtectionDomain());
}