Example usage for org.objectweb.asm Opcodes ASTORE

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

Introduction

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

Prototype

int ASTORE

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

Click Source Link

Usage

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.VarInstruction.java

License:Open Source License

public VarInstruction(final ReadMethod readMethod, final int opcode, final int lineNumber,
        final int localVarIndex) {
    super(readMethod, opcode, lineNumber);
    assert opcode == Opcodes.ILOAD || opcode == Opcodes.LLOAD || opcode == Opcodes.FLOAD
            || opcode == Opcodes.DLOAD || opcode == Opcodes.ALOAD || opcode == Opcodes.ISTORE
            || opcode == Opcodes.LSTORE || opcode == Opcodes.FSTORE || opcode == Opcodes.DSTORE
            || opcode == Opcodes.ASTORE || opcode == Opcodes.RET;
    this.localVarIndex = localVarIndex;
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.VarInstruction.java

License:Open Source License

private VarInstruction(final ReadMethod readMethod, final int lineNumber, final int opcode,
        final int localVarIndex, final int index) {
    super(readMethod, opcode, lineNumber, index);
    assert opcode == Opcodes.ILOAD || opcode == Opcodes.LLOAD || opcode == Opcodes.FLOAD
            || opcode == Opcodes.DLOAD || opcode == Opcodes.ALOAD || opcode == Opcodes.ISTORE
            || opcode == Opcodes.LSTORE || opcode == Opcodes.FSTORE || opcode == Opcodes.DSTORE
            || opcode == Opcodes.ASTORE || opcode == Opcodes.RET;
    this.localVarIndex = localVarIndex;
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.VarInstruction.java

License:Open Source License

@Override
public String toString() {
    String instruction;/*  w  w w .ja  va2 s.  com*/
    switch (getOpcode()) {
    case Opcodes.ILOAD:
        instruction = "ILOAD";
        break;
    case Opcodes.LLOAD:
        instruction = "LLOAD";
        break;
    case Opcodes.FLOAD:
        instruction = "FLOAD";
        break;
    case Opcodes.DLOAD:
        instruction = "DLOAD";
        break;
    case Opcodes.ALOAD:
        instruction = "ALOAD";
        break;
    case Opcodes.ISTORE:
        instruction = "ISTORE";
        break;
    case Opcodes.LSTORE:
        instruction = "LSTORE";
        break;
    case Opcodes.FSTORE:
        instruction = "FSTORE";
        break;
    case Opcodes.DSTORE:
        instruction = "DSTORE";
        break;
    case Opcodes.ASTORE:
        instruction = "ASTORE";
        break;
    case Opcodes.RET:
        instruction = "RET";
        break;
    default:
        instruction = "-ERROR-";
    }
    return new StringBuilder(instruction.length() + 11).append(instruction).append(' ')
            .append(this.localVarIndex).toString();
}

From source file:de.unisb.cs.st.javaslicer.slicing.Slicer.java

License:Open Source License

public void process(ThreadId threadId, final List<SlicingCriterion> sc, boolean multithreaded)
        throws InterruptedException {
    DependencesExtractor<SlicerInstance> depExtractor = DependencesExtractor.forTrace(this.trace,
            SlicerInstanceFactory.instance);
    for (ProgressMonitor mon : this.progressMonitors)
        depExtractor.addProgressMonitor(mon);

    VisitorCapability[] capabilities = { VisitorCapability.CONTROL_DEPENDENCES,
            VisitorCapability.DATA_DEPENDENCES_READ_AFTER_WRITE, VisitorCapability.INSTRUCTION_EXECUTIONS,
            VisitorCapability.METHOD_ENTRY_LEAVE, VisitorCapability.CONTROL_DEPENDENCES };
    if (this.untracedCallVisitors.size() > 0)
        capabilities[capabilities.length - 1] = VisitorCapability.UNTRACED_METHOD_CALLS;

    final List<SliceVisitor> sliceVisitors0 = Slicer.this.sliceVisitors;
    final List<UntracedCallVisitor> untracedCallVisitors0 = Slicer.this.untracedCallVisitors;
    depExtractor.registerVisitor(new DependencesVisitorAdapter<SlicerInstance>() {
        private final List<SlicingCriterionInstance> slicingCritInst = instantiateSlicingCriteria(sc);
        @SuppressWarnings("unchecked")
        private IntegerMap<Object>[] interestingLocalVariables = (IntegerMap<Object>[]) new IntegerMap<?>[0];
        private long[] critOccurenceNumbers = new long[2]; // 0 if not in a criterion
        private final SliceVisitor[] sliceVisitorsArray = sliceVisitors0
                .toArray(new SliceVisitor[sliceVisitors0.size()]);
        private final UntracedCallVisitor[] untracedCallsVisitorsArray = untracedCallVisitors0
                .toArray(new UntracedCallVisitor[untracedCallVisitors0.size()]);

        private ReadMethod enteredMethod;

        private List<SlicingCriterionInstance> instantiateSlicingCriteria(List<SlicingCriterion> criteria) {
            if (criteria.isEmpty())
                return Collections.emptyList();
            else if (criteria.size() == 1)
                return Collections.singletonList(criteria.get(0).getInstance());
            else {
                List<SlicingCriterionInstance> instances = new ArrayList<SlicingCriterionInstance>(
                        criteria.size());
                for (SlicingCriterion crit : criteria)
                    instances.add(crit.getInstance());
                return instances;
            }/*from www  .  j  a v  a 2  s.c  om*/
        }

        @Override
        public void visitInstructionExecution(SlicerInstance instance) {
            int stackDepth = instance.getStackDepth();
            if (this.critOccurenceNumbers.length <= stackDepth) {
                long[] newCritOccurenceNumbers = new long[2
                        * Math.max(this.critOccurenceNumbers.length, stackDepth)];
                System.arraycopy(this.critOccurenceNumbers, 0, newCritOccurenceNumbers, 0,
                        this.critOccurenceNumbers.length);
                this.critOccurenceNumbers = newCritOccurenceNumbers;
            }
            Instruction instruction = instance.getInstruction();
            for (SlicingCriterionInstance crit : this.slicingCritInst) {
                if (crit.matches(instance)) {
                    this.critOccurenceNumbers[stackDepth] = crit.getOccurenceNumber();
                    assert this.critOccurenceNumbers[stackDepth] > 0;
                    // for each criterion, there are three cases:
                    //  - track all data and control dependences of the instruction
                    //  - track a given set of local variables
                    //  - track the control dependences of this instruction
                    // only in the first case, the instruction itself is added to the dynamic slice
                    instance.fullTransitiveClosure = crit.computeTransitiveClosure();
                    if (instance.fullTransitiveClosure) {
                        // first case
                        if (instruction.getType() != InstructionType.LABEL
                                && instruction.getOpcode() != Opcodes.GOTO)
                            for (SliceVisitor vis : this.sliceVisitorsArray)
                                vis.visitMatchedInstance(instance);
                        instance.onDynamicSlice = true;
                        instance.criterionDistance = 0;
                    } else if (crit.hasLocalVariables()) {
                        // second case
                        if (this.interestingLocalVariables.length <= stackDepth) {
                            @SuppressWarnings("unchecked")
                            IntegerMap<Object>[] newInterestingLocalVariables = (IntegerMap<Object>[]) new IntegerMap<?>[Math
                                    .max(stackDepth + 1, this.interestingLocalVariables.length * 3 / 2)];
                            System.arraycopy(this.interestingLocalVariables, 0, newInterestingLocalVariables, 0,
                                    this.interestingLocalVariables.length);
                            this.interestingLocalVariables = newInterestingLocalVariables;
                        }
                        List<LocalVariable> localVariables = crit.getLocalVariables();
                        if (this.interestingLocalVariables[stackDepth] == null)
                            this.interestingLocalVariables[stackDepth] = new IntegerMap<Object>(
                                    localVariables.size() * 4 / 3 + 1);
                        for (LocalVariable i : localVariables)
                            this.interestingLocalVariables[stackDepth].put(i.getIndex(), null);
                    } else {
                        // third case
                        instance.onDynamicSlice = true;
                        instance.criterionDistance = 0;
                    }
                } else if (this.critOccurenceNumbers[stackDepth] != 0) {
                    this.critOccurenceNumbers[stackDepth] = 0;
                }
            }
            if (this.interestingLocalVariables.length > stackDepth
                    && this.interestingLocalVariables[stackDepth] != null) {
                switch (instruction.getOpcode()) {
                case Opcodes.ISTORE:
                case Opcodes.ASTORE:
                case Opcodes.LSTORE:
                case Opcodes.FSTORE:
                case Opcodes.DSTORE:
                    VarInstruction varInsn = (VarInstruction) instruction;
                    if (this.interestingLocalVariables[stackDepth].containsKey(varInsn.getLocalVarIndex())) {
                        this.interestingLocalVariables[stackDepth].remove(varInsn.getLocalVarIndex());
                        if (this.interestingLocalVariables[stackDepth].isEmpty())
                            this.interestingLocalVariables[stackDepth] = null;
                        for (SliceVisitor vis : this.sliceVisitorsArray)
                            vis.visitMatchedInstance(instance);
                        instance.onDynamicSlice = true;
                        // and we want to know where the data comes from...
                        instance.fullTransitiveClosure = true;
                        instance.criterionDistance = 0;
                    }
                    break;
                case Opcodes.INVOKEINTERFACE:
                case Opcodes.INVOKESPECIAL:
                case Opcodes.INVOKESTATIC:
                case Opcodes.INVOKEVIRTUAL:
                    if (this.enteredMethod != null) {
                        MethodInvocationInstruction mtdInvInsn = (MethodInvocationInstruction) instruction;
                        int paramCount = instruction.getOpcode() == INVOKESTATIC ? 0 : 1;
                        for (int param = mtdInvInsn.getParameterCount() - 1; param >= 0; --param)
                            paramCount += mtdInvInsn.parameterIsLong(param) ? 2 : 1;
                        boolean enteredMethodMatches = this.enteredMethod.getName()
                                .equals(mtdInvInsn.getInvokedMethodName())
                                && this.enteredMethod.getDesc().equals(mtdInvInsn.getInvokedMethodDesc());
                        if (enteredMethodMatches) {
                            boolean localVarsMatched = false;
                            for (int varNr = 0; varNr < paramCount; ++varNr) {
                                if (this.interestingLocalVariables[stackDepth].containsKey(varNr)) {
                                    this.interestingLocalVariables[stackDepth].remove(varNr);
                                    if (this.interestingLocalVariables[stackDepth].isEmpty())
                                        this.interestingLocalVariables[stackDepth] = null;
                                    localVarsMatched = true;
                                    instance.onDynamicSlice = true;
                                    // and we want to know where the data comes from...
                                    // TODO
                                    instance.fullTransitiveClosure = true;
                                    instance.criterionDistance = 0;
                                }
                            }
                            if (localVarsMatched)
                                for (SliceVisitor vis : this.sliceVisitorsArray)
                                    vis.visitMatchedInstance(instance);
                        }
                    }
                }
            }
            this.enteredMethod = null;
        }

        @Override
        public void visitControlDependence(SlicerInstance from, SlicerInstance to) {
            if (from.onDynamicSlice) {
                Instruction insn = to.getInstruction();
                if (insn.getType() == InstructionType.LABEL || insn.getOpcode() == Opcodes.GOTO) {
                    if (to.predecessors == null)
                        to.predecessors = Collections.singleton(from);
                    else {
                        if (to.predecessors.size() == 1)
                            to.predecessors = new HashSet<SlicerInstance>(to.predecessors);
                        to.predecessors.add(from);
                    }
                    if (from.criterionDistance < to.criterionDistance)
                        to.criterionDistance = from.criterionDistance;
                } else if (from.predecessors != null) {
                    assert (!from.predecessors.isEmpty());
                    for (SlicerInstance pred : from.predecessors) {
                        int distance = pred.criterionDistance + 1;
                        delegateControlSliceDependence(pred, to, distance);
                        if (distance < to.criterionDistance)
                            to.criterionDistance = distance;
                    }
                } else {
                    int distance = from.criterionDistance + 1;
                    delegateControlSliceDependence(from, to, distance);
                    if (distance < to.criterionDistance)
                        to.criterionDistance = distance;
                }
                to.onDynamicSlice = true;
            }
        }

        private void delegateControlSliceDependence(SlicerInstance from, SlicerInstance to, int distance) {

            for (SliceVisitor vis : this.sliceVisitorsArray)
                vis.visitSliceDependence(from, to, null, distance);

            // since "to" controls the execution of "from", we want to track all data dependences of "to"
            // to find out why it took this decision
            // exception: method invocations; here we only want to track why the method was executed,
            // but not the data that it consumed
            // important to check that "from" comes from inside the method which is called by "from"
            boolean calledMethodDependence = false;
            if (to.getInstruction().getType() == InstructionType.METHODINVOCATION) {
                MethodInvocationInstruction mtdInv = (MethodInvocationInstruction) to.getInstruction();
                ReadMethod calledMethod = from.getInstruction().getMethod();
                if (mtdInv.getInvokedMethodName().equals(calledMethod.getName())
                        && mtdInv.getInvokedMethodDesc().equals(calledMethod.getDesc())) {
                    calledMethodDependence = true;
                }
            }
            if (!calledMethodDependence) {
                to.fullTransitiveClosure = true;
            }
        }

        @Override
        public void visitDataDependence(SlicerInstance from, SlicerInstance to,
                Collection<? extends Variable> fromVars, Variable toVar, DataDependenceType type)
                throws InterruptedException {
            assert type == DataDependenceType.READ_AFTER_WRITE;

            if (from.onDynamicSlice && // from must definitively be on the dynamic slice
            (from.fullTransitiveClosure || // and either we want to track all data dependencies
            (from.interestingVariable != null && ( // or (if the interestingVariable is set) ...
            from.interestingVariable.equals(toVar) || // the interestingVariable must be the one we are just visiting
            (from.moreInterestingVariables != null && from.moreInterestingVariables.contains(toVar)))))) { // or it must be in the set of more variables
                Instruction insn = to.getInstruction();
                assert insn.getType() != InstructionType.LABEL;
                int distance = from.criterionDistance + 1;
                if (distance < to.criterionDistance)
                    to.criterionDistance = distance;
                for (SliceVisitor vis : this.sliceVisitorsArray)
                    vis.visitSliceDependence(from, to, toVar, distance);

                if (!fromVars.isEmpty()) {
                    Iterator<? extends Variable> varIt = fromVars.iterator();
                    assert varIt
                            .hasNext() : "Iterator of a non-empty collection should have at least one element";
                    Variable first = varIt.next();
                    if (to.interestingVariable == null || to.interestingVariable.equals(first)) {
                        to.interestingVariable = first;
                        first = varIt.hasNext() ? varIt.next() : null;
                    }
                    if (first != null) {
                        if (to.moreInterestingVariables == null)
                            to.moreInterestingVariables = new HashSet<Variable>(8);
                        to.moreInterestingVariables.add(first);
                        while (varIt.hasNext())
                            to.moreInterestingVariables.add(varIt.next());
                    }
                }
                to.onDynamicSlice = true;
            }
        }

        @Override
        public void visitMethodLeave(ReadMethod method, int stackDepth) throws InterruptedException {
            if (this.interestingLocalVariables.length > stackDepth
                    && this.interestingLocalVariables[stackDepth] != null) {
                this.interestingLocalVariables[stackDepth] = null;
            }
        }

        @Override
        public void visitMethodEntry(ReadMethod method, int stackDepth) throws InterruptedException {
            if (this.interestingLocalVariables.length > stackDepth
                    && this.interestingLocalVariables[stackDepth] != null) {
                this.enteredMethod = method;
                this.interestingLocalVariables[stackDepth] = null;
            }
        }

        @Override
        public void visitUntracedMethodCall(SlicerInstance instrInstance) throws InterruptedException {
            for (UntracedCallVisitor vis : this.untracedCallsVisitorsArray)
                vis.visitUntracedMethodCall(instrInstance);
        }

    }, capabilities);

    depExtractor.processBackwardTrace(threadId, multithreaded);
}

From source file:de.zib.sfs.instrument.DirectByteBufferAdapter.java

License:BSD License

@Override
protected void appendWrappedMethods(ClassVisitor visitor) {
    // override from MappedByteBuffer so we can re-init the callback
    // properly/*  w  ww  .j  ava  2  s  .c  om*/

    // public void setFileDescriptor(FileDescriptor fileDescriptor) {
    MethodVisitor settFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "setFileDescriptor",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), null, null);
    settFileDescriptorMV.visitCode();

    // this.fileDescriptor = fileDescriptor;
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 1);
    settFileDescriptorMV.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));

    // callback.openCallback(this.fileDescriptor);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
            this.callbackTypeDescriptor);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));
    settFileDescriptorMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, "openCallback",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), false);

    // }
    settFileDescriptorMV.visitInsn(Opcodes.RETURN);
    settFileDescriptorMV.visitMaxs(0, 0);
    settFileDescriptorMV.visitEnd();

    // also override from MappedByteBuffer

    // protected FileDescriptor getFileDescriptorImpl() {
    MethodVisitor getFileDescriptorImplMV = visitor.visitMethod(Opcodes.ACC_PROTECTED, "getFileDescriptorImpl",
            Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null);
    getFileDescriptorImplMV.visitCode();

    // return fileDescriptor;
    // }
    getFileDescriptorImplMV.visitVarInsn(Opcodes.ALOAD, 0);
    getFileDescriptorImplMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));
    getFileDescriptorImplMV.visitInsn(Opcodes.ARETURN);
    getFileDescriptorImplMV.visitMaxs(0, 0);
    getFileDescriptorImplMV.visitEnd();

    if (!skipReads()) {
        wrapMethod(Opcodes.ACC_PUBLIC, "get", Type.getType(ByteBuffer.class),
                new Type[] { Type.getType(byte[].class), Type.INT_TYPE, Type.INT_TYPE }, null, null,
                "getCallback", Type.INT_TYPE, new ParameterResultPasser(3));
    }

    if (!skipWrites()) {
        wrapMethod(Opcodes.ACC_PUBLIC, "put", Type.getType(ByteBuffer.class),
                new Type[] { Type.getType(byte[].class), Type.INT_TYPE, Type.INT_TYPE }, null, null,
                "putCallback", Type.INT_TYPE, new ParameterResultPasser(3));
    }

    if (!skipWrites()) {
        // public ByteBuffer put(ByteBuffer src) {
        MethodVisitor bulkPutMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)), null,
                null);
        bulkPutMV.visitCode();

        // if (isInstrumentationActive()) {
        isInstrumentationActive(bulkPutMV);
        Label instrumentationActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, instrumentationActiveLabel);

        // return nativeMethodPrefixput(src);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName,
                this.methodPrefix + "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)),
                false);
        bulkPutMV.visitInsn(Opcodes.ARETURN);

        // }
        bulkPutMV.visitLabel(instrumentationActiveLabel);

        // setInstrumentationActive(fromFileChannel);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "fromFileChannel",
                Type.getDescriptor(Boolean.TYPE));
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instrumentedTypeInternalName,
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // boolean srcInstrumentationActive = false;
        bulkPutMV.visitInsn(Opcodes.ICONST_0);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 2);

        // if (src instanceof MappedByteBuffer) {
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitTypeInsn(Opcodes.INSTANCEOF, Type.getInternalName(MappedByteBuffer.class));
        Label srcInstanceofMappedByteBufferLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, srcInstanceofMappedByteBufferLabel);

        // srcInstrumentationActive = src.isFromFileChannel();
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "isFromFileChannel", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 2);

        // src.setInstrumentationActive(true);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitInsn(Opcodes.ICONST_1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // }
        bulkPutMV.visitLabel(srcInstanceofMappedByteBufferLabel);

        // int length = src.remaining();
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Buffer.class), "remaining",
                Type.getMethodDescriptor(Type.INT_TYPE), false);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 4);

        // long startTime = System.nanoTime();
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.systemInternalName, "nanoTime",
                this.nanoTimeDescriptor, false);
        bulkPutMV.visitVarInsn(Opcodes.LSTORE, 5);

        // ByteBuffer result = nativeMethodPrefixput(src);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName,
                this.methodPrefix + "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)),
                false);
        bulkPutMV.visitVarInsn(Opcodes.ASTORE, 7);

        // long endTime = System.nanoTime();
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.systemInternalName, "nanoTime",
                this.nanoTimeDescriptor, false);
        bulkPutMV.visitVarInsn(Opcodes.LSTORE, 8);

        // if (isInstrumentationActive()) {
        isInstrumentationActive(bulkPutMV);
        Label instrumentationStillActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, instrumentationStillActiveLabel);

        // callback.putCallback(startTime, endTime, length);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
                this.callbackTypeDescriptor);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 5);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 8);
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 4);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, "putCallback",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.LONG_TYPE, Type.LONG_TYPE, Type.INT_TYPE), false);

        // setInstrumentationActive(false);
        setInstrumentationActive(bulkPutMV, false);

        // }
        bulkPutMV.visitLabel(instrumentationStillActiveLabel);

        // if (srcInstrumentationActive) {
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 2);
        Label srcInstrumentationActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, srcInstrumentationActiveLabel);

        // callback.onGetEnd(src.getFileDescriptor(), startTime, endTime,
        // length);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "getFileDescriptor", Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), false);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 5);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 8);
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 4);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.callbackTypeInternalName, "getCallback",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class), Type.LONG_TYPE,
                        Type.LONG_TYPE, Type.INT_TYPE),
                false);

        // src.setInstrumentationActive(false);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitInsn(Opcodes.ICONST_0);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // }
        bulkPutMV.visitLabel(srcInstrumentationActiveLabel);

        // return result;
        // }
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 7);
        bulkPutMV.visitInsn(Opcodes.ARETURN);
        bulkPutMV.visitMaxs(0, 0);
        bulkPutMV.visitEnd();
    }

    ResultPasser resultDiscarder = new DiscardResultPasser();

    // regular gets and puts
    for (Map.Entry<String, Type> type : TYPES.entrySet()) {
        if (!skipReads()) {
            // public TYPE getTYPE() { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "get" + type.getKey(), type.getValue(), null, null, null,
                    "get" + type.getKey() + "Callback", null, resultDiscarder);

            // public TYPE getTYPE(int index) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "get" + type.getKey(), type.getValue(), new Type[] { Type.INT_TYPE },
                    null, null, "get" + type.getKey() + "Callback", null, resultDiscarder);
        }

        if (!skipWrites()) {
            // public ByteBuffer putTYPE(TYPE value) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "put" + type.getKey(), Type.getType(ByteBuffer.class),
                    new Type[] { type.getValue() }, null, null, "put" + type.getKey() + "Callback", null,
                    resultDiscarder);

            // public ByteBuffer putTYPE(int index, TYPE value) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "put" + type.getKey(), Type.getType(ByteBuffer.class),
                    new Type[] { Type.INT_TYPE, type.getValue() }, null, null,
                    "put" + type.getKey() + "Callback", null, resultDiscarder);
        }
    }

    visitor.visitEnd();
}

