Search in sources :

Example 11 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class AsmMethod method visitVarInsn.

public void visitVarInsn(final int opcodeUsed, final int var) {
    final Instruction instruction = new VarInstruction(instructionList.size(), OPCODES_ARRAY[opcodeUsed], currentLineNumber, var);
    updateCurrentState(instruction);
}
Also used : VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) JumpInstruction(org.freud.analysed.classbytecode.method.instruction.JumpInstruction) ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) FieldInstruction(org.freud.analysed.classbytecode.method.instruction.FieldInstruction) IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)

Example 12 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class AsmMethod method visitLdcInsn.

public void visitLdcInsn(final Object constant) {
    final Instruction instruction;
    if (constant instanceof Type) {
        instruction = new ReferenceOperandInstruction(instructionList.size(), Opcode.LDC, currentLineNumber, constant.toString());
    } else {
        instruction = new ConstInstruction(instructionList.size(), Opcode.LDC, currentLineNumber, constant);
    }
    updateCurrentState(instruction);
}
Also used : ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) Type(org.objectweb.asm.Type) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) JumpInstruction(org.freud.analysed.classbytecode.method.instruction.JumpInstruction) ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) FieldInstruction(org.freud.analysed.classbytecode.method.instruction.FieldInstruction) IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)

Example 13 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class ClassByteCodeDsl method methodInvokedWithParams.

public static boolean methodInvokedWithParams(final ClassByteCodeMethod analysed, final Class expectedOwner, final String expectedMethodName, final Class... expectedParamTypes) {
    final String expectedOwnerName = typeEncoding(expectedOwner);
    final boolean[] found = new boolean[1];
    found[0] = false;
    analysed.findInstruction(new AbstractInstructionVisitor() {

        @Override
        public void methodInvocation(final Instruction instruction, final String owner, final String methodName, final String... args) {
            if (!found[0] && expectedOwnerName.equals(owner) && expectedMethodName.equals(methodName)) {
                Instruction prevInstruction = analysed.getInstruction(instruction.getInstructionIndex() - 1);
                OperandStack operandStack = prevInstruction.getOperandStack();
                found[0] = true;
                for (int i = expectedParamTypes.length - 1; i >= 0; i--) {
                    final String expectedType = typeEncoding(expectedParamTypes[i]);
                    if (!expectedType.equals(operandStack.getOperandType())) {
                        found[0] = false;
                        break;
                    }
                    operandStack = operandStack.next();
                }
            }
        }
    });
    return found[0];
}
Also used : OperandStack(org.freud.analysed.classbytecode.method.instruction.OperandStack) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 14 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class ClassByteCodeDsl method containsInstructions.

public static boolean containsInstructions(final ClassByteCodeMethod analysed, final Opcode... opcodes) {
    final Instruction[] found = new Instruction[1];
    analysed.findInstruction(new AbstractInstructionVisitor() {

        @Override
        public void noArgInstruction(final Instruction instruction) {
            for (int i = 0; i < opcodes.length; i++) {
                Opcode opcode = opcodes[i];
                if (instruction.getOpcode() == opcode) {
                    found[0] = instruction;
                    break;
                }
            }
        }
    });
    return found[0] != null;
}
Also used : Opcode(org.freud.analysed.classbytecode.method.instruction.Opcode) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 15 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class ClassByteCodeMethodMatchers method hasMethodInvocation.

public static FreudExtendedMatcher<ClassByteCodeMethod> hasMethodInvocation(final Class expectedOwner, final String expectedMethodName, final Class... expectedParamsDeclared) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private String expectedOwnerName;

        private String[] expectedParamNames;

        {
            expectedOwnerName = typeEncoding(expectedOwner);
            expectedParamNames = (expectedParamsDeclared.length == 0) ? EMPTY_ARGS : new String[expectedParamsDeclared.length];
            for (int i = 0, size = expectedParamsDeclared.length; i < size; i++) {
                expectedParamNames[i] = typeEncoding(expectedParamsDeclared[i]);
            }
        }

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            final boolean[] found = new boolean[] { false };
            found[0] = false;
            item.findInstruction(new AbstractInstructionVisitor() {

                @Override
                public void methodInvocation(final Instruction instruction, final String owner, final String methodName, final String... args) {
                    if (!found[0] && expectedOwnerName.equals(owner) && expectedMethodName.equals(methodName) && Arrays.equals(expectedParamNames, args)) {
                        found[0] = true;
                    }
                }
            });
            return found[0];
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("hasMethodInvocation(" + expectedOwner.getName() + ", " + expectedMethodName + ", " + Arrays.toString(expectedParamNames) + ")");
        }
    };
}
Also used : Description(org.hamcrest.Description) ClassByteCodeMethod(org.freud.analysed.classbytecode.method.ClassByteCodeMethod) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Aggregations

Instruction (org.freud.analysed.classbytecode.method.instruction.Instruction)16 ConstInstruction (org.freud.analysed.classbytecode.method.instruction.ConstInstruction)10 FieldInstruction (org.freud.analysed.classbytecode.method.instruction.FieldInstruction)10 IntOperandInstruction (org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction)10 JumpInstruction (org.freud.analysed.classbytecode.method.instruction.JumpInstruction)10 MethodInvocationInstruction (org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)10 ReferenceOperandInstruction (org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction)10 VarInstruction (org.freud.analysed.classbytecode.method.instruction.VarInstruction)10 AbstractInstructionVisitor (org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)6 Opcode (org.freud.analysed.classbytecode.method.instruction.Opcode)4 ClassByteCodeMethod (org.freud.analysed.classbytecode.method.ClassByteCodeMethod)3 FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)3 Description (org.hamcrest.Description)3 OperandStack (org.freud.analysed.classbytecode.method.instruction.OperandStack)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 CastOperandStack (org.freud.analysed.classbytecode.method.instruction.CastOperandStack)1 Label (org.freud.analysed.classbytecode.method.instruction.Label)1 Matcher (org.hamcrest.Matcher)1 Type (org.objectweb.asm.Type)1