Search in sources :

Example 31 with Bytecode

use of javassist.bytecode.Bytecode in project fakereplace by fakereplace.

the class ResteasyTransformer method transform.

@Override
public boolean transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode {
    try {
        if (file.getName().equals(ResteasyExtension.FILTER_DISPATCHER)) {
            FieldInfo field = new FieldInfo(file.getConstPool(), FIELD_NAME, FILTER_FIELD_TYPE);
            field.setAccessFlags(Modifier.PUBLIC);
            file.addField(field);
            field = new FieldInfo(file.getConstPool(), PARAMETER_FIELD_NAME, SET_TYPE);
            field.setAccessFlags(Modifier.PUBLIC);
            file.addField(field);
            for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
                if (method.getName().equals("init") && method.getDescriptor().equals("(Ljavax/servlet/FilterConfig;)V")) {
                    method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
                    modifiedMethods.add(method);
                    final Bytecode b = new Bytecode(file.getConstPool());
                    b.addAload(0);
                    b.addNew(RESTEASY_FILTER_CONFIG);
                    b.add(Opcode.DUP);
                    b.addAload(1);
                    b.addInvokespecial(RESTEASY_FILTER_CONFIG, "<init>", "(Ljavax/servlet/FilterConfig;)V");
                    b.addPutfield(ResteasyExtension.FILTER_DISPATCHER, FIELD_NAME, FILTER_FIELD_TYPE);
                    b.addAload(1);
                    b.addInvokeinterface("javax/servlet/FilterConfig", "getServletContext", "()Ljavax/servlet/ServletContext;", 1);
                    b.addAload(0);
                    b.addGetfield(ResteasyExtension.FILTER_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
                    b.addInvokestatic(CONTEXT_PARAMS, "init", INIT_METHOD_DESC);
                    b.addAload(0);
                    b.add(Opcode.SWAP);
                    b.addPutfield(ResteasyExtension.FILTER_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
                    method.getCodeAttribute().iterator().insert(b.get());
                    method.getCodeAttribute().computeMaxStack();
                } else if (method.getName().equals("<init>")) {
                    // no idea why this is needed
                    method.getCodeAttribute().setMaxStack(1);
                } else if (method.getName().equals("getDispatcher")) {
                    method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
                    modifiedMethods.add(method);
                }
            }
            return true;
        } else if (file.getName().equals(ResteasyExtension.SERVLET_DISPATCHER)) {
            FieldInfo field = new FieldInfo(file.getConstPool(), FIELD_NAME, SERVLET_FIELD_TYPE);
            field.setAccessFlags(Modifier.PUBLIC);
            file.addField(field);
            field = new FieldInfo(file.getConstPool(), PARAMETER_FIELD_NAME, SET_TYPE);
            field.setAccessFlags(Modifier.PUBLIC);
            file.addField(field);
            for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
                if (method.getName().equals("init") && method.getDescriptor().equals("(Ljavax/servlet/ServletConfig;)V")) {
                    method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
                    modifiedMethods.add(method);
                    final Bytecode b = new Bytecode(file.getConstPool());
                    b.addAload(0);
                    b.addNew(RESTEASY_SERVLET_CONFIG);
                    b.add(Opcode.DUP);
                    b.addAload(1);
                    b.addInvokespecial(RESTEASY_SERVLET_CONFIG, "<init>", "(Ljavax/servlet/ServletConfig;)V");
                    b.addPutfield(ResteasyExtension.SERVLET_DISPATCHER, FIELD_NAME, SERVLET_FIELD_TYPE);
                    b.addAload(1);
                    b.addInvokeinterface("javax/servlet/ServletConfig", "getServletContext", "()Ljavax/servlet/ServletContext;", 1);
                    b.addAload(0);
                    b.addGetfield(ResteasyExtension.SERVLET_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
                    b.addInvokestatic(CONTEXT_PARAMS, "init", INIT_METHOD_DESC);
                    b.addAload(0);
                    b.add(Opcode.SWAP);
                    b.addPutfield(ResteasyExtension.SERVLET_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
                    method.getCodeAttribute().iterator().insert(b.get());
                    method.getCodeAttribute().computeMaxStack();
                } else if (method.getName().equals("<init>")) {
                    method.getCodeAttribute().setMaxStack(1);
                } else if (method.getName().equals("getDispatcher")) {
                    method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
                    modifiedMethods.add(method);
                }
            }
            return true;
        }
    } catch (DuplicateMemberException e) {
        throw new RuntimeException(e);
    }
    return false;
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) MethodInfo(javassist.bytecode.MethodInfo) List(java.util.List) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) FieldInfo(javassist.bytecode.FieldInfo)

Example 32 with Bytecode

use of javassist.bytecode.Bytecode in project fakereplace by fakereplace.

the class WildflyHibernate5ClassTransformer method transform.

@Override
public boolean transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
    if (file.getName().equals("org.jboss.as.jpa.service.PersistenceUnitServiceImpl")) {
        for (MethodInfo method : (List<MethodInfo>) file.getMethods()) {
            if (method.getName().equals("getEntityManagerFactory")) {
                modifiedMethods.add(method);
                // need to save the method params so we can re-use them when we re-create our EMF
                Bytecode s = new Bytecode(file.getConstPool());
                // we need to interceptor the return value
                // and add in our own bytecode fragment.
                // first lets create our proxy creation code
                final Bytecode b = new Bytecode(file.getConstPool());
                b.addNew(PROXY_NAME);
                b.add(Opcode.DUP);
                b.addAload(0);
                b.addInvokespecial(PROXY_NAME, "<init>", "(Lorg/jipijapa/plugin/spi/PersistenceUnitService;)V");
                insertBeforeReturn(method, s, b);
            }
        }
        file.addInterface(HackPersistenceUnitService.class.getName());
        Bytecode bc = new Bytecode(file.getConstPool(), 1, 1);
        bc.addAload(0);
        bc.addGetfield("org.jboss.as.jpa.service.PersistenceUnitServiceImpl", "entityManagerFactory", "Ljavax/persistence/EntityManagerFactory;");
        bc.addOpcode(Bytecode.ARETURN);
        MethodInfo methodInfo = new MethodInfo(file.getConstPool(), "emf", "()Ljavax/persistence/EntityManagerFactory;");
        methodInfo.setAccessFlags(AccessFlag.PUBLIC);
        methodInfo.setCodeAttribute(bc.toCodeAttribute());
        file.addMethod(methodInfo);
        modifiedMethods.add(methodInfo);
        return true;
    } else {
        return false;
    }
}
Also used : MethodInfo(javassist.bytecode.MethodInfo) List(java.util.List) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 33 with Bytecode

