Example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode.

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:com.github.fge.grappa.transform.process.VarFramingGenerator.java

License:Apache License

@Override
public void process(@Nonnull ParserClassNode classNode, @Nonnull RuleMethod method) throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN)
        ret = ret.getPrevious();//from   ww  w  . j a v a 2  s  .  co  m

    CodeBlock block = CodeBlock.newCodeBlock();

    block.newobj(CodegenUtils.p(VarFramingMatcher.class)).dup_x1().swap();

    createVarFieldArray(block, method);

    block.invokespecial(CodegenUtils.p(VarFramingMatcher.class), "<init>",
            CodegenUtils.sig(void.class, Rule.class, Var[].class));

    instructions.insertBefore(ret, block.getInstructionList());

    method.setBodyRewritten();
}

From source file:com.github.fge.grappa.transform.RuleMethodInterpreter.java

License:Open Source License

@Override
public void returnOperation(AbstractInsnNode insn, BasicValue value, BasicValue expected) {
    Preconditions.checkState(insn.getOpcode() == ARETURN);
    Preconditions.checkState(unwrap(value).getType().equals(Type.getType(Rule.class)));
    Preconditions.checkState(unwrap(expected).getType().equals(Type.getType(Rule.class)));
    Preconditions.checkState(method.getReturnInstructionNode() == null);
    method.setReturnInstructionNode(createNode(insn, null, value));
}

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

License:Open Source License

private List<AbstractInsnNode> getRealInstructions(MethodNode method) {
    List<AbstractInsnNode> instructions = new LinkedList<>();
    for (AbstractInsnNode node : method.instructions.toArray()) {
        if (node.getOpcode() != -1)
            instructions.add(node);//www.  j a v  a  2s. c o m
    }
    return instructions;
}

From source file:com.google.devtools.build.android.desugar.DefaultMethodClassFixerTest.java

License:Open Source License

private void checkClinitForDefaultInterfaceMethodWithStaticInitializerTestInterfaceSetOneC(
        byte[] classContent) {
    ClassReader reader = new ClassReader(classContent);
    reader.accept(new ClassVisitor(Opcodes.ASM5) {

        class ClinitMethod extends MethodNode {

            public ClinitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                super(Opcodes.ASM5, access, name, desc, signature, exceptions);
            }/* ww w  .ja  v a  2s.  c o  m*/
        }

        private ClinitMethod clinit;

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if ("<clinit>".equals(name)) {
                assertThat(clinit).isNull();
                clinit = new ClinitMethod(access, name, desc, signature, exceptions);
                return clinit;
            }
            return super.visitMethod(access, name, desc, signature, exceptions);
        }

        @Override
        public void visitEnd() {
            assertThat(clinit).isNotNull();
            assertThat(clinit.instructions.size()).isEqualTo(3);
            AbstractInsnNode instruction = clinit.instructions.getFirst();
            {
                assertThat(instruction).isInstanceOf(MethodInsnNode.class);
                MethodInsnNode field = (MethodInsnNode) instruction;
                assertThat(field.owner).isEqualTo("com/google/devtools/build/android/desugar/testdata/java8/"
                        + "DefaultInterfaceMethodWithStaticInitializer" + "$TestInterfaceSetOne$I1$$CC");
                assertThat(field.name)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_NAME);
                assertThat(field.desc)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC);
            }
            {
                instruction = instruction.getNext();
                assertThat(instruction).isInstanceOf(MethodInsnNode.class);
                MethodInsnNode field = (MethodInsnNode) instruction;
                assertThat(field.owner).isEqualTo("com/google/devtools/build/android/desugar/testdata/java8/"
                        + "DefaultInterfaceMethodWithStaticInitializer" + "$TestInterfaceSetOne$I2$$CC");
                assertThat(field.name)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_NAME);
                assertThat(field.desc)
                        .isEqualTo(InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC);
            }
            {
                instruction = instruction.getNext();
                assertThat(instruction).isInstanceOf(InsnNode.class);
                assertThat(instruction.getOpcode()).isEqualTo(Opcodes.RETURN);
            }
        }
    }, 0);
}

