Example usage for org.objectweb.asm Opcodes ISTORE

List of usage examples for org.objectweb.asm Opcodes ISTORE

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ISTORE.

Prototype

int ISTORE

To view the source code for org.objectweb.asm Opcodes ISTORE.

Click Source Link

Usage

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.DefUseChains.java

License:Open Source License

private static void removeUnboxedValueInsns(AbstractInterpretationValue val, ByteCodeMethodVisitor bcmv) {
    for (Insn i : val.getDefs())
        removeInsn(bcmv, i, val, "RemovingBoxedValueDefinition");

    for (Insn i : val.getUses()) {
        if (i.isBoxingMethod()) {
            removeInsn(bcmv, i, val, "RemoveBoxingMethod");
        } else if (i.isUnBoxingMethod()) {
            removeInsn(bcmv, i, val, "UnboxingMethod");
        } else if (i.isCheckCast()) {
            removeInsn(bcmv, i, val, "CheckCast"); // FIXME CHF
        } else if (i instanceof VarInsn) {
            VarInsn vi = (VarInsn) i;//from   w  w w  . j  a v  a  2s  .  c o m
            if (vi.opcode == Opcodes.ASTORE) {
                int j = bcmv.insns.indexOf(i);
                removeInsn(bcmv, i, val, "astoreconversion" + val.getType());
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new VarInsn("ISTORE", Opcodes.ISTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new VarInsn("LSTORE", Opcodes.LSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new VarInsn("FSTORE", Opcodes.FSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new VarInsn("DSTORE", Opcodes.DSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new VarInsn("ISTORE", Opcodes.ISTORE, val.getValueNumber(), vi.index));
                else
                    bcmv.insns.add(j, new VarInsn("ASTORE", Opcodes.ASTORE, val.getValueNumber(), vi.index));
            } else if (vi.opcode == Opcodes.ALOAD) {
                int j = bcmv.insns.indexOf(i);
                removeInsn(bcmv, i, val, "Aloadconversion" + val.getType());
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new VarInsn("ILOAD", Opcodes.ILOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new VarInsn("LLOAD", Opcodes.LLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new VarInsn("FLOAD", Opcodes.FLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new VarInsn("DLOAD", Opcodes.DLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new VarInsn("ILOAD", Opcodes.ILOAD, val.getValueNumber(), vi.index));
                else
                    bcmv.insns.add(j, new VarInsn("ALOAD", Opcodes.ALOAD, val.getValueNumber(), vi.index));
            }
        } else if (i instanceof SingleInsn) {
            SingleInsn si = (SingleInsn) i;
            if (si.opcode == Opcodes.ARETURN) {
                int j = bcmv.insns.indexOf(i);
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FZZ32", "make",
                            "(I)Lcom/sun/fortress/compiler/runtimeValues/FZZ32;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FZZ64", "make",
                            "(J)Lcom/sun/fortress/compiler/runtimeValues/FZZ64;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FRR32", "make",
                            "(F)Lcom/sun/fortress/compiler/runtimeValues/FRR32;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FRR64", "make",
                            "(D)Lcom/sun/fortress/compiler/runtimeValues/FRR64;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FVoid;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FVoid", "make",
                            "()Lcom/sun/fortress/compiler/runtimeValues/FVoid;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FBoolean", "make",
                            "(Z)Lcom/sun/fortress/compiler/runtimeValues/FBoolean;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FJavaString;"))
                    bcmv.insns.add(j,
                            new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                                    "com/sun/fortress/compiler/runtimeValues/FJavaString", "make",
                                    "(java.lang.String)Lcom/sun/fortress/compiler/runtimeValues/FJavaString;",
                                    "ReboxingReturnValue"));
                else
                    throw new RuntimeException("Don't recognize var type " + val.getType());
            }
        }
    }
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.Inlining.java

License:Open Source License

public static List<Insn> convertInsns(MethodInsn mi, List<Insn> insns, int[] args, int _index, Label end) {
    List<Insn> result = new ArrayList<Insn>();
    HashMap labels = new HashMap();
    int index = _index;
    for (Insn i : insns) {
        if (i.isExpanded()) {
            MethodInsn expanded = (MethodInsn) i;
            // This use of end should be OK because all returns should have been removed when inlined before.
            // What could go wrong?
            result.addAll(convertInsns(expanded, expanded.inlineExpansionInsns, args, _index, end));
        } else if (i instanceof SingleInsn) {
            SingleInsn si = (SingleInsn) i;
            switch (si.opcode) {
            case Opcodes.IRETURN:
            case Opcodes.LRETURN:
            case Opcodes.FRETURN:
            case Opcodes.DRETURN:
            case Opcodes.ARETURN:
            case Opcodes.RETURN:
                result.add(new JumpInsn("RETURN->GOTO", Opcodes.GOTO, end, newIndex(mi, index++)));
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }/*from  w ww  .  ja v a2 s.c  om*/
        } else if (i instanceof VarInsn) {
            VarInsn vi = (VarInsn) i;
            switch (vi.opcode) {
            case Opcodes.ILOAD:
            case Opcodes.LLOAD:
            case Opcodes.FLOAD:
            case Opcodes.DLOAD:
            case Opcodes.ALOAD:
            case Opcodes.ISTORE:
            case Opcodes.LSTORE:
            case Opcodes.FSTORE:
            case Opcodes.DSTORE:
            case Opcodes.ASTORE:
                VarInsn newVarInsn = new VarInsn(vi.name, vi.opcode, args[vi.var], newIndex(mi, index++));
                result.add(newVarInsn);
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }
        } else if (i instanceof VisitMaxs) {
        } else if (i instanceof VisitEnd) {
        } else if (i instanceof VisitCode) {
        } else if (i instanceof VisitFrame) {
        } else if (i instanceof LabelInsn) {
            LabelInsn li = (LabelInsn) i;
            if (labels.containsKey(li.label))
                result.add(new LabelInsn(li.name, (Label) labels.get(li.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(li.label, l);
                result.add(new LabelInsn(li.name, l, newIndex(mi, index++)));
            }
        } else if (i instanceof JumpInsn) {
            JumpInsn ji = (JumpInsn) i;
            if (labels.containsKey(ji.label))
                result.add(
                        new JumpInsn(ji.name, ji.opcode, (Label) labels.get(ji.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(ji.label, l);
                result.add(new JumpInsn(ji.name, ji.opcode, l, newIndex(mi, index++)));
            }
        } else if (i instanceof VisitLineNumberInsn) {
            VisitLineNumberInsn vlni = (VisitLineNumberInsn) i;
            if (labels.containsKey(vlni.start))
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, (Label) labels.get(vlni.start),
                        newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(vlni.start, l);
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, l, newIndex(mi, index++)));
            }
        } else if (i instanceof LocalVariableInsn) {
            LocalVariableInsn lvi = (LocalVariableInsn) i;
            if (labels.containsKey(lvi.start) && labels.containsKey(lvi.end)) {
                result.add(new LocalVariableInsn(lvi.name, lvi._name, lvi.desc, lvi.sig,
                        (Label) labels.get(lvi.start), (Label) labels.get(lvi.end), args[lvi._index],
                        newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
        } else if (i instanceof TryCatchBlock) {
            TryCatchBlock tcb = (TryCatchBlock) i;
            if (labels.containsKey(tcb.start) && labels.containsKey(tcb.end)
                    && labels.containsKey(tcb.handler)) {
                result.add(
                        new TryCatchBlock(tcb.name, (Label) labels.get(tcb.start), (Label) labels.get(tcb.end),
                                (Label) labels.get(tcb.handler), tcb.type, newIndex(mi, index++)));
            } else if (!labels.containsKey(tcb.start) && !labels.containsKey(tcb.end)
                    && !labels.containsKey(tcb.handler)) {
                Label s = new Label();
                Label e = new Label();
                Label h = new Label();
                labels.put(tcb.start, s);
                labels.put(tcb.end, e);
                labels.put(tcb.handler, h);
                result.add(new TryCatchBlock(tcb.name, s, e, h, tcb.type, newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
            // Need to add TableSwitch, LookupSwitch
        } else {
            result.add(i.copy(newIndex(mi, index++)));
        }
    }
    return result;
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method getFooRaw so the
 * interpreter uses a switch instruction for ***GetRaw
 * based on the hash values of String names in this namespace.
 *///from  w w w  .  j  a v  a  2s.  c om
private static void writeMethodGetRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(Ljava/lang/String;)" + environmentClass.descriptor(), null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);

    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label returnNull = new Label();

    getRawHelper(mv, className, hashCodeRelation, environmentClass, sortedCodes, returnNull);

    mv.visitLabel(returnNull);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);

    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", "Ljava/lang/String;", null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 2);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method putRaw so the
 * interpreter uses a switch instruction for ***PutRaw
 * based on the hash values of String names in this namespace.
 *//*from   w  w w.j a v a  2  s . com*/
private static void writeMethodPutRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(" + STRING_DESCRIPTOR + environmentClass.descriptor() + ")V", null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_INTERNALNAME, "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 3);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label notFound = new Label();
    putRawHelper(mv, className, environmentClass, hashCodeRelation, sortedCodes, notFound);
    mv.visitLabel(notFound);
    mv.visitInsn(Opcodes.RETURN);
    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", STRING_DESCRIPTOR, null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("value", environmentClass.descriptor(), null, beginFunction, endFunction, 2);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 3);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 4);
    mv.visitEnd();
}

From source file:com.trigersoft.jaque.expression.ExpressionMethodVisitor.java

License:Apache License

@Override
public void visitVarInsn(int opcode, int var) {
    if (_me != null) {
        if (var == 0) {
            _exprStack.push(_me);/*from  w  w  w  .  j  a v  a2s. c o  m*/
            return;
        }
        var--;
    }
    Class<?> type;
    switch (opcode) {
    case Opcodes.ISTORE:
    case Opcodes.LSTORE:
    case Opcodes.FSTORE:
    case Opcodes.DSTORE:
    case Opcodes.ASTORE:
    case Opcodes.RET:
    default:
        throw notLambda(opcode);
    case Opcodes.ILOAD:
        type = Integer.TYPE;
        break;
    case Opcodes.LLOAD:
        type = Long.TYPE;
        break;
    case Opcodes.FLOAD:
        type = Float.TYPE;
        break;
    case Opcodes.DLOAD:
        type = Double.TYPE;
        break;
    case Opcodes.ALOAD:
        type = _argTypes[var];
        break;
    }

    _exprStack.push(Expression.parameter(type, var));
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.LocalValue.java

@Override
public BytecodeSequence write(final BytecodeExpression value) {
    return new BytecodeSequence() {
        @Override/*from   www . j a v  a  2s  .  c  o m*/
        public void generate(CodeEmitter code) {
            code.checkFrame(LocalValue.this);
            value.generate(code);
            code.getMethodVisitor().visitVarInsn(type.getJVMType().getOpcode(Opcodes.ISTORE), start);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.LocalValue.java

@Override
public BytecodeSequence write(final TypeWidget top) {
    return new BytecodeSequence() {
        @Override/*from w ww  . j  a v  a  2s.  c  om*/
        public void generate(CodeEmitter code) {
            code.checkFrame(LocalValue.this);
            code.cast(type, top);
            code.getMethodVisitor().visitVarInsn(type.getJVMType().getOpcode(Opcodes.ISTORE), start);
        }
    };
}

From source file:de.dynamicfiles.projects.gradle.plugins.javafx.tasks.internal.MonkeyPatcher.java

License:Apache License

private static void doMonkeyPatchFileHandleLeak(ClassReader classReader, ClassWriter classWriter) {
    classReader.accept(new ClassVisitor(Opcodes.ASM5, classWriter) {
        @Override//from  w  w  w .  ja  v a 2 s .  c  o m
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if (!(name.equals(METHOD_TO_MONKEY_PATCH) && desc.equals(METHOD_SIGNATURE_TO_MONKEY_PATCH))) {
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
            // helpful source: http://web.cs.ucla.edu/~msb/cs239-tutorial/
            // "We will do this using the Adapter Pattern. Adapters wrap an object, overriding some of its methods, and delegating to the others."
            // ugly adapter-pattern ... took me more time than I really can tell here <.<
            return getMonkeyPatchedFileHandleLeakMethodVisitor(access, name, desc, signature, exceptions);
        }

        private MethodVisitor getMonkeyPatchedFileHandleLeakMethodVisitor(int access, String name, String desc,
                String signature, String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM5,
                    super.visitMethod(access, name, desc, signature, exceptions)) {

                /*
                 TODO improve detection of lambda-positions, numbers might vary on different compile-versions
                 */
                @Override
                public void visitCode() {
                    // This mostly got generated from ASM itself, except some adjustments for lambda-IDs and removed "visitMaxs()"-call
                    String javalangThrowable = "java/lang/Throwable";
                    String javaioFile = "java/io/File";
                    String javalangString = "java/lang/String";

                    Label l0 = new Label();
                    Label l1 = new Label();
                    Label l2 = new Label();
                    mv.visitTryCatchBlock(l0, l1, l2, javalangThrowable);
                    Label l3 = new Label();
                    Label l4 = new Label();
                    Label l5 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l5, javalangThrowable);
                    Label l6 = new Label();
                    mv.visitTryCatchBlock(l3, l4, l6, null);
                    Label l7 = new Label();
                    Label l8 = new Label();
                    Label l9 = new Label();
                    mv.visitTryCatchBlock(l7, l8, l9, javalangThrowable);
                    Label l10 = new Label();
                    mv.visitTryCatchBlock(l5, l10, l6, null);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    Label l11 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l11);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "isDirectory", "()Z", false);
                    Label l12 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNE, l12);
                    mv.visitLabel(l11);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { javalangString }, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, javaioFile);
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("java.home");
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty",
                            "(Ljava/lang/String;)Ljava/lang/String;", false);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, javaioFile, "<init>", "(Ljava/lang/String;)V",
                            false);
                    mv.visitVarInsn(Opcodes.ASTORE, 2);
                    mv.visitLabel(l12);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitFieldInsn(Opcodes.GETSTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                            "VS_VERS", "[Ljava/lang/String;");
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInsn(Opcodes.ARRAYLENGTH);
                    mv.visitVarInsn(Opcodes.ISTORE, 5);
                    mv.visitInsn(Opcodes.ICONST_0);
                    mv.visitVarInsn(Opcodes.ISTORE, 6);
                    Label l13 = new Label();
                    mv.visitLabel(l13);
                    mv.visitFrame(Opcodes.F_APPEND, 3,
                            new Object[] { "[Ljava/lang/String;", Opcodes.INTEGER, Opcodes.INTEGER }, 0, null);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitVarInsn(Opcodes.ILOAD, 5);
                    Label l14 = new Label();
                    mv.visitJumpInsn(Opcodes.IF_ICMPGE, l14);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitVarInsn(Opcodes.ILOAD, 6);
                    mv.visitInsn(Opcodes.AALOAD);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/oracle/tools/packager/windows/WinAppBundler",
                            "copyMSVCDLLs", "(Ljava/io/File;Ljava/lang/String;)Z", false);
                    Label l15 = new Label();
                    mv.visitJumpInsn(Opcodes.IFEQ, l15);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ASTORE, 3);
                    mv.visitJumpInsn(Opcodes.GOTO, l14);
                    mv.visitLabel(l15);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitIincInsn(6, 1);
                    mv.visitJumpInsn(Opcodes.GOTO, l13);
                    mv.visitLabel(l14);
                    mv.visitFrame(Opcodes.F_CHOP, 3, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    Label l16 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, l16);
                    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("Not found MSVC dlls");
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                            "(Ljava/lang/String;)V", false);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l16);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference",
                            "<init>", "()V", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 4);
                    mv.visitVarInsn(Opcodes.ALOAD, 3);
                    mv.visitVarInsn(Opcodes.ASTORE, 5);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "toPath", "()Ljava/nio/file/Path;",
                            false);
                    mv.visitLdcInsn("bin");
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/nio/file/Path", "resolve",
                            "(Ljava/lang/String;)Ljava/nio/file/Path;", true);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/nio/file/Files", "list",
                            "(Ljava/nio/file/Path;)Ljava/util/stream/Stream;", false);
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitLabel(l3);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInvokeDynamicInsn("test", "()Ljava/util/function/Predicate;", new Handle(
                            Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"),
                                    new Handle(Opcodes.H_INVOKESTATIC,
                                            "com/oracle/tools/packager/windows/WinAppBundler",
                                            "lambda$copyMSVCDLLs$261", "(Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 5);
                    mv.visitInvokeDynamicInsn("test", "(Ljava/lang/String;)Ljava/util/function/Predicate;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)Z"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$262", "(Ljava/lang/String;Ljava/nio/file/Path;)Z"),
                                    Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter",
                            "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true);
                    mv.visitVarInsn(Opcodes.ALOAD, 1);
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitInvokeDynamicInsn("accept",
                            "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/function/Consumer;",
                            new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
                                    "metafactory",
                                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                            new Object[] { Type.getType("(Ljava/lang/Object;)V"), new Handle(
                                    Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler",
                                    "lambda$copyMSVCDLLs$263",
                                    "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;Ljava/nio/file/Path;)V"),
                                    Type.getType("(Ljava/nio/file/Path;)V") }); // modified lambda-name
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "forEach",
                            "(Ljava/util/function/Consumer;)V", true);
                    mv.visitLabel(l4);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l17 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l17);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l18 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l18);
                    mv.visitLabel(l0);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l1);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l2);
                    mv.visitFrame(Opcodes.F_FULL, 8,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l18);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitJumpInsn(Opcodes.GOTO, l17);
                    mv.visitLabel(l5);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 8);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitVarInsn(Opcodes.ASTORE, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 8);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l6);
                    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 9);
                    mv.visitLabel(l10);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l19 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l19);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    Label l20 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l20);
                    mv.visitLabel(l7);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l8);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l9);
                    mv.visitFrame(Opcodes.F_FULL, 10,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString, "java/util/stream/Stream", javalangThrowable, Opcodes.TOP,
                                    javalangThrowable },
                            1, new Object[] { javalangThrowable });
                    mv.visitVarInsn(Opcodes.ASTORE, 10);
                    mv.visitVarInsn(Opcodes.ALOAD, 7);
                    mv.visitVarInsn(Opcodes.ALOAD, 10);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed",
                            "(Ljava/lang/Throwable;)V", false);
                    mv.visitJumpInsn(Opcodes.GOTO, l19);
                    mv.visitLabel(l20);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V",
                            true);
                    mv.visitLabel(l19);
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    mv.visitVarInsn(Opcodes.ALOAD, 9);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l17);
                    mv.visitFrame(Opcodes.F_FULL, 6,
                            new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile,
                                    javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference",
                                    javalangString },
                            0, new Object[] {});
                    mv.visitVarInsn(Opcodes.ALOAD, 4);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicReference",
                            "get", "()Ljava/lang/Object;", false);
                    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/io/IOException");
                    mv.visitVarInsn(Opcodes.ASTORE, 6);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    Label l21 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNULL, l21);
                    mv.visitVarInsn(Opcodes.ALOAD, 6);
                    mv.visitInsn(Opcodes.ATHROW);
                    mv.visitLabel(l21);
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/io/IOException" }, 0, null);
                    mv.visitInsn(Opcodes.RETURN);
                }

            };
        }

    }, ClassReader.EXPAND_FRAMES); // ClassReader.EXPAND_FRAMES required for Java 8
}

