com.sun.org.apache.bcel.internal.generic.ClassGen

Here are the examples of the java api class com.sun.org.apache.bcel.internal.generic.ClassGen taken from open source projects.

1. Bug8003147Test#test()

Project: openjdk
Source File: Bug8003147Test.java
View license
@Test
public void test() throws Exception {
    String classfile = getClass().getResource("Bug8003147Test.class").getPath();
    JavaClass jc = new ClassParser(classfile).parse();
    // rename class
    ConstantPool cp = jc.getConstantPool();
    int cpIndex = ((ConstantClass) cp.getConstant(jc.getClassNameIndex())).getNameIndex();
    cp.setConstant(cpIndex, new ConstantUtf8("Bug8003147TestPrime"));
    ClassGen gen = new ClassGen(jc);
    Method[] methods = jc.getMethods();
    int index;
    for (index = 0; index < methods.length; index++) {
        if (methods[index].getName().equals("doSomething")) {
            break;
        }
    }
    Method m = methods[index];
    MethodGen mg = new MethodGen(m, gen.getClassName(), gen.getConstantPool());
    gen.replaceMethod(m, mg.getMethod());
    String path = classfile.replace("Bug8003147Test", "Bug8003147TestPrime");
    gen.getJavaClass().dump(new FileOutputStream(path));
    try {
        Class.forName("Bug8003147TestPrime");
    } catch (ClassFormatError cfe) {
        cfe.printStackTrace();
        Assert.fail("modified version of class does not pass verification");
    }
}