Search in sources :

Example 1 with VirtualToStaticManipulator

use of org.fakereplace.manip.VirtualToStaticManipulator in project fakereplace by fakereplace.

the class WeldClassTransformer 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 {
    // Hack up the proxy factory so it stores the proxy ClassFile. We need this to regenerate proxies.
    if (file.getName().equals(ORG_JBOSS_WELD_BEAN_PROXY_PROXY_FACTORY)) {
        for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
            if (method.getName().equals("createProxyClass")) {
                modifiedMethods.add(method);
                final VirtualToStaticManipulator virtualToStaticManipulator = new VirtualToStaticManipulator();
                virtualToStaticManipulator.replaceVirtualMethodInvokationWithStatic(ClassLoader.class.getName(), WELD_PROXY_CLASS_LOADING_DELEGATE, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", loader);
                virtualToStaticManipulator.replaceVirtualMethodInvokationWithStatic("org.jboss.weld.util.bytecode.ClassFileUtils", WELD_PROXY_CLASS_LOADING_DELEGATE, "toClass", "(Lorg/jboss/classfilewriter/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", "(Lorg/jboss/classfilewriter/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", loader);
                virtualToStaticManipulator.transformClass(file, loader, true, modifiedMethods);
                return true;
            } else if (method.getName().equals("<init>")) {
                modifiedMethods.add(method);
                Integer beanArgument = null;
                int count = 1;
                for (final String paramType : DescriptorUtils.descriptorStringToParameterArray(method.getDescriptor())) {
                    if (paramType.equals("Ljavax/enterprise/inject/spi/Bean")) {
                        beanArgument = count;
                        break;
                    } else if (paramType.equals("D") || paramType.equals("J")) {
                        count += 2;
                    } else {
                        count++;
                    }
                }
                if (beanArgument == null) {
                    log.error("Constructor org.jboss.weld.bean.proxy.ProxyFactory.<init>" + method.getDescriptor() + " does not have a bean parameter, proxies produced by this factory will not be reloadable");
                    continue;
                }
                // similar to other tracked instances
                // but we need a strong ref
                Bytecode code = new Bytecode(file.getConstPool());
                code.addAload(0);
                code.addAload(beanArgument);
                code.addInvokestatic(WeldClassChangeAware.class.getName(), "addProxyFactory", "(Ljava/lang/Object;Ljava/lang/Object;)V");
                CodeIterator it = method.getCodeAttribute().iterator();
                it.skipConstructor();
                it.insert(code.get());
            }
        }
    }
    return false;
}
Also used : VirtualToStaticManipulator(org.fakereplace.manip.VirtualToStaticManipulator) CodeIterator(javassist.bytecode.CodeIterator) MethodInfo(javassist.bytecode.MethodInfo) List(java.util.List) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Aggregations

List (java.util.List)1 BadBytecode (javassist.bytecode.BadBytecode)1 Bytecode (javassist.bytecode.Bytecode)1 CodeIterator (javassist.bytecode.CodeIterator)1 MethodInfo (javassist.bytecode.MethodInfo)1 VirtualToStaticManipulator (org.fakereplace.manip.VirtualToStaticManipulator)1