I am not able to display bytecodes on my screen. I want to generate output alike Javap Command for hello World.
I created following class and hello world class. Could any one please let me know what I have to do to generate bytecode and what am I doing wrong?
**package com.gannon.ASMInterpreterMain;**
import java.io.FileOutputStream;
import java.io.IOException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class CodeGenerator {
public void generateClass()
{
ClassWriter cw=new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "com/gannon/ASMInterpreterMain/HelloWorldOutPut", null, "java/lang/Object", null);
mv=cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv=cw.visitMethod(Opcodes.ACC_PUBLIC+ Opcodes.ACC_STATIC, "Main", "([Ljava/lang/String;)V", null, null);
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("Test");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
cw.visitEnd();
this.WriteClass(cw);
}
public void WriteClass(ClassWriter cw){
FileOutputStream fos;
try{
fos = new FileOutputStream("C:\\Users\\Anish\\workspace\\ASMProject\\Main.class");
fos.write(cw.toByteArray());
fos.close();
}
catch (IOException ex){
System.out.println("Error: "+ex.getMessage());
//Logger.getLogger(CodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main (String [] args){
CodeGenerator CG = new CodeGenerator();
CG.generateClass();
}
}
I tried to use tracefilevisitor, but my code is not getting compiled now, I am getting Exceptions like this; Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class can any body help me here please
package com.gannon.ASMInterpreterMain;
import java.io.PrintWriter;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.util.TraceClassVisitor;
public class CodeGenerator {
public void generateClass() {
ClassWriter cw = new ClassWriter(0);
TraceClassVisitor tc = new TraceClassVisitor(
new PrintWriter(System.out));
MethodVisitor mv;
tc.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC,
"com/gannon/ASMInterpreterMain/HelloWorldOutPut", null,
"java/lang/Object", null);
mv = tc.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>",
"()V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = tc.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "Main",
"([Ljava/lang/String;)V", null, null);
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("Test");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream",
"println", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
tc.visitEnd();
byte [] b = cw.toByteArray();
System.out.println(b.length);
for (int i = 1; i < b.length; i++) {
System.out.println(b[i]);
}
}
// public void WriteClass(ClassWriter cw) {
// FileOutputStream fos;
// try {
// fos = new FileOutputStream(
// "C:\\Users\\Anish\\workspace\\ASMProject\\Main.class");
// fos.write(cw.toByteArray());
// fos.close();
// } catch (IOException ex) {
//
// System.out.println("Error: " + ex.getMessage());
// //Logger.getLogger(CodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
//public static void main (String [] args){
// CodeGenerator CG = new CodeGenerator();
// CG.generateClass();
//}
public static void main(String[] args) throws Exception {
// String pathToClassFile = "C:\\Users\\Anish\\workspace\\ASMProject\\Main.class";
// Textifier.main(pathToClassFile);
CodeGenerator CG = new CodeGenerator();
CG.generateClass();
}
}
Hi, I tried implementing the concept that you had ask but my code is still not working, I am attaching my code here that i want to run to display opcodes for my helloworld program. could you please help me in debugging this code? i implemented textifier but it seems it ask for string array object in main method. I tried converting my class file path but it didn't work. Could you please help here?
package com.gannon.ASMInterpreterMain;
import java.io.PrintWriter;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.util.TraceClassVisitor;
public class CodeGenerator {
public void generateClass() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
TraceClassVisitor tc = new TraceClassVisitor( cw,
new PrintWriter(System.out));
MethodVisitor mv;
tc.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC,
"com/gannon/ASMInterpreterMain/HelloWorldOutPut", null,
"java/lang/Object", null);
mv = tc.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>",
"()V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = tc.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "Main",
"([Ljava/lang/String;)V", null, null);
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("Test");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream",
"println", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
tc.visitEnd();
byte [] b = cw.toByteArray();
System.out.println(b.length);
for (int i = 1; i < b.length; i++) {
System.out.println(b[i]);
}
}
public static void main (String [] args){
String[] pathToClassFile = {"com/gannon/ASMInterpreterMain/HelloWorldOutPut"};
try {
Textifier.main(pathToClassFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Excption thrown from main is "+e.getMessage());
}
}
}