Created
January 31, 2011 21:49
-
-
Save jponge/804894 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package makemeaclass; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import org.objectweb.asm.*; | |
import org.objectweb.asm.util.*; | |
import org.objectweb.asm.tree.*; | |
public class MakeMeAClass implements Opcodes { | |
@SuppressWarnings("unchecked") | |
static void createClass() throws IOException { | |
ClassNode cnode = new ClassNode(); | |
cnode.version = V1_5; | |
cnode.sourceFile = "Grappa.java"; | |
cnode.access = ACC_PUBLIC; | |
cnode.name = "grappa/Grappa"; | |
cnode.superName = "java/lang/Object"; | |
cnode.fields.add(new FieldNode(ACC_PRIVATE + ACC_FINAL, "grappa", "Ljava/lang/String;", null, "Grappa!")); | |
MethodNode ctNode = new MethodNode(ACC_PUBLIC, "<init>", "()V", null, null); | |
ctNode.instructions.add(new VarInsnNode(ALOAD, 0)); | |
ctNode.instructions.add(new MethodInsnNode(INVOKESPECIAL, "java/lang/Object", "<init>", "()V")); | |
ctNode.instructions.add(new VarInsnNode(ALOAD, 0)); | |
ctNode.instructions.add(new LdcInsnNode("Grappa!")); | |
ctNode.instructions.add(new FieldInsnNode(PUTFIELD, "grappa/Grappa", "grappa", "Ljava/lang/String;")); | |
ctNode.instructions.add(new InsnNode(RETURN)); | |
MethodNode mnode = new MethodNode(ACC_PUBLIC, "grappaMe", "()Ljava/lang/String;", null, null); | |
mnode.instructions.add(new VarInsnNode(ALOAD, 0)); | |
mnode.instructions.add(new FieldInsnNode(GETFIELD, "grappa/Grappa", "grappa", "Ljava/lang/String;")); | |
mnode.instructions.add(new InsnNode(ARETURN)); | |
cnode.methods.add(ctNode); | |
cnode.methods.add(mnode); | |
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); | |
cnode.accept(writer); | |
StringWriter sw = new StringWriter(); | |
PrintWriter pw = new PrintWriter(sw); | |
CheckClassAdapter.verify(new ClassReader(writer.toByteArray()), false, pw); | |
FileOutputStream out = new FileOutputStream("bin/grappa/Grappa.class"); | |
out.write(writer.toByteArray()); | |
out.close(); | |
} | |
static void useClass() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { | |
Class<?> clazz = Class.forName("grappa.Grappa"); | |
Object instance = clazz.newInstance(); | |
Method grappa = clazz.getMethod("grappaMe"); | |
String result = (String) grappa.invoke(instance); | |
System.out.println(result); | |
} | |
public static void main(String[] args) throws Throwable { | |
createClass(); | |
useClass(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment