Search in sources :

Example 76 with BadBytecode

use of javassist.bytecode.BadBytecode 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, boolean replaceable) 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, replaceable);
                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)

Example 77 with BadBytecode

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

the class WildflyClassTransformer method transform.

@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods, boolean replaceable) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
    if (!file.getName().equals("org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService")) {
        return false;
    }
    for (MethodInfo method : (List<MethodInfo>) file.getMethods()) {
        if (method.getName().equals("createServletConfig")) {
            CodeAttribute code = method.getCodeAttribute();
            code.setMaxStack(code.getMaxStack() + 1);
            CodeIterator it = code.iterator();
            modifiedMethods.add(method);
            while (it.hasNext()) {
                int pos = it.next();
                int inst = it.byteAt(pos);
                if (inst == CodeAttribute.ARETURN) {
                    Bytecode b = new Bytecode(method.getConstPool());
                    b.addGetstatic("org.fakereplace.integration.wildfly.autoupdate.WebUpdateHandlerWrapper", "INSTANCE", "Lio/undertow/server/HandlerWrapper;");
                    b.addInvokevirtual("io.undertow.servlet.api.DeploymentInfo", "addInnerHandlerChainWrapper", "(Lio/undertow/server/HandlerWrapper;)Lio/undertow/servlet/api/DeploymentInfo;");
                    it.insert(pos, b.get());
                }
            }
        }
    }
    return true;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) MethodInfo(javassist.bytecode.MethodInfo) List(java.util.List) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 78 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, boolean replaceable) 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 79 with BadBytecode

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

the class JavassistUtils method getMethodUsage.

static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<Type> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
    // TODO avoid bridge and synthetic methods
    for (CtMethod method : ctClass.getDeclaredMethods()) {
        if (method.getName().equals(name)) {
            // TODO check typeParametersValues
            MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
            if (argumentsTypes.size() < methodUsage.getNoParams()) {
                // this method cannot be a good candidate (except if variadic ?)
                continue;
            }
            try {
                if (method.getGenericSignature() != null) {
                    SignatureAttribute.MethodSignature classSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
                    List<Type> parametersOfReturnType = parseTypeParameters(classSignature.getReturnType().toString(), typeSolver, invokationContext);
                    Type newReturnType = methodUsage.returnType();
                    // consume one parametersOfReturnType at the time
                    if (!(newReturnType instanceof VoidType)) {
                        newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
                    }
                    methodUsage = methodUsage.replaceReturnType(newReturnType);
                }
                return Optional.of(methodUsage);
            } catch (BadBytecode e) {
                throw new RuntimeException(e);
            }
        }
    }
    try {
        CtClass superClass = ctClass.getSuperclass();
        if (superClass != null) {
            Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
            if (ref.isPresent()) {
                return ref;
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
            if (ref.isPresent()) {
                return ref;
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    return Optional.empty();
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) BadBytecode(javassist.bytecode.BadBytecode) TypeParameterDeclaration(com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) SignatureAttribute(javassist.bytecode.SignatureAttribute) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) UnsolvedSymbolException(com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException) Context(com.github.javaparser.symbolsolver.core.resolution.Context) TypeParametrizable(com.github.javaparser.symbolsolver.model.declarations.TypeParametrizable) ReferenceTypeDeclaration(com.github.javaparser.symbolsolver.model.declarations.ReferenceTypeDeclaration) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage) CtMethod(javassist.CtMethod)

Example 80 with BadBytecode

use of javassist.bytecode.BadBytecode in project UniverseCore by EB-wilson.

the class Analyzer method mergeRet.

private void mergeRet(IntQueue queue, CodeIterator iter, int pos, Frame frame, Subroutine subroutine) throws BadBytecode {
    if (subroutine == null)
        throw new BadBytecode("Ret on no subroutine! [pos = " + pos + "]");
    for (int caller : subroutine.callers()) {
        int returnLoc = getNext(iter, caller, pos);
        boolean changed = false;
        Frame old = frames[returnLoc];
        if (old == null) {
            old = frames[returnLoc] = frame.copyStack();
            changed = true;
        } else {
            changed = old.mergeStack(frame);
        }
        for (int index : subroutine.accessed()) {
            Type oldType = old.getLocal(index);
            Type newType = frame.getLocal(index);
            if (oldType != newType) {
                old.setLocal(index, newType);
                changed = true;
            }
        }
        if (!old.isRetMerged()) {
            old.setRetMerged(true);
            changed = true;
        }
        if (changed && old.isJsrMerged())
            queue.add(returnLoc);
    }
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

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