Instantly share code, notes, and snippets.

Embed
What would you like to do?
throw new Object()
package ru.izebit;
import org.apache.bcel.Const;
import org.apache.bcel.generic.*;
import java.io.FileOutputStream;
/**
* @author <a href="izebit@gmail.com">Artem Konovalov</a> <br/>
* Date: 27/04/2018/.
*/
public class TestExample {
public static void main(String[] argv) throws Exception {
generator();
}
private static void generator() throws Exception {
String className = "ru.izebit.Test";
ClassGen classGenerator = new ClassGen(className, "java.lang.Object", "Test", Const.ACC_PUBLIC, new String[0]);
InstructionFactory instructionFactory = new InstructionFactory(classGenerator);
// test();
// return;
InstructionList instructionListOfMainMethod = new InstructionList();
InstructionHandle invokeHandler = instructionListOfMainMethod.append(
instructionFactory.createInvoke(className, "test",
Type.VOID, Type.NO_ARGS, Const.INVOKESTATIC));
instructionListOfMainMethod.append(InstructionFactory.createReturn(Type.VOID));
// System.out.println("An exception has been caught");
InstructionHandle catchBlock = instructionListOfMainMethod.append(
instructionFactory.createFieldAccess("java.lang.System", "out",
new ObjectType("java.io.PrintStream"), Const.GETSTATIC));
instructionListOfMainMethod.append(InstructionConst.DUP);
instructionListOfMainMethod.append(new PUSH(classGenerator.getConstantPool(), "An exception has been caught"));
instructionListOfMainMethod.append(
instructionFactory.createInvoke("java.io.PrintStream", "println",
Type.VOID, new Type[]{Type.STRING}, Const.INVOKEVIRTUAL));
instructionListOfMainMethod.append(InstructionFactory.createReturn(Type.VOID));
// public static void main(String[]args)
MethodGen mainMethodGenerator = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC,
Type.VOID, new Type[]{new ArrayType(Type.STRING, 1)}, new String[]{"args"},
"main", className,
instructionListOfMainMethod, classGenerator.getConstantPool());
// catch(Object e)
mainMethodGenerator.addExceptionHandler(invokeHandler, invokeHandler.getNext(), catchBlock, ObjectType.getInstance("java/lang/Object"));
classGenerator.addMethod(mainMethodGenerator.getMethod());
// throw new Object()
InstructionList instructionListOfTestMethod = new InstructionList();
instructionListOfTestMethod.append(instructionFactory.createNew("java/lang/Object"));
instructionListOfTestMethod.append(InstructionConst.DUP);
instructionListOfTestMethod.append(
instructionFactory.createInvoke("java/lang/Object", "<init>",
Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
instructionListOfTestMethod.append(InstructionConst.ATHROW);
instructionListOfTestMethod.append(InstructionFactory.createReturn(Type.VOID));
// public static void test() throws Object
MethodGen testMethodGenerator = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC,
Type.VOID, Type.NO_ARGS, new String[0],
"test", className,
instructionListOfTestMethod, classGenerator.getConstantPool());
testMethodGenerator.addException("java/lang/Object");
testMethodGenerator.stripAttributes(true);
testMethodGenerator.setMaxStack();
testMethodGenerator.setMaxLocals();
classGenerator.addMethod(testMethodGenerator.getMethod());
String outputPath = TestExample.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "ru/izebit/Test.class";
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
classGenerator.getJavaClass().dump(fos);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment