Search in sources :

Example 1 with MethodInformation

use of org.jboss.weld.util.bytecode.MethodInformation in project core by weld.

the class EnterpriseProxyFactory method addSpecialMethods.

@Override
protected void addSpecialMethods(ClassFile proxyClassType, ClassMethod staticConstructor) {
    super.addSpecialMethods(proxyClassType, staticConstructor);
    // Add methods for the EnterpriseBeanInstance interface
    try {
        proxyClassType.addInterface(EnterpriseBeanInstance.class.getName());
        for (Method method : EnterpriseBeanInstance.class.getMethods()) {
            BeanLogger.LOG.addingMethodToEnterpriseProxy(method);
            MethodInformation methodInfo = new RuntimeMethodInformation(method);
            createInterceptorBody(proxyClassType.addMethod(method), methodInfo, staticConstructor);
        }
    } catch (Exception e) {
        throw new WeldException(e);
    }
}
Also used : WeldException(org.jboss.weld.exceptions.WeldException) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) MethodInformation(org.jboss.weld.util.bytecode.MethodInformation) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) ClassMethod(org.jboss.classfilewriter.ClassMethod) Method(java.lang.reflect.Method) WeldException(org.jboss.weld.exceptions.WeldException)

Example 2 with MethodInformation

use of org.jboss.weld.util.bytecode.MethodInformation in project core by weld.

the class ProxyFactory method addMethodsFromClass.

protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticConstructor) {
    try {
        // Add all methods from the class hierarchy
        Class<?> cls = getBeanType();
        // First add equals/hashCode methods if required
        generateEqualsMethod(proxyClassType);
        generateHashCodeMethod(proxyClassType);
        // In rare cases, the bean class may be abstract - in this case we have to add methods from all interfaces implemented by any abstract class
        // from the hierarchy
        boolean isBeanClassAbstract = Modifier.isAbstract(cls.getModifiers());
        while (cls != null) {
            addMethods(cls, proxyClassType, staticConstructor);
            if (isBeanClassAbstract && Modifier.isAbstract(cls.getModifiers())) {
                for (Class<?> implementedInterface : Reflections.getInterfaceClosure(cls)) {
                    if (!additionalInterfaces.contains(implementedInterface)) {
                        addMethods(implementedInterface, proxyClassType, staticConstructor);
                    }
                }
            }
            cls = cls.getSuperclass();
        }
        for (Class<?> c : additionalInterfaces) {
            for (Method method : c.getMethods()) {
                if (isMethodAccepted(method, getProxySuperclass())) {
                    try {
                        MethodInformation methodInfo = new RuntimeMethodInformation(method);
                        ClassMethod classMethod = proxyClassType.addMethod(method);
                        if (Reflections.isDefault(method)) {
                            addConstructedGuardToMethodBody(classMethod);
                            createForwardingMethodBody(classMethod, methodInfo, staticConstructor);
                        } else {
                            createSpecialMethodBody(classMethod, methodInfo, staticConstructor);
                        }
                        BeanLogger.LOG.addingMethodToProxy(method);
                    } catch (DuplicateMemberException e) {
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new WeldException(e);
    }
}
Also used : DuplicateMemberException(org.jboss.classfilewriter.DuplicateMemberException) WeldException(org.jboss.weld.exceptions.WeldException) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) MethodInformation(org.jboss.weld.util.bytecode.MethodInformation) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) ClassMethod(org.jboss.classfilewriter.ClassMethod) Method(java.lang.reflect.Method) ClassMethod(org.jboss.classfilewriter.ClassMethod) DuplicateMemberException(org.jboss.classfilewriter.DuplicateMemberException) DefinitionException(org.jboss.weld.exceptions.DefinitionException) WeldException(org.jboss.weld.exceptions.WeldException) IOException(java.io.IOException)

Example 3 with MethodInformation

use of org.jboss.weld.util.bytecode.MethodInformation in project core by weld.

the class ProxyFactory method addMethods.

private void addMethods(Class<?> cls, ClassFile proxyClassType, ClassMethod staticConstructor) {
    for (Method method : AccessController.doPrivileged(new GetDeclaredMethodsAction(cls))) {
        if (isMethodAccepted(method, getProxySuperclass())) {
            try {
                MethodInformation methodInfo = new RuntimeMethodInformation(method);
                ClassMethod classMethod = proxyClassType.addMethod(method);
                addConstructedGuardToMethodBody(classMethod);
                createForwardingMethodBody(classMethod, methodInfo, staticConstructor);
                BeanLogger.LOG.addingMethodToProxy(method);
            } catch (DuplicateMemberException e) {
            // do nothing. This will happen if superclass methods
            // have been overridden
            }
        }
    }
}
Also used : DuplicateMemberException(org.jboss.classfilewriter.DuplicateMemberException) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) MethodInformation(org.jboss.weld.util.bytecode.MethodInformation) GetDeclaredMethodsAction(org.jboss.weld.security.GetDeclaredMethodsAction) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) ClassMethod(org.jboss.classfilewriter.ClassMethod) Method(java.lang.reflect.Method) ClassMethod(org.jboss.classfilewriter.ClassMethod)

