예제 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void generate(java.util.List<Node> instructions, String outputfile) throws java.io.IOException
        public virtual void generate(IList <Node> instructions, string outputfile)
        {
            ClassWriter cw = new ClassWriter(0);

            cw.visit(V1_1, ACC_PUBLIC, outputfile, null, "java/lang/Object", null);

            // creates a MethodWriter for the (implicit) constructor
            MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);

            // pushes the 'this' variable
            mw.visitVarInsn(ALOAD, 0);
            // invokes the super class constructor
            mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            mw.visitInsn(RETURN);
            // this code uses a maximum of one stack element and one local variable
            mw.visitMaxs(1, 1);
            mw.visitEnd();


            // creates a MethodWriter for the 'main' method
            mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
            // pushes the 'out' field (of type PrintStream) of the System class
            mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
            // pushes the "Hello Juliar Future" String constant
            mw.visitLdcInsn("Now Calling generated Juliar Methods!");
            // invokes the 'println' method (defined in the PrintStream class)
            mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
            //mw.visitInsn(RETURN);
            // this code uses a maximum of two stack elements and two local
            // variables
            //mw.visitVarInsn(ALOAD,0);
            mw.visitMethodInsn(INVOKESTATIC, outputfile, "juliarMethod", "()V", false);
            mw.visitInsn(RETURN);

            mw.visitMaxs(2, 2);
            mw.visitEnd();

            mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "juliarMethod", "()V", null, null);

            int?stackSize       = 0;
            GeneratorAdapter ga = new GeneratorAdapter(mw, ACC_PUBLIC + ACC_STATIC, "juliarMethod", "()V");

            evaluateExpressions(instructions, mw, ga, stackSize);



            mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
            mw.visitLdcInsn("Instructions:" + instructions);
            mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);


            mw.visitInsn(RETURN);
            mw.visitMaxs(16, 6);
            mw.visitEnd();

            /*MethodVisitor foo = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "foo", "()V", null, null);
             *
             * GeneratorAdapter ga1 = new GeneratorAdapter(foo, ACC_PUBLIC + ACC_STATIC, "foo", "()V");
             * foo.visitInsn(RETURN);
             * foo.visitEnd();*/


            // gets the bytecode of the Example class, and loads it dynamically
            sbyte[] code = cw.toByteArray();

            //Create JAR output
            FileOutputStream fout = new FileOutputStream(outputfile + ".jar");

            Manifest manifest = new Manifest();

            manifest.MainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
            manifest.MainAttributes.put(Attributes.Name.MAIN_CLASS, outputfile);

            JarOutputStream jarOut = new JarOutputStream(fout, manifest);

            try
            {
                jarOut.putNextEntry(new ZipEntry("com/juliar/pal/Primitives.class"));
                InputStream primitiveStream = typeof(Primitives).getResourceAsStream("Primitives.class");
                jarOut.write(getBytes(primitiveStream));
                jarOut.closeEntry();

                jarOut.putNextEntry(new ZipEntry(outputfile + ".class"));
                jarOut.write(code);
                jarOut.closeEntry();
            }
            catch (Exception e)
            {
                JuliarLogger.log(e);
            }
            jarOut.close();
            fout.close();


            /*
             * List<String> Dependencies = SomeClass.getDependencies();
             * FileOutputStream fout = new FileOutputStream(outputfile+".jar");
             *
             * JarOutputStream jarOut = new JarOutputStream(fout);
             *
             * //jarOut.putNextEntry(new ZipEntry("com/juliar/pal")); // Folders must end with "/".
             * //jarOut.putNextEntry(new ZipEntry("com/juliar/pal/Primitives.class"));
             * //jarOut.write(getBytes("com/juliar/pal/Primitives.class"));
             * //jarOut.closeEntry();
             *
             * jarOut.putNextEntry(new ZipEntry(outputfile+".class"));
             * jarOut.write(getBytes(outputfile+".class"));
             * jarOut.closeEntry();
             *
             * for(String dependency : Dependencies){
             *  int index = dependency.lastIndexOf( '/' );
             *  jarOut.putNextEntry(new ZipEntry(dependency.substring( index ))); // Folders must end with "/".
             *  jarOut.putNextEntry(new ZipEntry(dependency));
             *  jarOut.write(getBytes(dependency));
             *  jarOut.closeEntry();
             * }
             *
             *
             * jarOut.close();
             *
             * fout.close();*/
        }