Package alt.jiapi.reflect

Examples of alt.jiapi.reflect.InstructionFactory


        // No API to set SourceFile!

        JiapiMethod method = c.addMethod(Modifier.PUBLIC, "<init>", emptySig);
        InstructionList il = method.getInstructionList();
        InstructionFactory iFactory = il.getInstructionFactory();
        il.add(iFactory.aload(0));
        il.add(iFactory.invoke(0, "java/lang/Object", "<init>", emptySig));
        il.add(iFactory.returnMethod(method));

        method = c.addMethod(Modifier.PUBLIC | Modifier.STATIC, "main", mainSig);
        il = method.getInstructionList();
        iFactory = il.getInstructionFactory();
        il.add(iFactory.getField(Modifier.STATIC,
                "java/lang/System",
                "out",
                "Ljava/io/PrintStream;"));
        il.add(iFactory.pushConstant("Hello world!"));
        il.add(iFactory.invoke(0, "java/io/PrintStream", "println", printlnSig));
        il.add(iFactory.returnMethod(method));

        return c.getByteCode();
    }
View Full Code Here


            forward(il);
            forward(method.getInstructionList());
        }

        if (createReturn) {
            InstructionFactory factory = method.getInstructionFactory();
            method.getInstructionList().add(factory.returnMethod(method));
        }
    }
View Full Code Here

                     il.getDeclaringMethod().getName());
            forward(il);
            return;
        }

        InstructionFactory factory =
            il.getDeclaringMethod().getInstructionFactory();

        JiapiClass jc = getCurrentClass();

        JiapiField field = null;
        try {
            field = jc.getDeclaredField(fieldName);
        }
        catch (NoSuchFieldException e) {
            throw new JiapiRuntimeException("No such field: " + e.getMessage());
        }

        // Get the initialized value from Runtime.
        il.add(factory.pushConstant(fieldName));
        il.add(factory.invoke(getFieldValue));
       
        // Cast the field.
        il.add(factory.cast(field.getTypeName()));

        // Set the field.
        il.add(factory.setField(field));

        forward(il);
    }
View Full Code Here

                                             signature);
       
        // Then create the method body. The body will make a call to
        // System.out.println and then just return.
        InstructionList il = method.getInstructionList();
        InstructionFactory iFactory = method.getInstructionFactory();

        JiapiMethod println = clazz.getDeclaredMethod("println", new String[]
                                                      {"java.lang.Object"});

//         il.add(iFactory.getField(loader.loadClass("java.lang.System").getField("out")));
//         JiapiMethod println = loader.loadClass(System.out.getClass().getName()).getMethod("println", new String[] { "java.lang.String" } );

        il.add(iFactory.pushConstant("hello world!"));

        il.add(iFactory.invoke(println));
        il.add(iFactory.returnMethod(method));

        // Finalize the modified class and dump it to the file:

        clazz.dump(new FileOutputStream("Test.class"));
    }
