Package org.mozilla.classfile

Examples of org.mozilla.classfile.ClassFileWriter


        return c.getByteCode();
    }

    static byte[] mozillaClassFileHelloWorld() {
        ClassFileWriter c = new ClassFileWriter("HelloWorld",
                "java/lang/Object",
                "HelloWorld.java");

        c.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        c.addLoadThis();
        c.addInvoke(ByteCode.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        c.add(ByteCode.RETURN);
        c.stopMethod((short) 1);

        c.startMethod("main",
                "()V",
                (short) (ClassFileWriter.ACC_PUBLIC | ClassFileWriter.ACC_STATIC));
        c.add(ByteCode.GETSTATIC,
                "java/lang/System",
                "out",
                "Ljava/io/PrintStream;");
        c.addPush("Hello world!");
        c.addInvoke(ByteCode.INVOKEVIRTUAL,
                "java/io/PrintStream",
                "println",
                "(Ljava/lang/String;)V");
        c.add(ByteCode.RETURN);
        c.stopMethod((short) 1);

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


   
   
    private static byte[] loadBytecode()
    {
        String secureCallerClassName = SecureCaller.class.getName();
        ClassFileWriter cfw = new ClassFileWriter(
                secureCallerClassName + "Impl", secureCallerClassName,
                "<generated>");
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.addALoad(0);
        cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName,
                "<init>", "()V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1);
        String callableCallSig =
            "Lorg/mozilla/javascript/Context;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "[Ljava/lang/Object;)Ljava/lang/Object;";
       
        cfw.startMethod("call",
                "(Lorg/mozilla/javascript/Callable;" + callableCallSig,
                (short)(ClassFileWriter.ACC_PUBLIC
                        | ClassFileWriter.ACC_FINAL));
        for(int i = 1; i < 6; ++i) {
            cfw.addALoad(i);
        }
        cfw.addInvoke(ByteCode.INVOKEINTERFACE,
                "org/mozilla/javascript/Callable", "call",
                "(" + callableCallSig);
        cfw.add(ByteCode.ARETURN);
        cfw.stopMethod((short)6);
        return cfw.toByteArray();
    }
View Full Code Here

   
   
    private static byte[] loadBytecode()
    {
        String secureCallerClassName = SecureCaller.class.getName();
        ClassFileWriter cfw = new ClassFileWriter(
                secureCallerClassName + "Impl", secureCallerClassName,
                "<generated>");
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.addALoad(0);
        cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName,
                "<init>", "()V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1);
        String callableCallSig =
            "Lorg/mozilla/javascript/Context;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "[Ljava/lang/Object;)Ljava/lang/Object;";
       
        cfw.startMethod("call",
                "(Lorg/mozilla/javascript/Callable;" + callableCallSig,
                (short)(ClassFileWriter.ACC_PUBLIC
                        | ClassFileWriter.ACC_FINAL));
        for(int i = 1; i < 6; ++i) {
            cfw.addALoad(i);
        }
        cfw.addInvoke(ByteCode.INVOKEINTERFACE,
                "org/mozilla/javascript/Callable", "call",
                "(" + callableCallSig);
        cfw.add(ByteCode.ARETURN);
        cfw.stopMethod((short)6);
        return cfw.toByteArray();
    }
View Full Code Here

   
   
    private static byte[] loadBytecode()
    {
        String secureCallerClassName = SecureCaller.class.getName();
        ClassFileWriter cfw = new ClassFileWriter(
                secureCallerClassName + "Impl", secureCallerClassName,
                "<generated>");
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.addALoad(0);
        cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName,
                "<init>", "()V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1);
        String callableCallSig =
            "Lorg/mozilla/javascript/Context;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "[Ljava/lang/Object;)Ljava/lang/Object;";
       
        cfw.startMethod("call",
                "(Lorg/mozilla/javascript/Callable;" + callableCallSig,
                (short)(ClassFileWriter.ACC_PUBLIC
                        | ClassFileWriter.ACC_FINAL));
        for(int i = 1; i < 6; ++i) {
            cfw.addALoad(i);
        }
        cfw.addInvoke(ByteCode.INVOKEINTERFACE,
                "org/mozilla/javascript/Callable", "call",
                "(" + callableCallSig);
        cfw.add(ByteCode.ARETURN);
        cfw.stopMethod((short)6);
        return cfw.toByteArray();
    }
View Full Code Here

            classNum = ++classNumber;
        }

        String className = "inv" + classNum;

        ClassFileWriter cfw = new ClassFileWriter(className,
                                "org.mozilla.javascript.Invoker", "");
        cfw.setFlags((short)(ClassFileWriter.ACC_PUBLIC |
                             ClassFileWriter.ACC_FINAL));

        // Add our instantiator!
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.add(ByteCode.ALOAD_0);
        cfw.add(ByteCode.INVOKESPECIAL,
                "org.mozilla.javascript.Invoker",
                "<init>", "()", "V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1, null); // one argument -- this???

        // Add the invoke() method call
        cfw.startMethod("invoke",
                        "(Ljava/lang/Object;[Ljava/lang/Object;)"+
                         "Ljava/lang/Object;",
                        (short)(ClassFileWriter.ACC_PUBLIC |
                                ClassFileWriter.ACC_FINAL));

        // If we return a primitive type, then do something special!
        String declaringClassName = method.getDeclaringClass().getName
            ().replace('.', '/');
        Class returnType = method.getReturnType();
        String invokeSpecial = null;
        String invokeSpecialType = null;
        boolean returnsVoid = false;
        boolean returnsBoolean = false;

        if (returnType.isPrimitive()) {
            if (returnType == Boolean.TYPE) {
                returnsBoolean = true;
                invokeSpecialType = "(Z)";
            } else if (returnType == Void.TYPE) {
                returnsVoid = true;
                invokeSpecialType = "(V)";
            } else if (returnType == Integer.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Integer");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(I)";
            } else if (returnType == Long.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Long");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(J)";
            } else if (returnType == Short.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Short");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(S)";
            } else if (returnType == Float.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Float");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(F)";
            } else if (returnType == Double.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Double");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(D)";
            } else if (returnType == Byte.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Byte");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(B)";
            } else if (returnType == Character.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial
                            = "java/lang/Character");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(C)";
            }
        }

        // handle setup of call to virtual function (if calling non-static)
        if (!java.lang.reflect.Modifier.isStatic(method.getModifiers())) {
            cfw.add(ByteCode.ALOAD_1);
            cfw.add(ByteCode.CHECKCAST, declaringClassName);
        }

        // Handle parameters!
        StringBuffer params = new StringBuffer(2 + ((types!=null)?(20 *
                                types.length):0));

        params.append('(');
        if (types != null) {
            for(int i = 0; i < types.length; i++) {
                Class type = types[i];

                cfw.add(ByteCode.ALOAD_2);

                if (i <= 5) {
                    cfw.add((byte) (ByteCode.ICONST_0 + i));
                } else if (i <= Byte.MAX_VALUE) {
                    cfw.add(ByteCode.BIPUSH, i);
                } else if (i <= Short.MAX_VALUE) {
                    cfw.add(ByteCode.SIPUSH, i);
                } else {
                    cfw.addLoadConstant((int)i);
                }

                cfw.add(ByteCode.AALOAD);

                if (type.isPrimitive()) {
                    // Convert enclosed type back to primitive.

                    if (type == Boolean.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Boolean");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Boolean",
                                "booleanValue", "()", "Z");
                        params.append('Z');
                    } else if (type == Integer.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "intValue", "()", "I");
                        params.append('I');
                    } else if (type == Short.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "shortValue", "()", "S");
                        params.append('S');
                    } else if (type == Character.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Character");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Character",
                                "charValue", "()", "C");
                        params.append('C');
                    } else if (type == Double.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "doubleValue", "()", "D");
                        params.append('D');
                    } else if (type == Float.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "floatValue", "()", "F");
                        params.append('F');
                    } else if (type == Byte.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Byte");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Byte",
                                "byteValue", "()", "B");
                        params.append('B');
                    }
                } else {
                    String typeName = type.getName().replace('.', '/');
                    cfw.add(ByteCode.CHECKCAST, typeName);

                    if (!type.isArray()) {
                        params.append('L');
                    }
                    params.append(typeName);

                    if (!type.isArray()) {
                        params.append(';');
                    }
                }
            }
        }
        params.append(')');

        // Call actual function!
        if (!java.lang.reflect.Modifier.isStatic(method.getModifiers())) {
            cfw.add(ByteCode.INVOKEVIRTUAL, declaringClassName,
                    method.getName(), params.toString(),
                    (invokeSpecialType!=null?invokeSpecialType.substring(1,2)
                                             :returnType.isArray()?
                                              returnType.getName().replace('.','/')
                                              :"L".concat
                (returnType.getName().replace('.', '/').concat(";"))));
        } else {
            cfw.add(ByteCode.INVOKESTATIC, declaringClassName,
                    method.getName(), params.toString(),
                    (invokeSpecialType!=null?invokeSpecialType.substring(1,2)
                                             :returnType.isArray()?
                                              returnType.getName().replace('.','/')
                                              :"L".concat
                (returnType.getName().replace('.', '/').concat(";"))));
        }

        // Handle return value
        if (returnsVoid) {
            cfw.add(ByteCode.ACONST_NULL);
            cfw.add(ByteCode.ARETURN);
        } else if (returnsBoolean) {
            // HACK
            //check to see if true;
            // '7' is the number of bytes of the ifeq<branch> plus getstatic<TRUE> plus areturn instructions
            cfw.add(ByteCode.IFEQ, 7);
            cfw.add(ByteCode.GETSTATIC,
                    "java/lang/Boolean",
                    "TRUE",
                    "Ljava/lang/Boolean;");
            cfw.add(ByteCode.ARETURN);
            cfw.add(ByteCode.GETSTATIC,
                    "java/lang/Boolean",
                    "FALSE",
                    "Ljava/lang/Boolean;");
            cfw.add(ByteCode.ARETURN);
        } else if (invokeSpecial != null) {
            cfw.add(ByteCode.INVOKESPECIAL,
                    invokeSpecial,
                    "<init>", invokeSpecialType, "V");
            cfw.add(ByteCode.ARETURN);
        } else {
            cfw.add(ByteCode.ARETURN);
        }
        cfw.stopMethod((short)3, null); // three arguments, including the this pointer???

        byte[] bytes = cfw.toByteArray();

        // Add class to our classloader.
        Class c = classLoader.defineClass(className, bytes);
        classLoader.linkClass(c);
        try {
View Full Code Here


    private static byte[] loadBytecode()
    {
        String secureCallerClassName = SecureCaller.class.getName();
        ClassFileWriter cfw = new ClassFileWriter(
                secureCallerClassName + "Impl", secureCallerClassName,
                "<generated>");
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.addALoad(0);
        cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName,
                "<init>", "()V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1);
        String callableCallSig =
            "Lorg/mozilla/javascript/Context;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "[Ljava/lang/Object;)Ljava/lang/Object;";

        cfw.startMethod("call",
                "(Lorg/mozilla/javascript/Callable;" + callableCallSig,
                (short)(ClassFileWriter.ACC_PUBLIC
                        | ClassFileWriter.ACC_FINAL));
        for(int i = 1; i < 6; ++i) {
            cfw.addALoad(i);
        }
        cfw.addInvoke(ByteCode.INVOKEINTERFACE,
                "org/mozilla/javascript/Callable", "call",
                "(" + callableCallSig);
        cfw.add(ByteCode.ARETURN);
        cfw.stopMethod((short)6);
        return cfw.toByteArray();
    }
View Full Code Here

        Set<Method> methods = new HashSet<Method>();
        Class<?> clazz = (Class<?>)classes[0];
        boolean isInterface = clazz.isInterface();
        String superName = isInterface ? Object.class.getName() : clazz.getName();
        String adapterSignature = classToSignature(EventAdapter.class);
        ClassFileWriter cfw = new ClassFileWriter(className, superName,
                                                  "<EventAdapter>");
        for (Object c : classes) {
            clazz = (Class<?>)c;
            if (clazz.isInterface()) {
                cfw.addInterface(clazz.getName());
            }
            Collections.addAll(methods, clazz.getMethods());
        }

        cfw.addField("events", adapterSignature, (short) (ACC_PRIVATE | ACC_FINAL));

        cfw.startMethod("<init>", "(" + adapterSignature + ")V", ACC_PUBLIC);
        // Invoke base class constructor
        cfw.addLoadThis();
        cfw.addInvoke(ByteCode.INVOKESPECIAL, superName, "<init>", "()V");
        cfw.addLoadThis();
        cfw.add(ByteCode.ALOAD_1)// event adapter
        cfw.add(ByteCode.PUTFIELD, cfw.getClassName(), "events", adapterSignature);
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)2);

        for (Method method : methods) {
            String methodName = method.getName();
            String eventName = overrides == null
                    ? toEventName(methodName)
                    : toStringOrNull(overrides.get(methodName));
            int mod = method.getModifiers();
            if (!Modifier.isAbstract(mod) && (eventName == null || Modifier.isFinal(mod))) {
                continue;
            }
            Class<?>[]paramTypes = method.getParameterTypes();
            int paramLength = paramTypes.length;
            int localsLength = paramLength + 1;
            for (Class<?> c : paramTypes) {
                // adjust locals length for long and double parameters
                if (c == Double.TYPE || c == Long.TYPE) ++localsLength;
            }
            Class<?>returnType = method.getReturnType();
            cfw.startMethod(methodName, getSignature(paramTypes, returnType), ACC_PUBLIC);
            cfw.addLoadThis();
            cfw.add(ByteCode.GETFIELD, cfw.getClassName(), "events", adapterSignature);
            cfw.addLoadConstant(eventName); // event type
            cfw.addLoadConstant(paramLength)// create args array
            cfw.add(ByteCode.ANEWARRAY, "java/lang/Object");
            for (int i = 0; i < paramLength; i++) {
                cfw.add(ByteCode.DUP);
                cfw.addLoadConstant(i);
                Class<?> param = paramTypes[i];
                if (param == Integer.TYPE || param == Byte.TYPE
                        || param == Character.TYPE || param == Short.TYPE) {
                    cfw.addILoad(i + 1);
                    cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Integer",
                            "valueOf", "(I)Ljava/lang/Integer;");
                } else if (param == Boolean.TYPE) {
                    cfw.addILoad(i + 1);
                    cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Boolean",
                            "valueOf", "(Z)Ljava/lang/Boolean;");
                } else if (param == Double.TYPE) {
                    cfw.addDLoad(i + 1);
                    cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Double",
                            "valueOf", "(I)Ljava/lang/Double;");
                } else if (param == Float.TYPE) {
                    cfw.addFLoad(i + 1);
                    cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Float",
                            "valueOf", "(I)Ljava/lang/Float;");
                } else if (param == Long.TYPE) {
                    cfw.addLLoad(i + 1);
                    cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Long",
                            "valueOf", "(I)Ljava/lang/Long;");
                } else {
                    cfw.addALoad(i + 1);
                }
                cfw.add(ByteCode.AASTORE);
            }
            cfw.addInvoke(ByteCode.INVOKEVIRTUAL, EventAdapter.class.getName(),
                    "emit", "(Ljava/lang/String;[Ljava/lang/Object;)Z");
            cfw.add(ByteCode.POP); // always discard result of emit()
            if (returnType == Void.TYPE) {
                cfw.add(ByteCode.RETURN);
            } else if (returnType == Integer.TYPE || returnType == Byte.TYPE
                    || returnType == Character.TYPE || returnType == Short.TYPE) {
                cfw.add(ByteCode.ICONST_0);
                cfw.add(ByteCode.IRETURN);
            } else if (returnType == Boolean.TYPE) {
                cfw.add(ByteCode.ICONST_1); // return true for boolean
                cfw.add(ByteCode.IRETURN);
            } else if (returnType == Double.TYPE) {
                cfw.add(ByteCode.DCONST_0);
                cfw.add(ByteCode.DRETURN);
            } else if (returnType == Float.TYPE) {
                cfw.add(ByteCode.FCONST_0);
                cfw.add(ByteCode.FRETURN);
            } else if (returnType == Long.TYPE) {
                cfw.add(ByteCode.LCONST_0);
                cfw.add(ByteCode.LRETURN);
            } else {
                cfw.add(ByteCode.ACONST_NULL);
                cfw.add(ByteCode.ARETURN);
            }
            cfw.stopMethod((short)(localsLength));
        }

        return cfw.toByteArray();
    }
