Package alt.jiapi.reflect

Examples of alt.jiapi.reflect.InstructionFactory


        }
    }


    private void addClassForNameInstructions(String name, InstructionList il) {
        InstructionFactory f = il.getInstructionFactory();

        InstructionList nl = il.createEmptyList();
       
        // NOTE: We do not create exception handlers for
        //       Class.forName(...) invocation.
        //       However, we use this to get Class of the running object,
        //       so its Class is allways found.
        try {
            nl.add(f.pushConstant(name));
            nl.add(f.invoke(Modifier.STATIC, "java.lang.Class",
                            "forName", new Signature("java.lang.Class",
                                                     new String[] {"java.lang.String"})));
        }
        catch(Exception e) {
            e.printStackTrace();
            il.add(f.pushNull());
        }

        il.add(nl);
    }
View Full Code Here


    }


    private void handleReturnValue(InstructionList il, String rType) {
        // Convert return value if needed.
        InstructionFactory factory = il.getInstructionFactory();

        if ("int".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Integer");
                JiapiMethod jm =
                    jc.getDeclaredMethod("intValue", new String[0]);

                il.add(factory.cast("java.lang.Integer"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("long".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Long");
                JiapiMethod jm =
                    jc.getDeclaredMethod("longValue", new String[0]);

                il.add(factory.cast("java.lang.Long"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("char".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Character");
                JiapiMethod jm =
                    jc.getDeclaredMethod("charValue", new String[0]);

                il.add(factory.cast("java.lang.Character"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("boolean".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Boolean");
                JiapiMethod jm =
                    jc.getDeclaredMethod("booleanValue", new String[0]);

                il.add(factory.cast("java.lang.Boolean"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("byte".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Byte");
                JiapiMethod jm =
                    jc.getDeclaredMethod("byteValue", new String[0]);

                il.add(factory.cast("java.lang.Byte"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("float".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Float");
                JiapiMethod jm =
                    jc.getDeclaredMethod("floatValue", new String[0]);

                il.add(factory.cast("java.lang.Float"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("double".equals(rType)) {
            try {
                JiapiClass jc = new Loader().loadClass("java.lang.Double");
                JiapiMethod jm =
                    jc.getDeclaredMethod("doubleValue", new String[0]);

                il.add(factory.cast("java.lang.Double"));
                il.add(factory.invoke(jm));
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        else if ("void".equals(rType)){
            // Pop out the return value(probably null) of
            // the invocation handler if it was a 'void' method
            il.add(new Instruction(new byte[]{Opcodes.POP}));
        }
        else { // Cast to correct Object
            il.add(factory.cast(rType));
        }
    }
View Full Code Here

        if ("<clinit>".equals(jm.getName())) {
            return;
        }

        InstructionFactory factory = il.getInstructionFactory();
        JiapiClass ii = getEventProducer();
        JiapiMethod invokeMethod = null;

        try {
            invokeMethod =
                ii.getDeclaredMethod("invokeMethod",
                                     new String[] { "java.lang.Object",
                                                    "java.lang.String",
                                                    "java.lang.Object[]",
                                                    "java.lang.String"});
        }
        catch(Exception e) {
            e.printStackTrace();
        }


        int idx = -1;
        while ((idx = il.indexOf(OpcodeGroups.INVOKE_INSTRUCTIONS, idx+1)) != -1) {
            Invocation ins = (Invocation)il.get(idx);
            if (!match(ins.getClassName() + "." + ins.getMethodName())) {
                continue;
            }


//             if (System.getProperty("no-lfix") == null) {
//                 // --- bug workaround  for long/doubles in method params
//                 boolean bailOutForLongDoubleSyndrome = false;
//                 String[] i_params = ins.getParameterTypes();
//                 for (int i = 0; i < i_params.length; i++) {
//                     if ("double".equals(i_params[i]) ||
//                         "long".equals(i_params[i])) {
//                         bailOutForLongDoubleSyndrome = true;
//                         break;
//                     }
//                 }
//                 if (bailOutForLongDoubleSyndrome) {
//                     log.warn("Will not instrument invocations to methods with long or double as parameter: In " + jm.getDeclaringClass().getName() + "#" + jm +
//                              ", a call to " + ins + ". This is a workaround for bug in jiapi.");
//                     continue;
//                 }
//                 // --- bug workaround  for long/doubles in method params
//             }

            // We support only these methods at the moment.
            if (ins.getOpcode() != Opcodes.INVOKESTATIC &&
                ins.getOpcode() != Opcodes.INVOKEVIRTUAL) {
                continue;
            }

            JiapiField interceptor = getEventProducerField();
       
//              InstructionList nList = il.createEmptyList();
            InstructionList nList = new InstructionList();


            // each entry in pList holds creation of one argument
            // to method invocation
//             InstructionList[] pLists = createArgumentLists(il, idx);
            ArgumentList al = createArgumentLists(il, idx);
            InstructionList[] pLists = al.arguments;
            int paramIdx = al.paramIndex;

            short opCode = ins.getOpcode();
            if (opCode == Opcodes.INVOKEVIRTUAL ||
                opCode == Opcodes.INVOKESPECIAL) {
                paramIdx--; // Include object ref
            }

            // Generate code, that replaces invocation with a new
            // invocation to InvokeHandler
            switch(opCode) {
            case Opcodes.INVOKEVIRTUAL:
            case Opcodes.INVOKESTATIC:

                nList.add(factory.getField(interceptor)); // Interceptor
                if (opCode == Opcodes.INVOKESTATIC) {
//                     addClassForNameInstructions(jm.getDeclaringClass().getName(), nList);
                    addClassForNameInstructions(ins.getClassName(), nList);
                   
                }
                else {
                    nList.add(il.get(paramIdx)); // objref
                }

                String mName = ins.getMethodName();
                //nList.add(factory.getField(rField));
                nList.add(factory.pushConstant(mName));
                nList.add(factory.newArray("java.lang.Object",
                                           ins.getParameterTypes().length));

                String[] i_params = ins.getParameterTypes();
                // Populate Object array with call parameters
                for (int i = 0; i < pLists.length; i++) {
                    if ("long".equals(i_params[i])) {
                        nList.add(factory.dup());
                    }
                    else if ("double".equals(i_params[i])) {
                        nList.add(factory.dup());
                    }
                    else {
                        nList.add(factory.dup());
                    }
                    nList.add(factory.pushConstant(i));
                    nList.add(pLists[i]);
                    nList.add(factory.aastore());
                }

                // Add Methods signature
//                 nList.add(factory.pushConstant(ins.getDescriptor()));
                // Add cache-key
                nList.add(factory.pushConstant(ins.getClassName() + ins.getMethodName() + ins.getDescriptor()));

                // call Interceptor
                nList.add(factory.invoke(invokeMethod));

                handleReturnValue(nList, ins);


                // Replace invocation and its parameters with new
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.