Example 4 with MethodInformation

use of org.jboss.weld.util.bytecode.MethodInformation in project core by weld.

the class InterceptedSubclassFactory method addMethodsFromClass.

@Override
protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticConstructor) {
    try {
        final Set<MethodSignature> finalMethods = new HashSet<MethodSignature>();
        final Set<BridgeMethod> processedBridgeMethods = new HashSet<BridgeMethod>();
        // Add all methods from the class hierarchy
        Class<?> cls = getBeanType();
        while (cls != null) {
            Set<BridgeMethod> declaredBridgeMethods = new HashSet<BridgeMethod>();
            for (Method method : AccessController.doPrivileged(new GetDeclaredMethodsAction(cls))) {
                final MethodSignatureImpl methodSignature = new MethodSignatureImpl(method);
                if (!Modifier.isFinal(method.getModifiers()) && !method.isBridge() && enhancedMethodSignatures.contains(methodSignature) && !finalMethods.contains(methodSignature) && !bridgeMethodsContainsMethod(processedBridgeMethods, methodSignature, method.getGenericReturnType(), Modifier.isAbstract(method.getModifiers()))) {
                    try {
                        final MethodInformation methodInfo = new RuntimeMethodInformation(method);
                        if (interceptedMethodSignatures.contains(methodSignature)) {
                            // create delegate-to-super method
                            createDelegateMethod(proxyClassType, method, methodInfo);
                            // this method is intercepted
                            // override a subclass method to delegate to method handler
                            ClassMethod classMethod = proxyClassType.addMethod(method);
                            addConstructedGuardToMethodBody(classMethod);
                            createForwardingMethodBody(classMethod, methodInfo, staticConstructor);
                            BeanLogger.LOG.addingMethodToProxy(method);
                        } else {
                            // this method is not intercepted
                            // we still need to override and push InterceptionDecorationContext stack to prevent full interception
                            ClassMethod classMethod = proxyClassType.addMethod(method);
                            new RunWithinInterceptionDecorationContextGenerator(classMethod, this) {

                                @Override
                                void doWork(CodeAttribute b, ClassMethod classMethod) {
                                    if (Modifier.isPrivate(classMethod.getAccessFlags())) {
                                        // Weld cannot use invokespecial to invoke a private method from the superclass
                                        invokePrivateMethodHandler(b, classMethod, methodInfo, staticConstructor);
                                    } else {
                                        // build the bytecode that invokes the super class method directly
                                        b.aload(0);
                                        // create the method invocation
                                        b.loadMethodParameters();
                                        b.invokespecial(methodInfo.getDeclaringClass(), methodInfo.getName(), methodInfo.getDescriptor());
                                    }
                                // leave the result on top of the stack
                                }

                                @Override
                                void doReturn(CodeAttribute b, ClassMethod method) {
                                    // assumes doWork() result is on top of the stack
                                    b.returnInstruction();
                                }
                            }.runStartIfNotOnTop();
                        }
                    } catch (DuplicateMemberException e) {
                    // do nothing. This will happen if superclass methods have
                    // been overridden
                    }
                } else {
                    if (Modifier.isFinal(method.getModifiers())) {
                        finalMethods.add(methodSignature);
                    }
                    if (method.isBridge()) {
                        declaredBridgeMethods.add(new BridgeMethod(methodSignature, method.getGenericReturnType()));
                    }
                }
            }
            processedBridgeMethods.addAll(declaredBridgeMethods);
            cls = cls.getSuperclass();
        }
        for (Class<?> c : getAdditionalInterfaces()) {
            for (Method method : c.getMethods()) {
                MethodSignature signature = new MethodSignatureImpl(method);
                // For interfaces we do not consider return types when going through processed bridge methods
                if (enhancedMethodSignatures.contains(signature) && !bridgeMethodsContainsMethod(processedBridgeMethods, signature, null, Modifier.isAbstract(method.getModifiers()))) {
                    try {
                        MethodInformation methodInfo = new RuntimeMethodInformation(method);
                        if (interceptedMethodSignatures.contains(signature) && Reflections.isDefault(method)) {
                            createDelegateMethod(proxyClassType, method, methodInfo);
                            // this method is intercepted
                            // override a subclass method to delegate to method handler
                            ClassMethod classMethod = proxyClassType.addMethod(method);
                            addConstructedGuardToMethodBody(classMethod);
                            createForwardingMethodBody(classMethod, methodInfo, staticConstructor);
                            BeanLogger.LOG.addingMethodToProxy(method);
                        } else {
                            if (Reflections.isDefault(method)) {
                                createDelegateMethod(proxyClassType, method, methodInfo);
                            } else {
                                final ClassMethod classMethod = proxyClassType.addMethod(method);
                                createSpecialMethodBody(classMethod, methodInfo, staticConstructor);
                                BeanLogger.LOG.addingMethodToProxy(method);
                            }
                        }
                    } catch (DuplicateMemberException e) {
                    }
                }
                if (method.isBridge()) {
                    processedBridgeMethods.add(new BridgeMethod(signature, method.getGenericReturnType()));
                }
            }
        }
    } catch (Exception e) {
        throw new WeldException(e);
    }
}
Also used : DuplicateMemberException(org.jboss.classfilewriter.DuplicateMemberException) WeldException(org.jboss.weld.exceptions.WeldException) MethodSignature(org.jboss.weld.annotated.enhanced.MethodSignature) ClassMethod(org.jboss.classfilewriter.ClassMethod) Method(java.lang.reflect.Method) WeldException(org.jboss.weld.exceptions.WeldException) DuplicateMemberException(org.jboss.classfilewriter.DuplicateMemberException) MethodSignatureImpl(org.jboss.weld.annotated.enhanced.jlr.MethodSignatureImpl) MethodInformation(org.jboss.weld.util.bytecode.MethodInformation) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) CodeAttribute(org.jboss.classfilewriter.code.CodeAttribute) GetDeclaredMethodsAction(org.jboss.weld.security.GetDeclaredMethodsAction) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) ClassMethod(org.jboss.classfilewriter.ClassMethod) HashSet(java.util.HashSet)

