Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.krb;
- import java.io.PrintStream;
- import org.objectweb.asm.ClassWriter;
- import org.objectweb.asm.Opcodes;
- import org.objectweb.asm.Type;
- import org.objectweb.asm.commons.GeneratorAdapter;
- import org.objectweb.asm.commons.Method;
- public class Helloworld extends ClassLoader implements Opcodes {
- public static void main(final String args[]) throws Exception {
- // Generates the bytecode corresponding to the following Java class:
- //
- // public class Example {
- // public static void main (String[] args) {
- // System.out.println("Hello world!");
- // }
- // }
- // creates a ClassWriter for the Example public class,
- // which inherits from Object
- ClassWriter cw = new ClassWriter(0);
- cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
- cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
- // creates a GeneratorAdapter for the (implicit) constructor
- Method m = Method.getMethod("void <init> ()");
- GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC,
- m,
- null,
- null,
- cw);
- mg.loadThis();
- mg.invokeConstructor(Type.getType(Object.class), m);
- mg.returnValue();
- mg.endMethod();
- // creates a GeneratorAdapter for the 'main' method
- m = Method.getMethod("void main (String[])");
- mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
- mg.getStatic(Type.getType(System.class),
- "out",
- Type.getType(PrintStream.class));
- mg.push("Hello world!");
- mg.invokeVirtual(Type.getType(PrintStream.class),
- Method.getMethod("void println (String)"));
- mg.returnValue();
- mg.endMethod();
- cw.visitEnd();
- byte[] code = cw.toByteArray();
- Helloworld loader = new Helloworld();
- Class exampleClass = loader.defineClass("Example", code, 0, code.length);
- // uses the dynamically generated class to print 'Helloworld'
- exampleClass.getMethods()[0].invoke(null, new Object[] { null });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement