Search in sources :

Example 61 with BadBytecode

use of javassist.bytecode.BadBytecode in project pinpoint by naver.

the class JavassistMethod method insertBeforeMethod.

private int insertBeforeMethod(int pos, String src) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");
    CodeIterator iterator = ca.iterator();
    Javac jv = new Javac(cc);
    try {
        int nvars = jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordParamNames(ca, nvars);
        jv.recordLocalVariables(ca, 0);
        jv.recordType(getReturnType0());
        jv.compileStmnt(src);
        Bytecode b = jv.getBytecode();
        int stack = b.getMaxStack();
        int locals = b.getMaxLocals();
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        if (locals > ca.getMaxLocals())
            ca.setMaxLocals(locals);
        if (pos != -1) {
            iterator.insertEx(pos, b.get());
        } else {
            pos = iterator.insertEx(b.get());
        }
        iterator.insert(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return pos + b.length();
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 62 with BadBytecode

use of javassist.bytecode.BadBytecode 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)

Example 63 with BadBytecode

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

the class ClassInfoTransformer method transform.

@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
    if (className.equals("com/sun/beans/introspect/ClassInfo")) {
        file.addInterface(Runnable.class.getName());
        MethodInfo run = new MethodInfo(file.getConstPool(), "run", "()V");
        run.setAccessFlags(AccessFlag.PUBLIC);
        Bytecode b = new Bytecode(file.getConstPool(), 1, 1);
        b.addGetstatic(file.getName(), "CACHE", "Lcom/sun/beans/util/Cache;");
        b.addInvokevirtual("com/sun/beans/util/Cache", "clear", "()V");
        b.add(Opcode.RETURN);
        run.setCodeAttribute(b.toCodeAttribute());
        file.addMethod(run);
        MethodInfo m = file.getMethod("<init>");
        CodeIterator iterator = m.getCodeAttribute().iterator();
        int pos = 0;
        while (iterator.hasNext()) {
            pos = iterator.next();
        }
        b = new Bytecode(file.getConstPool(), 1, 0);
        b.add(Bytecode.ALOAD_0);
        b.addPutstatic(ClassInfoTransformer.class.getName(), "clearAction", "Ljava/lang/Runnable;");
        iterator.insert(pos, b.get());
        m.getCodeAttribute().computeMaxStack();
        return true;
    }
    return false;
}
Also used : CodeIterator(javassist.bytecode.CodeIterator) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 64 with BadBytecode

use of javassist.bytecode.BadBytecode 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 65 with BadBytecode

use of javassist.bytecode.BadBytecode 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)

Aggregations

BadBytecode (javassist.bytecode.BadBytecode)135 Bytecode (javassist.bytecode.Bytecode)57 CodeAttribute (javassist.bytecode.CodeAttribute)56 CodeIterator (javassist.bytecode.CodeIterator)43 MethodInfo (javassist.bytecode.MethodInfo)38 CtClass (javassist.CtClass)32 NotFoundException (javassist.NotFoundException)32 ConstPool (javassist.bytecode.ConstPool)30 Javac (javassist.compiler.Javac)27 CompileError (javassist.compiler.CompileError)26 CannotCompileException (javassist.CannotCompileException)22 ClassPool (javassist.ClassPool)12 List (java.util.List)11 Type (javassist.bytecode.analysis.Type)8 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)7 IOException (java.io.IOException)6 CtMethod (javassist.CtMethod)5 ClassFile (javassist.bytecode.ClassFile)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 Method (java.lang.reflect.Method)4