From source file:de.zib.sfs.instrument.FileChannelImplAdapter.java

License:BSD License

@Override
protected void appendWrappedMethods(ClassVisitor visitor) {
    // public FileDescriptor getFileDescriptor() {
    MethodVisitor getFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "getFileDescriptor",
            Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null);
    getFileDescriptorMV.visitCode();/* ww  w  .  j a  v a  2  s  .c o  m*/

    // return fileDescriptor;
    // }
    getFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    getFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(FileChannelImpl.class), "fd",
            Type.getDescriptor(FileDescriptor.class));
    getFileDescriptorMV.visitInsn(Opcodes.ARETURN);
    getFileDescriptorMV.visitMaxs(0, 0);
    getFileDescriptorMV.visitEnd();

    if (!skipReads()) {
        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class), Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.LONG_TYPE,
                new Type[] { Type.getType(ByteBuffer[].class), Type.INT_TYPE, Type.INT_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.LONG_TYPE, 0, false);

        // transferTo is basically a read

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "transferTo", Type.LONG_TYPE,
                new Type[] { Type.LONG_TYPE, Type.LONG_TYPE, Type.getType(WritableByteChannel.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.LONG_TYPE, 2, true);
    }

    // repeat for write methods

    if (!skipWrites()) {
        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class), Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.LONG_TYPE,
                new Type[] { Type.getType(ByteBuffer[].class), Type.INT_TYPE, Type.INT_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.LONG_TYPE, 0, false);

        // transferFrom is basically a write

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "transferFrom", Type.LONG_TYPE,
                new Type[] { Type.getType(ReadableByteChannel.class), Type.LONG_TYPE, Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.LONG_TYPE, 0, true);
    }

    String mapMethodDescriptor = Type.getMethodDescriptor(Type.getType(MappedByteBuffer.class),
            Type.getType(MapMode.class), Type.LONG_TYPE, Type.LONG_TYPE);

    // public MappedByteBuffer map(MapMode mode, long position, long size)
    // throws IOException {
    MethodVisitor mapMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "map", mapMethodDescriptor, null,
            new String[] { Type.getInternalName(IOException.class) });
    mapMV.visitCode();

    // MappedByteBuffer mbb = nativeMethodPrefixmap(mode, position, size);
    mapMV.visitVarInsn(Opcodes.ALOAD, 0);
    mapMV.visitVarInsn(Opcodes.ALOAD, 1);
    mapMV.visitVarInsn(Opcodes.LLOAD, 2);
    mapMV.visitVarInsn(Opcodes.LLOAD, 4);
    mapMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName, this.methodPrefix + "map",
            mapMethodDescriptor, false);
    mapMV.visitVarInsn(Opcodes.ASTORE, 6);

    // mbb.setFromFileChannel(true);
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitInsn(Opcodes.ICONST_1);
    mapMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
            "setFromFileChannel", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

    // mbb.setFileDescriptor(fd);
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitVarInsn(Opcodes.ALOAD, 0);
    mapMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "fd",
            Type.getDescriptor(FileDescriptor.class));
    mapMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
            "setFileDescriptor", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)),
            false);

    // if we don't want to trace mmap calls, then map needs to reset the
    // instrumentationActive flag
    if (!this.traceMmap) {
        // setInstrumentationActive(false);
        setInstrumentationActive(mapMV, false);
    }

    // return mbb;
    // }
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitInsn(Opcodes.ARETURN);
    mapMV.visitMaxs(0, 0);
    mapMV.visitEnd();
}