use of javassist.bytecode.Bytecode in project OpenTripPlanner by opentripplanner.

the class ClassCustomizer method addDoubleGetter.

/**
 * Add a simple getter with signature "double getFoo()" to the class, which simply returns the value of the
 * field fieldName
 * @param ctClass
 * @param classFile
 * @param fieldName
 * @throws DuplicateMemberException
 */
private void addDoubleGetter(ClassFile classFile, String fieldName) throws DuplicateMemberException {
    ConstPool constPool = classFile.getConstPool();
    // double getFoo()
    MethodInfo getter = new MethodInfo(constPool, "get" + ucfirst(fieldName), "()D");
    Bytecode code = new Bytecode(constPool, 2, 1);
    // load this
    code.addAload(0);
    code.addGetfield(ctClass, fieldName, "D");
    // return with value
    code.addOpcode(Opcode.DRETURN);
    getter.setCodeAttribute(code.toCodeAttribute());
    getter.setAccessFlags(AccessFlag.PUBLIC);
    classFile.addMethod(getter);
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode)

Example 34 with Bytecode

use of javassist.bytecode.Bytecode in project OpenTripPlanner by opentripplanner.

the class ClassCustomizer method addDoubleSetter.

private void addDoubleSetter(ClassFile classFile, String fieldName) throws DuplicateMemberException {
    ConstPool constPool = classFile.getConstPool();
    // void setFoo(double)
    MethodInfo setter = new MethodInfo(constPool, "set" + ucfirst(fieldName), "(D)V");
    Bytecode code = new Bytecode(constPool, 3, 3);
    // load this
    code.addAload(0);
    // load param
    code.addDload(1);
    code.addPutfield(ctClass, fieldName, "D");
    code.addOpcode(Opcode.RETURN);
    setter.setCodeAttribute(code.toCodeAttribute());
    setter.setAccessFlags(AccessFlag.PUBLIC);
    classFile.addMethod(setter);
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode)

Example 35 with Bytecode

use of javassist.bytecode.Bytecode in project tutorials by eugenp.

the class JavasisstUnitTest method givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor.

@Test
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
    // given
    ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
    Bytecode code = new Bytecode(cf.getConstPool());
    code.addAload(0);
    code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
    code.addReturn(null);
    // when
    MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
    minfo.setCodeAttribute(code.toCodeAttribute());
    cf.addMethod(minfo);
    // then
    CodeIterator ci = code.toCodeAttribute().iterator();
    List<String> operations = new LinkedList<>();
    while (ci.hasNext()) {
        int index = ci.next();
        int op = ci.byteAt(index);
        operations.add(Mnemonic.OPCODE[op]);
    }
    assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return"));
}
Also used : ClassFile(javassist.bytecode.ClassFile) CodeIterator(javassist.bytecode.CodeIterator) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) MethodInfo(javassist.bytecode.MethodInfo) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Bytecode (javassist.bytecode.Bytecode)36 MethodInfo (javassist.bytecode.MethodInfo)28 BadBytecode (javassist.bytecode.BadBytecode)24 CodeIterator (javassist.bytecode.CodeIterator)15 CodeAttribute (javassist.bytecode.CodeAttribute)11 ConstPool (javassist.bytecode.ConstPool)11 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)8 IOException (java.io.IOException)6 List (java.util.List)6 ClassFile (javassist.bytecode.ClassFile)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataOutputStream (java.io.DataOutputStream)4 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 NotFoundException (javassist.NotFoundException)4 JumpMarker (org.fakereplace.util.JumpMarker)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 CannotCompileException (javassist.CannotCompileException)3