View Full Code Here

            classNum = ++classNumber;
        }

        String className = "inv" + classNum;

        ClassFileWriter cfw = new ClassFileWriter(className,
                                "org.mozilla.javascript.Invoker", "");
        cfw.setFlags((short)(ClassFileWriter.ACC_PUBLIC |
                             ClassFileWriter.ACC_FINAL));

        // Add our instantiator!
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.add(ByteCode.ALOAD_0);
        cfw.add(ByteCode.INVOKESPECIAL,
                "org.mozilla.javascript.Invoker",
                "<init>", "()", "V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1, null); // one argument -- this???

        // Add the invoke() method call
        cfw.startMethod("invoke",
                        "(Ljava/lang/Object;[Ljava/lang/Object;)"+
                         "Ljava/lang/Object;",
                        (short)(ClassFileWriter.ACC_PUBLIC |
                                ClassFileWriter.ACC_FINAL));

        // If we return a primitive type, then do something special!
        String declaringClassName = method.getDeclaringClass().getName
            ().replace('.', '/');
        Class returnType = method.getReturnType();
        String invokeSpecial = null;
        String invokeSpecialType = null;
        boolean returnsVoid = false;
        boolean returnsBoolean = false;

        if (returnType.isPrimitive()) {
            if (returnType == Boolean.TYPE) {
                returnsBoolean = true;
                invokeSpecialType = "(Z)";
            } else if (returnType == Void.TYPE) {
                returnsVoid = true;
                invokeSpecialType = "(V)";
            } else if (returnType == Integer.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Integer");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(I)";
            } else if (returnType == Long.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Long");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(J)";
            } else if (returnType == Short.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Short");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(S)";
            } else if (returnType == Float.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Float");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(F)";
            } else if (returnType == Double.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Double");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(D)";
            } else if (returnType == Byte.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial = "java/lang/Byte");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(B)";
            } else if (returnType == Character.TYPE) {
                cfw.add(ByteCode.NEW, invokeSpecial
                            = "java/lang/Character");
                cfw.add(ByteCode.DUP);
                invokeSpecialType = "(C)";
            }
        }

        // handle setup of call to virtual function (if calling non-static)
        if (!java.lang.reflect.Modifier.isStatic(method.getModifiers())) {
            cfw.add(ByteCode.ALOAD_1);
            cfw.add(ByteCode.CHECKCAST, declaringClassName);
        }

        // Handle parameters!
        StringBuffer params = new StringBuffer(2 + ((types!=null)?(20 *
                                types.length):0));

        params.append('(');
        if (types != null) {
            for(int i = 0; i < types.length; i++) {
                Class type = types[i];

                cfw.add(ByteCode.ALOAD_2);

                if (i <= 5) {
                    cfw.add((byte) (ByteCode.ICONST_0 + i));
                } else if (i <= Byte.MAX_VALUE) {
                    cfw.add(ByteCode.BIPUSH, i);
                } else if (i <= Short.MAX_VALUE) {
                    cfw.add(ByteCode.SIPUSH, i);
                } else {
                    cfw.addLoadConstant((int)i);
                }

                cfw.add(ByteCode.AALOAD);

                if (type.isPrimitive()) {
                    // Convert enclosed type back to primitive.

                    if (type == Boolean.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Boolean");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Boolean",
                                "booleanValue", "()", "Z");
                        params.append('Z');
                    } else if (type == Integer.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "intValue", "()", "I");
                        params.append('I');
                    } else if (type == Short.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "shortValue", "()", "S");
                        params.append('S');
                    } else if (type == Character.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Character");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Character",
                                "charValue", "()", "C");
                        params.append('C');
                    } else if (type == Double.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "doubleValue", "()", "D");
                        params.append('D');
                    } else if (type == Float.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Number");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Number",
                                "floatValue", "()", "F");
                        params.append('F');
                    } else if (type == Byte.TYPE) {
                        cfw.add(ByteCode.CHECKCAST, "java/lang/Byte");
                        cfw.add(ByteCode.INVOKEVIRTUAL, "java/lang/Byte",
                                "byteValue", "()", "B");
                        params.append('B');
                    }
                } else {
                    String typeName = type.getName().replace('.', '/');
                    cfw.add(ByteCode.CHECKCAST, typeName);

                    if (!type.isArray()) {
                        params.append('L');
                    }
                    params.append(typeName);

                    if (!type.isArray()) {
                        params.append(';');
                    }
                }
            }
        }
        params.append(')');

        // Call actual function!
        if (!java.lang.reflect.Modifier.isStatic(method.getModifiers())) {
            cfw.add(ByteCode.INVOKEVIRTUAL, declaringClassName,
                    method.getName(), params.toString(),
                    (invokeSpecialType!=null?invokeSpecialType.substring(1,2)
                                             :returnType.isArray()?
                                              returnType.getName().replace('.','/')
                                              :"L".concat
                (returnType.getName().replace('.', '/').concat(";"))));
        } else {
            cfw.add(ByteCode.INVOKESTATIC, declaringClassName,
                    method.getName(), params.toString(),
                    (invokeSpecialType!=null?invokeSpecialType.substring(1,2)
                                             :returnType.isArray()?
                                              returnType.getName().replace('.','/')
                                              :"L".concat
                (returnType.getName().replace('.', '/').concat(";"))));
        }

        // Handle return value
        if (returnsVoid) {
            cfw.add(ByteCode.ACONST_NULL);
            cfw.add(ByteCode.ARETURN);
        } else if (returnsBoolean) {
            // HACK
            //check to see if true;
            // '7' is the number of bytes of the ifeq<branch> plus getstatic<TRUE> plus areturn instructions
            cfw.add(ByteCode.IFEQ, 7);
            cfw.add(ByteCode.GETSTATIC,
                    "java/lang/Boolean",
                    "TRUE",
                    "Ljava/lang/Boolean;");
            cfw.add(ByteCode.ARETURN);
            cfw.add(ByteCode.GETSTATIC,
                    "java/lang/Boolean",
                    "FALSE",
                    "Ljava/lang/Boolean;");
            cfw.add(ByteCode.ARETURN);
        } else if (invokeSpecial != null) {
            cfw.add(ByteCode.INVOKESPECIAL,
                    invokeSpecial,
                    "<init>", invokeSpecialType, "V");
            cfw.add(ByteCode.ARETURN);
        } else {
            cfw.add(ByteCode.ARETURN);
        }
        cfw.stopMethod((short)3, null); // three arguments, including the this pointer???

        byte[] bytes = cfw.toByteArray();

        // Add class to our classloader.
        Class c = classLoader.defineClass(className, bytes);
        classLoader.linkClass(c);
        try {
View Full Code Here

   
   
    private static byte[] loadBytecode()
    {
        String secureCallerClassName = SecureCaller.class.getName();
        ClassFileWriter cfw = new ClassFileWriter(
                secureCallerClassName + "Impl", secureCallerClassName,
                "<generated>");
        cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC);
        cfw.addALoad(0);
        cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName,
                "<init>", "()V");
        cfw.add(ByteCode.RETURN);
        cfw.stopMethod((short)1);
        String callableCallSig =
            "Lorg/mozilla/javascript/Context;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "Lorg/mozilla/javascript/Scriptable;" +
            "[Ljava/lang/Object;)Ljava/lang/Object;";
       
        cfw.startMethod("call",
                "(Lorg/mozilla/javascript/Callable;" + callableCallSig,
                (short)(ClassFileWriter.ACC_PUBLIC
                        | ClassFileWriter.ACC_FINAL));
        for(int i = 1; i < 6; ++i) {
            cfw.addALoad(i);
        }
        cfw.addInvoke(ByteCode.INVOKEINTERFACE,
                "org/mozilla/javascript/Callable", "call",
                "(" + callableCallSig);
        cfw.add(ByteCode.ARETURN);
        cfw.stopMethod((short)6);
        return cfw.toByteArray();
    }
View Full Code Here

TOP

Related Classes of org.mozilla.classfile.ClassFileWriter

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.