Example 5 with MethodInformation

use of org.jboss.weld.util.bytecode.MethodInformation in project core by weld.

the class DecoratorProxyFactory method addMethodsFromClass.

@Override
protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticConstructor) {
    Method initializerMethod = null;
    int delegateParameterPosition = -1;
    if (delegateInjectionPoint instanceof ParameterInjectionPoint<?, ?>) {
        ParameterInjectionPoint<?, ?> parameterIP = (ParameterInjectionPoint<?, ?>) delegateInjectionPoint;
        if (parameterIP.getMember() instanceof Method) {
            initializerMethod = ((Method) parameterIP.getMember());
            delegateParameterPosition = parameterIP.getAnnotated().getPosition();
        }
    }
    try {
        if (delegateParameterPosition >= 0) {
            addHandlerInitializerMethod(proxyClassType, staticConstructor);
        }
        Class<?> cls = getBeanType();
        Set<Method> methods = new LinkedHashSet<Method>();
        decoratorMethods(cls, methods);
        for (Method method : methods) {
            MethodInformation methodInfo = new RuntimeMethodInformation(method);
            if (!method.getDeclaringClass().getName().equals("java.lang.Object") || method.getName().equals("toString")) {
                if ((delegateParameterPosition >= 0) && (initializerMethod.equals(method))) {
                    createDelegateInitializerCode(proxyClassType.addMethod(method), methodInfo, delegateParameterPosition);
                }
                // exclude bridge methods
                if (Modifier.isAbstract(method.getModifiers())) {
                    createAbstractMethodCode(proxyClassType.addMethod(method), methodInfo, staticConstructor);
                }
            }
        }
    } catch (Exception e) {
        throw new WeldException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WeldException(org.jboss.weld.exceptions.WeldException) MethodInformation(org.jboss.weld.util.bytecode.MethodInformation) StaticMethodInformation(org.jboss.weld.util.bytecode.StaticMethodInformation) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) RuntimeMethodInformation(org.jboss.weld.util.bytecode.RuntimeMethodInformation) ClassMethod(org.jboss.classfilewriter.ClassMethod) Method(java.lang.reflect.Method) FieldInjectionPoint(org.jboss.weld.injection.FieldInjectionPoint) ParameterInjectionPoint(org.jboss.weld.injection.ParameterInjectionPoint) ParameterInjectionPoint(org.jboss.weld.injection.ParameterInjectionPoint) WeldException(org.jboss.weld.exceptions.WeldException)

Aggregations

Method (java.lang.reflect.Method)8 ClassMethod (org.jboss.classfilewriter.ClassMethod)8 MethodInformation (org.jboss.weld.util.bytecode.MethodInformation)8 RuntimeMethodInformation (org.jboss.weld.util.bytecode.RuntimeMethodInformation)8 WeldException (org.jboss.weld.exceptions.WeldException)7 DuplicateMemberException (org.jboss.classfilewriter.DuplicateMemberException)6 GetDeclaredMethodsAction (org.jboss.weld.security.GetDeclaredMethodsAction)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 CodeAttribute (org.jboss.classfilewriter.code.CodeAttribute)2 MethodSignature (org.jboss.weld.annotated.enhanced.MethodSignature)2 MethodSignatureImpl (org.jboss.weld.annotated.enhanced.jlr.MethodSignatureImpl)2 DefinitionException (org.jboss.weld.exceptions.DefinitionException)2 LinkedHashSet (java.util.LinkedHashSet)1 FieldInjectionPoint (org.jboss.weld.injection.FieldInjectionPoint)1 ParameterInjectionPoint (org.jboss.weld.injection.ParameterInjectionPoint)1 StaticMethodInformation (org.jboss.weld.util.bytecode.StaticMethodInformation)1