From source file:dyco4j.instrumentation.internals.InitTracingMethodVisitor.java

License:BSD License

@Override
public void visitVarInsn(final int opcode, final int var) {
    super.visitVarInsn(opcode, var);
    switch (opcode) {
    case Opcodes.ILOAD:
    case Opcodes.FLOAD:
        this.stackFrame.push(OTHER);
        break;/*  w  w w .j av  a 2 s  . co m*/
    case Opcodes.LLOAD:
    case Opcodes.DLOAD:
        this.stackFrame.push(OTHER);
        this.stackFrame.push(OTHER);
        break;
    case Opcodes.ALOAD:
        this.stackFrame.push(var == 0 ? THIS : OTHER);
        break;
    case Opcodes.ASTORE:
    case Opcodes.ISTORE:
    case Opcodes.FSTORE:
        this.stackFrame.pop();
        break;
    case Opcodes.LSTORE:
    case Opcodes.DSTORE:
        this.stackFrame.pop();
        this.stackFrame.pop();
        break;
    }
}

From source file:edu.illinois.nondex.instr.IdentityHashMapShufflingAdder.java

License:Open Source License

public void addNextIndex() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PROTECTED, "nextIndex", "()I", null, null);
    mv.visitCode();//from ww  w .jav a  2s  .c  om
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0",
            "Ljava/util/IdentityHashMap;");
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "modCount", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "expectedModCount",
            "I");
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l0);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ConcurrentModificationException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ConcurrentModificationException", "<init>", "()V",
            false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/IdentityHashMap$IdentityHashMapIterator", "hasNext",
            "()Z", false);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.IFNE, l1);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/NoSuchElementException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/NoSuchElementException", "<init>", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "keys",
            "Ljava/util/List;");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.DUP);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "idx", "I");
    mv.visitInsn(Opcodes.DUP_X1);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IADD);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "idx", "I");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true);
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/lang/Object", Opcodes.INTEGER }, 0, null);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0",
            "Ljava/util/IdentityHashMap;");
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table", "[Ljava/lang/Object;");
    mv.visitInsn(Opcodes.ARRAYLENGTH);
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPGE, l3);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0",
            "Ljava/util/IdentityHashMap;");
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table", "[Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitInsn(Opcodes.AALOAD);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    Label l4 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ACMPNE, l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
            "lastReturnedIndex", "I");
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitIincInsn(2, 2);
    mv.visitJumpInsn(Opcodes.GOTO, l2);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
            "lastReturnedIndex", "I");
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(5, 3);
    mv.visitEnd();
}