View Full Code Here

    public CopyInstrumentor(InstructionList source) {
        this.source = source;
    }

    public void instrument(InstructionList il) {
        InstructionFactory factory =
            il.getDeclaringMethod().getInstructionFactory();

        if (true)
            throw new RuntimeException("NOT IMPLEMENTED");
//         il.add(factory.copy(source));
View Full Code Here

        super(fep);
    }

    public void instrument(JiapiMethod jm) {
        InstructionList il = jm.getInstructionList();
        InstructionFactory factory = il.getInstructionFactory();
        JiapiClass fep = getEventProducer();
        JiapiMethod fieldGet = null;
        JiapiMethod fieldSet = null;
        JiapiField jiapiField = getEventProducerField();

        try {
            fieldGet =
                fep.getDeclaredMethod("fieldGet",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
            fieldSet =
                fep.getDeclaredMethod("fieldSet",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
        }
        catch(Exception e) {
            System.out.println("ERROR: " + e);
        }

        //System.out.println("Instrumenting " + il.getDeclaringMethod());

        int idx = -1;
        while ((idx = il.indexOf(OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS, idx+1)) != -1) {
            FieldAccess ins = (FieldAccess)il.get(idx);
            // Do not instrument on __jiapi_fields
            if (ins.getName().startsWith("__jiapi_field")) {
                continue;
            }

            //System.out.println("  found " + ins);
            InstructionList invokeList = il.createEmptyList();

            switch(ins.getOpcode()) {
            case Opcodes.PUTFIELD:
            case Opcodes.GETFIELD:
                invokeList.add(factory.getField(jiapiField));
                invokeList.add(factory.pushThis());
                invokeList.add(factory.pushConstant(ins.getFieldName()));
                if (ins.getOpcode() == Opcodes.GETFIELD) {
                    invokeList.add(factory.invoke(fieldGet));
                }
                else {
                    invokeList.add(factory.invoke(fieldSet));
                }
                break;
            case Opcodes.PUTSTATIC:
            case Opcodes.GETSTATIC:
                invokeList.add(factory.getField(jiapiField));
                invokeList.add(factory.getField(jiapiField)); // BUG: Class
                invokeList.add(factory.pushConstant(ins.getFieldName()));
                if (ins.getOpcode() == Opcodes.GETSTATIC) {
                    invokeList.add(factory.invoke(fieldGet));
                }
                else {
                    invokeList.add(factory.invoke(fieldSet));
                }
                break;
            }

            // Insert list after field access operation
View Full Code Here

        super(mep);
    }

    public void instrument(JiapiMethod jm) {
        InstructionList il = jm.getInstructionList();
        InstructionFactory factory = il.getInstructionFactory();
        JiapiClass mep = getEventProducer();
        JiapiMethod methodEntered = null;
        JiapiMethod methodExited = null;
        JiapiField jiapiField = getEventProducerField();

        // a flag, that tells whether or not InstructionList being
        // processed is in a static method.
        boolean isStatic = Modifier.isStatic(il.getDeclaringMethod().getModifiers());

        try {
            methodEntered =
                mep.getDeclaredMethod("methodEntered",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
            methodExited =
                mep.getDeclaredMethod("methodExited",
                                      new String[] { "java.lang.Object",
                                                     "java.lang.String" });
        }
        catch(Exception e) {
            System.out.println("ERROR: " + e);
        }

        // Create instructions for method entrance
        InstructionList entryList = il.createEmptyList();
        entryList.add(factory.getField(jiapiField));
        if (isStatic) {
            entryList.add(factory.pushConstant(il.getDeclaringMethod().getDeclaringClass().getName())); // BUG: we should pass a Class
        }
        else {
            entryList.add(factory.pushThis());
        }
        entryList.add(factory.pushConstant(il.getDeclaringMethod().getName()));
        entryList.add(factory.invoke(methodEntered));

        // Skip super(....) call, if in <init> method
        int superIdx = il.indexOf(Opcodes.INVOKESPECIAL);

        // BUG: we should insert entrylist on <clinit> pass the
        //      __jiapi_field initialization
        if (!"<clinit>".equals(il.getDeclaringMethod().getName())) {
            // FIXME: See bug above
            il.insert(superIdx+1, entryList);
        }
        else {
            int idx = findFieldInitIndex(il, jiapiField);
            if (idx != -1) {
                il.insert(idx + 1, entryList);
            }
        }

        // Create instructions for method exits
        InstructionList exitList = il.createEmptyList();
        exitList.add(factory.getField(jiapiField));
        if (isStatic) {
            exitList.add(factory.pushConstant(il.getDeclaringMethod().getDeclaringClass().getName())); // BUG: we should pass a Class
        }
        else {
            exitList.add(factory.pushThis());
        }

        exitList.add(factory.pushConstant(il.getDeclaringMethod().getName()));
        exitList.add(factory.invoke(methodExited));

        // Find all method exits
        int idx = il.indexOf(OpcodeGroups.RETURN_INSTRUCTIONS, 0);
        while (idx != -1) {
            Instruction ins = il.get(idx);
View Full Code Here

                mee.printStackTrace();
            }
        }

        InstructionList il = clinit.getInstructionList();
        InstructionFactory factory = il.getInstructionFactory();
        InstructionList initializer = il.createEmptyList();

        try {
            JiapiClass er = currentClass.getLoader().loadClass("alt.jiapi.event.EventRuntime");
            JiapiMethod gfv =
                er.getDeclaredMethod("getFieldValue",
                                     new String[] {"java.lang.String"});

            // Get the field value from runtime
            initializer.add(factory.pushConstant(fieldName));
            initializer.add(factory.invoke(gfv));


            // put the returned event producer to __jiapi_field
            initializer.add(factory.cast(ep.getClass().getName()));
            initializer.add(factory.setField(eventProducerField));
        }
        catch(NoSuchMethodException nsme) {
            nsme.printStackTrace();
        }
        catch(ClassNotFoundException cnfe) {
View Full Code Here

     * Instrument instruction list.
     */
    private void patchInstructionList(InstructionList il,
                                      Instrumentation instrumentation) {
        //System.out.println(">> " + il.getDeclaringMethod());
        InstructionFactory factory = il.getInstructionFactory();
        if (factory == null) {
            throw new NullPointerException("Got null factory");
        }

        int currentMethodModifiers = il.getDeclaringMethod().getModifiers();

        JiapiClass clazz = getCurrentClass();
        Class[] hookParams = hookMethod.getParameterTypes();

        if (isDynamic) {
            log.debug("Hook is dynamic");
            // Obtain a field, that holds a reference to the method
            // to be called.
            JiapiField field = null;
            try {
                field = clazz.getDeclaredField(jiapiFieldName);
            }
            catch (NoSuchFieldException nsfe) {
                throw new JiapiRuntimeException("No such field: " + nsfe.getMessage());
            }

            il.add(factory.getField(field));

            if (Modifier.isStatic(currentMethodModifiers)) {
                il.add(factory.pushConstant(clazz.getName()));
            }
            else {
                il.add(factory.pushThis()); // First argument is 'this'
            }
        }
        else {
            log.debug("Hook is static");
            // On static methods, First argument is name of the current class.
            // This should be Class object.
            // Runtime.forName(clazz.getName())
            // factory.forName(clazz.getName())
            il.add(factory.pushConstant(clazz.getName()));
        }

        // Skip first param (source object made above)
        for (int i = 1; i < hookParams.length; i++) {
            if (hookParams[i].equals(String.class)) {
                // ---  target name  ---
                String targetName = instrumentation.getTargetName();
                if (targetName == null) {
                    targetName = "???";
                }

                il.add(factory.pushConstant(targetName));
            }
            else if (hookParams[i].equals(Object.class)) {
                // ---  target Object  ---
                InstructionList targetCode = instrumentation.getTargetCode();
                if (targetCode != null) {
                    log.debug("Got target code: " + targetCode);
                    il.add(targetCode);
                }
                else {
                    log.debug("No target code");
                    il.add(factory.pushNull());
                }
            }
            else if (hookParams[i].equals(Object[].class)) {
                // ---  target Object[]  ---
                log.warn("target arguments are not supported");
                il.add(factory.pushNull());
            }
            else {
                log.error("Invalid Hook method: " + hookMethod);
            }
        }

        Loader l = new Loader();
        try {
            JiapiClass hClass = l.loadClass(hookClass.getName());
            Class[] hpTypes = hookMethod.getParameterTypes();
            String[] pTypes = new String[hpTypes.length];
           
            for (int i = 0; i < pTypes.length; i++) {
                pTypes[i] = hpTypes[i].getName();
            }
           
            JiapiMethod hMethod =hClass.getDeclaredMethod(hookMethod.getName(),
                                                          pTypes);
            il.add(factory.invoke(hMethod));
        }
        catch(Exception e) {
            log.error("Failed to add invoke instruction: " + e);
        }
    }
View Full Code Here

        if (il.size() > 0) {
            index = il.indexOf(OpcodeGroups.REFERENCE_STORE_INSTRUCTIONS,0);
            log.debug("First instruction during analyze is " + il.get(0));

            if (index == 0) {
                InstructionFactory factory =
                    il.getInstructionFactory();

                // First instruction should be the one that stores
                // Exception instance into local variable<n>.
                // If it is not, we might have already instrumented this part
View Full Code Here

TOP

Related Classes of alt.jiapi.reflect.InstructionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.