Creating a .class file for the JVM

I am working on a project that requires me to generate a java .class file on the go, which can later be compiled on the JVM. After learning and working with MSIL (Microsoft IL), which is also an intermediate stack-based programming language, the following problems I encounter are:

  • Compared to IL (for C # or VB), java bytecode in a .class file contains information in a structured way and as far as I can understand the file, it contains metadata, except for program data, is this true ?? Can I create the same in the form of a template for each class file?
  • Is it necessary to generate a class file in binary format?

I referenced Joshua Engel's "Programming for the Java ™ Virtual Machine", but that did not help my purpose, as I already learned about the JVm instruction set.

Can someone please help me with this? All help would be greatly appreciated. An example to create a simple class file would be really useful, since I could not find any yet.

+5
source share
5 answers

Example of using the ASM file byte library converted to work with .NET using the IKVM Java -to -.NET compiler :

Hello.cs:

using System;
using System.IO;
using org.objectweb.asm;

namespace test.helloWorld
{
    public class helloDump
    {

        public static byte[] dump ()
        {

            ClassWriter cw = new ClassWriter(0);
            MethodVisitor mv;

            cw.visit(Opcodes.__Fields.V1_6, Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_SUPER, "hello", null, "java/lang/Object", null);

            mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(Opcodes.__Fields.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.__Fields.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
            mv.visitInsn(Opcodes.__Fields.RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();

            mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
            mv.visitCode();
            mv.visitFieldInsn(Opcodes.__Fields.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
            mv.visitLdcInsn("Hello World!");
            mv.visitMethodInsn(Opcodes.__Fields.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
            mv.visitInsn(Opcodes.__Fields.RETURN);
            mv.visitMaxs(2, 1);
            mv.visitEnd();

            cw.visitEnd();

            return cw.toByteArray();
        }

        public static void Main(string[] args)
        {
            FileStream helloWorldFile = new FileStream("hello.class", FileMode.Create);
            byte[] helloWorldClass = dump();
            helloWorldFile.Seek(0, SeekOrigin.Begin);
            helloWorldFile.Write(helloWorldClass, 0, helloWorldClass.Length);
        }
    }
}

commands:

$ ikvmc -out:org.objectweb.asm.dll -target:library -version:3.2.0.0 asm-3.2.jar
$ mcs -r:org.objectweb.asm.dll  hello.cs
$ mono hello.exe
$ ls hello.class
$ java hello
+4
source

, ASM. . JVM : Clojure, Groovy, Jython, JRuby.

, Java javac, .class . , .

+2

Simple.java javac Simple.java ( , JDK)

public class SimpleJava {
  public static main(String[] args) {
    System.out.println("Big Sorry, couldn't stop myself ;-)");
  }
}

, , ;)

0

.

Java- Java . , BCEL. , " " , - Java.

, -, , , Java .

, ,

(1) , .

(2) , "". java-- - (, ) ( , - , javap - , )

Edit

, , , Java, .

"" Java , . , .

The cool file of my Java encoded evaluator would be the perfect template for generating the "dynamic" class - I just needed to figure out how to "replace" the dummy expression with a real one, recreate the actual class file (you may need to adjust some pointers due to different lengths lines) and it should work.

Even if this assumption is completely false, it can serve as an example and can help you on your way :)

0
source

All Articles