From source file:edu.illinois.nondex.instr.IdentityHashMapShufflingAdder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("hasNext".equals(name)) {
        return super.visitMethod(access, "originalHasNext", desc, signature, exceptions);
    }/* w ww .  ja v  a2 s  . c  o  m*/
    if ("nextIndex".equals(name)) {
        return super.visitMethod(access, "originalNextIndex", desc, signature, exceptions);
    }
    if ("<init>".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.RETURN) {
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "size", "I");
                    Label l0 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l0);
                    super.visitInsn(Opcodes.ICONST_0);
                    Label l1 = new Label();
                    super.visitJumpInsn(Opcodes.GOTO, l1);
                    super.visitLabel(l0);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/IdentityHashMap$IdentityHashMapIterator",
                                    "java/util/IdentityHashMap" },
                            1, new Object[] { "java/util/IdentityHashMap$IdentityHashMapIterator" });
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table",
                            "[Ljava/lang/Object;");
                    super.visitInsn(Opcodes.ARRAYLENGTH);
                    super.visitLabel(l1);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/IdentityHashMap$IdentityHashMapIterator",
                                    "java/util/IdentityHashMap" },
                            2, new Object[] { "java/util/IdentityHashMap$IdentityHashMapIterator",
                                    Opcodes.INTEGER });
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "index", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "modCount", "I");
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "expectedModCount", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ICONST_M1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "lastReturnedIndex", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table",
                            "[Ljava/lang/Object;");
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "traversalTable", "[Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
                    super.visitInsn(Opcodes.DUP);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "order", "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
                    super.visitInsn(Opcodes.DUP);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "keys", "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ICONST_0);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "idx", "I");
                    Label l2 = new Label();
                    super.visitLabel(l2);
                    super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                            "java/util/IdentityHashMap$IdentityHashMapIterator", "originalHasNext", "()Z",
                            false);
                    Label l3 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l3);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "order", "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                            "java/util/IdentityHashMap$IdentityHashMapIterator", "originalNextIndex", "()I",
                            false);
                    super.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
                            "(I)Ljava/lang/Integer;", false);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add",
                            "(Ljava/lang/Object;)Z", true);
                    super.visitInsn(Opcodes.POP);
                    super.visitJumpInsn(Opcodes.GOTO, l2);
                    super.visitLabel(l3);
                    super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "order", "Ljava/util/List;");
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "(Ljava/util/List;)Ljava/util/List;", false);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "order", "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "order", "Ljava/util/List;");
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "iterator",
                            "()Ljava/util/Iterator;", true);
                    super.visitVarInsn(Opcodes.ASTORE, 2);
                    Label l4 = new Label();
                    super.visitLabel(l4);
                    super.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/util/Iterator" }, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z",
                            true);
                    Label l5 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l5);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next",
                            "()Ljava/lang/Object;", true);
                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
                    super.visitVarInsn(Opcodes.ASTORE, 3);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "keys", "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator",
                            "this$0", "Ljava/util/IdentityHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table",
                            "[Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 3);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
                    super.visitInsn(Opcodes.AALOAD);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add",
                            "(Ljava/lang/Object;)Z", true);
                    super.visitInsn(Opcodes.POP);
                    super.visitJumpInsn(Opcodes.GOTO, l4);
                    super.visitLabel(l5);
                    super.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
                }
                super.visitInsn(opcode);
            }
        };
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:edu.illinois.nondex.instr.MethodShufflingAdder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("getExceptionTypes".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override/*ww  w  .  j a va2  s .c o m*/
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.ARETURN) {
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
                    super.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    if ("getGenericExceptionTypes".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.ARETURN) {
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
                    super.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/reflect/Type;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    if ("getDeclaredAnnotations".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.ARETURN) {
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
                    super.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/annotation/Annotation;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    if ("getParameterAnnotations".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.ARETURN) {
                    super.visitVarInsn(Opcodes.ASTORE, 1);
                    super.visitInsn(Opcodes.ICONST_0);
                    super.visitVarInsn(Opcodes.ISTORE, 2);
                    Label l0 = new Label();
                    super.visitLabel(l0);
                    super.visitFrame(Opcodes.F_APPEND, 2,
                            new Object[] { "[[Ljava/lang/annotation/Annotation;", Opcodes.INTEGER }, 0, null);
                    super.visitVarInsn(Opcodes.ILOAD, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitInsn(Opcodes.ARRAYLENGTH);
                    Label l1 = new Label();
                    super.visitJumpInsn(Opcodes.IF_ICMPGE, l1);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitVarInsn(Opcodes.ILOAD, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitVarInsn(Opcodes.ILOAD, 2);
                    super.visitInsn(Opcodes.AALOAD);
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
                    super.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/annotation/Annotation;");
                    super.visitInsn(Opcodes.AASTORE);
                    super.visitIincInsn(2, 1);
                    super.visitJumpInsn(Opcodes.GOTO, l0);
                    super.visitLabel(l1);
                    super.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                }
                super.visitInsn(opcode);
            }
        };
    }

    return super.visitMethod(access, name, desc, signature, exceptions);
}