From source file:de.kandid.model.Emitter.java

License:Apache License

/**
 * Assembles a class that implements the given interface by generating the byte code.
 * @param interfaze the interface to implement
 * @return the class/* www . j av  a  2 s.  co m*/
 */
private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) {
    String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$');
    String nameInterface = Type.getInternalName(interfaze);
    // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) });

    // Generate default construcotor
    MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V");
    cv.visitInsn(RETURN);
    cv.visitMaxs(1, 1);

    // Generate methods
    Method[] methods = interfaze.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method m = methods[i];
        if (m.getReturnType() != void.class)
            throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value");
        final String descMethod = Type.getMethodDescriptor(m);
        final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null,
                null);
        final Type[] argTypes = Type.getArgumentTypes(m);

        // for (int i = 0; i < _end; i += 2)
        //    ((Listener) _listeners[i]).send(...);

        int localStart = 1; // give one for "this"
        for (Type at : argTypes)
            localStart += at.getSize();
        Label entry = new Label();
        Label exit = new Label();
        mw.visitLabel(entry);

        // _isFiring = true;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_1);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        // setup local variables: i, _listeners, _end
        mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitIntInsn(Opcodes.ISTORE, localStart);

        mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray);
        mw.visitIntInsn(Opcodes.ASTORE, localStart + 1);

        mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor());
        mw.visitIntInsn(Opcodes.ISTORE, localStart + 2);

        final Label condition = new Label();
        mw.visitJumpInsn(GOTO, condition);

        final Label loop = new Label();
        mw.visitLabel(loop);

        //((Listener) _listeners[i]).doSomething()
        mw.visitIntInsn(Opcodes.ALOAD, localStart + 1);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitInsn(Opcodes.AALOAD);
        mw.visitTypeInsn(CHECKCAST, nameInterface);
        int offs = 1; // give one for "this"
        for (Type at : argTypes) {
            mw.visitVarInsn(at.getOpcode(ILOAD), offs);
            offs += at.getSize();
        }
        mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod);

        // i += 2
        mw.visitIincInsn(localStart, 2);

        // if (i < end) goto loop
        mw.visitLabel(condition);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitIntInsn(Opcodes.ILOAD, localStart + 2);
        mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop);

        // _isFiring = false;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        mw.visitLabel(exit);
        mw.visitInsn(RETURN);
        mw.visitMaxs(localStart + 2, localStart + 3);
        mw.visitEnd();
    }
    cw.visitEnd();
    return _loader.loadClass(cw, nameClass.replace('/', '.'));
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static void transformOnInvChanged(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_IS_SADDLED, false));
    needle.add(new VarInsnNode(Opcodes.ISTORE, 3));

    AbstractInsnNode pointer = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(//from w ww  .j  ava2 s .  c  o  m
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(new VarInsnNode(Opcodes.ASTORE, 4));

    method.instructions.insert(pointer, newInstr);

    needle = new InsnList();
    needle.add(new LdcInsnNode("mob.horse.armor"));
    needle.add(new LdcInsnNode(0.5F));
    needle.add(new InsnNode(Opcodes.FCONST_1));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_PLAY_SOUND, false));
    needle.add(new LabelNode());

    pointer = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    newInstr = new InsnList();
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_SHOVEL));
    LabelNode l9 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IF_ACMPNE, l9));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_IS_ITEM_EQUAL, false));
    newInstr.add(new JumpInsnNode(Opcodes.IFNE, l9));
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new LdcInsnNode("mob.horse.armor"));
    newInstr.add(new LdcInsnNode(0.5F));
    newInstr.add(new InsnNode(Opcodes.FCONST_1));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_PLAY_SOUND, false));
    newInstr.add(l9);

    method.instructions.insert(pointer, newInstr);
}