From source file:com.googlecode.dex2jar.tools.DecryptStringCmd.java

License:Apache License

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length != 1) {
        usage();/*  w w  w . j a v  a  2  s  . c o m*/
        return;
    }

    final Path jar = new File(remainingArgs[0]).toPath();
    if (!Files.exists(jar)) {
        System.err.println(jar + " is not exists");
        return;
    }
    if (methodName == null || methodOwner == null) {
        System.err.println("Please set --decrypt-method-owner and --decrypt-method-name");
        return;
    }

    if (output == null) {
        if (Files.isDirectory(jar)) {
            output = new File(jar.getFileName() + "-decrypted.jar").toPath();
        } else {
            output = new File(getBaseName(jar.getFileName().toString()) + "-decrypted.jar").toPath();
        }
    }

    if (Files.exists(output) && !forceOverwrite) {
        System.err.println(output + " exists, use --force to overwrite");
        return;
    }

    System.err.println(jar + " -> " + output);

    List<String> list = new ArrayList<String>();
    if (classpath != null) {
        list.addAll(Arrays.asList(classpath.split(";|:")));
    }
    list.add(jar.toAbsolutePath().toString());
    URL[] urls = new URL[list.size()];
    for (int i = 0; i < list.size(); i++) {
        urls[i] = new File(list.get(i)).toURI().toURL();
    }
    final Method jmethod;
    try {
        URLClassLoader cl = new URLClassLoader(urls);
        jmethod = cl.loadClass(methodOwner).getMethod(methodName, String.class);
        jmethod.setAccessible(true);
    } catch (Exception ex) {
        System.err.println("can't load method: String " + methodOwner + "." + methodName + "(String), message:"
                + ex.getMessage());
        return;
    }
    final String methodOwnerInternalType = this.methodOwner.replace('.', '/');
    try (FileSystem outputFileSystem = createZip(output)) {
        final Path outputBase = outputFileSystem.getPath("/");
        walkJarOrDir(jar, new FileVisitorX() {
            @Override
            public void visitFile(Path file, Path relative) throws IOException {
                if (file.getFileName().toString().endsWith(".class")) {

                    ClassReader cr = new ClassReader(Files.readAllBytes(file));
                    ClassNode cn = new ClassNode();
                    cr.accept(cn, 0);

                    for (Object m0 : cn.methods) {
                        MethodNode m = (MethodNode) m0;
                        if (m.instructions == null) {
                            continue;
                        }
                        AbstractInsnNode p = m.instructions.getFirst();
                        while (p != null) {
                            if (p.getOpcode() == Opcodes.LDC) {
                                LdcInsnNode ldc = (LdcInsnNode) p;
                                if (ldc.cst instanceof String) {
                                    String v = (String) ldc.cst;
                                    AbstractInsnNode q = p.getNext();
                                    if (q.getOpcode() == Opcodes.INVOKESTATIC) {
                                        MethodInsnNode mn = (MethodInsnNode) q;
                                        if (mn.name.equals(methodName)
                                                && mn.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")
                                                && mn.owner.equals(methodOwnerInternalType)) {
                                            try {
                                                Object newValue = jmethod.invoke(null, v);
                                                ldc.cst = newValue;
                                            } catch (Exception e) {
                                                // ignore
                                            }
                                            m.instructions.remove(q);
                                        }
                                    }
                                }
                            }
                            p = p.getNext();
                        }
                    }

                    ClassWriter cw = new ClassWriter(0);
                    cn.accept(cw);
                    Files.write(outputBase.resolve(relative), cw.toByteArray());
                } else {
                    Files.copy(file, outputBase.resolve(relative));
                }
            }
        });
    }
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private void patchWorldServerTick(MethodNode method, boolean obfuscated) {
    Iterator<AbstractInsnNode> iter = method.instructions.iterator();

    // Replace// w  w w.  java2 s .  com
    // > this.worldInfo.setWorldTime(this.worldInfo.getWorldTime() + 1L);
    // To
    // > TimeTweaksManager.addTick(this.worldInfo);
    int index = 0;
    while (iter.hasNext()) {
        AbstractInsnNode currentNode = iter.next();

        if (currentNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) currentNode;
            if (WORLD_INFO_GET_WORLD_TIME.matchesNode(methodInsnNode, "()J", obfuscated)
                    && currentNode.getNext().getOpcode() == Opcodes.LCONST_1) {
                // Found the call
                index = method.instructions.indexOf(currentNode) - 2;
            }
        }
    }
    if (index == 0)
        error("Could not find call in WorldServer.tick method");

    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.insertBefore(method.instructions.get(index),
            new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "addTick",
                    "(L" + WORLD_INFO.getPath(obfuscated) + ";)V"));
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private void patchWorldClientTick(MethodNode method, boolean obfuscated) {
    Iterator<AbstractInsnNode> iter = method.instructions.iterator();

    // Replace// ww w  .  j  av a  2s . c om
    // > this.setWorldTime(this.getWorldTime() + 1L);
    // To
    // > TimeTweaksManager.addTick(this.provider);
    int index = 0;
    while (iter.hasNext()) {
        AbstractInsnNode currentNode = iter.next();

        if (currentNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) currentNode;
            if (WORLD_CLIENT_GET_WORLD_TIME.matchesNode(methodInsnNode, "()J", obfuscated)
                    && currentNode.getNext().getOpcode() == Opcodes.LCONST_1) {
                index = method.instructions.indexOf(currentNode) - 1;
            }
        }
    }
    if (index == 0)
        error("Could not find call in WorldServer.tick method");

    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.remove(method.instructions.get(index));
    method.instructions.insertBefore(method.instructions.get(index),
            new FieldInsnNode(Opcodes.GETFIELD, WORLD.getPath(obfuscated), WORLD_PROVIDER.get(obfuscated),
                    "L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";"));
    method.instructions.insert(method.instructions.get(index),
            new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "addTick",
                    "(L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";)V"));
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

@Test
public void testSerializerWrite() {
    MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, "name", "()V", null, null);

    MethodNodeGenerator methodNodeGenerator = new MethodNodeGenerator(methodNode);

    InsnList insnList = methodNode.instructions;

    for (Type type : _types) {
        IntrabandProxyUtil.serializerWrite(methodNodeGenerator, type);

        AbstractInsnNode abstractInsnNode = insnList.getLast();

        Assert.assertTrue(abstractInsnNode instanceof MethodInsnNode);

        MethodInsnNode methodInsnNode = (MethodInsnNode) abstractInsnNode;

        Assert.assertEquals(Opcodes.INVOKEVIRTUAL, abstractInsnNode.getOpcode());
        Assert.assertEquals(Type.getInternalName(Serializer.class), methodInsnNode.owner);

        if (type.getSort() <= Type.DOUBLE) {
            String name = TextFormatter.format(type.getClassName(), TextFormatter.G);

            Assert.assertEquals("write".concat(name), methodInsnNode.name);
            Assert.assertEquals(Type.getMethodDescriptor(Type.VOID_TYPE, type), methodInsnNode.desc);
        } else if (type.equals(Type.getType(String.class))) {
            Assert.assertEquals("writeString", methodInsnNode.name);
            Assert.assertEquals(Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)),
                    methodInsnNode.desc);
        } else {/*from w w w . ja  v  a2  s  . c o m*/
            Assert.assertEquals("writeObject", methodInsnNode.name);
            Assert.assertEquals(Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Serializable.class)),
                    methodInsnNode.desc);
        }
    }
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

private void _assertFieldInsnNode(AbstractInsnNode abstractInsnNode, int opcode, String owner, String name,
        Class<?> clazz) {/* w ww.  ja  va2 s  .com*/

    Assert.assertEquals(opcode, abstractInsnNode.getOpcode());

    FieldInsnNode fieldInsnNode = (FieldInsnNode) abstractInsnNode;

    Assert.assertEquals(owner, fieldInsnNode.owner);
    Assert.assertEquals(name, fieldInsnNode.name);
    Assert.assertEquals(Type.getDescriptor(clazz), fieldInsnNode.desc);
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

private void _assertInsnNode(AbstractInsnNode abstractInsnNode, int opcode) {

    Assert.assertEquals(opcode, abstractInsnNode.getOpcode());
}