Package alt.jiapi.reflect

Examples of alt.jiapi.reflect.InstructionList


        JiapiClass c = JiapiClass.createClass("HelloWorld");

        // 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


                // Case: start==0, end==0
                // Should we continue here or not.
                end = start;
            }

            InstructionList view = il.createView(start, end);

            forward(view);

            start = boundary.getEnd() + 1; // Just after hotspot
        }
View Full Code Here

        List hotSpots = strategy.findHotSpots(il);

        Iterator iter = hotSpots.iterator();
        while (iter.hasNext()) {
            HotSpot hs = (HotSpot)iter.next();
            InstructionList view = il.createView(hs.getStart(), hs.getEnd());
//             System.out.println("Grep:" + hs.getStart() + "-" + hs.getEnd() +
//                                ":" + view);

            forward(view);
        }
View Full Code Here

        JiapiMethod method = clazz.addMethod(Modifier.PUBLIC, "helloWorld",
                                             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 void instrument(InstructionList il) {
        log.info("Instrumenting " + getCurrentClass().getName() + "." +
                 il.getDeclaringMethod().getName());

        InstructionList view;

        if (instructionCount > il.size()) {
            log.warn("List size exceeded: instructions needed : " +
                     instructionCount + ", available " + il.size() +
                     ". Using all that is available.");
View Full Code Here

    FieldEventInstrumentor(FieldEventProducer fep) {
        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
            il.insert(idx + 1, invokeList);

            // Next index. Skip Instructions created above.
            idx += invokeList.size();
        }
    }
View Full Code Here

    MethodEventInstrumentor(MethodEventProducer mep) {
        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);
            il.insert(idx, exitList);

            // Next index. Skip Instructions created above.
            idx = il.indexOf(OpcodeGroups.RETURN_INSTRUCTIONS,
                             idx + exitList.size() + 1);
        }
    }
View Full Code Here

            catch(MethodExistsException mee) {
                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

                int idx = il.indexOf(inv);

                // Following assumes, that trigger call is
                // void-void method.
                // First, we remove a call to Advice class...
                InstructionList view = il.createView(idx-1, idx + 1);
    view.clear();

                // ...then we replace, that call with trigger action
                // instructions.
                if ("getHotSpotName".equals(inv.getMethodName())) {
                    view.add(factory.pushConstant(hs.getName()));
                }
                else if ("getArguments".equals(inv.getMethodName())) {
                    // NOT IMPLEMENTED
                    // This needs some further thinking before implementing
                }
                else if ("getInstrumentedObject".equals(inv.getMethodName())) {
        JiapiMethod jm = il.getDeclaringMethod();
        int modifiers = jm.getModifiers();
        if (Modifier.isStatic(modifiers)) {
      // Static methods do not have reference to 'this'
      //view.add(factory.pushNull());
      view.add(factory.pushConstant(jm.getDeclaringClass().getName()));
      view.add(factory.invoke(Modifier.STATIC, "java.lang.Class", "forName", new Signature("java.lang.Class", new String[] {"java.lang.String"})));
        }
        else {
      view.add(factory.aload(0));
        }
                    // NOT IMPLEMENTED
                    // This needs some further thinking before implementing
                }
            }
View Full Code Here

                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 {
View Full Code Here

TOP

Related Classes of alt.jiapi.reflect.InstructionList

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.