private <T> T createClientProxy(BeanInstance beanInstance, String contextId) { ProxyFactory<T> factory = new ProxyFactory<>(contextId, getBeanClass(), getTypes(), this); return factory.create(beanInstance); }
private Class<T> createProxyClass(String proxyClassName) throws Exception { Set<Class<?>> specialInterfaces = Sets.newHashSet( LifecycleMixin.class, TargetInstanceProxy.class, ProxyObject.class); addAdditionalInterfaces(specialInterfaces); if (getBeanType().isInterface()) { proxyClassType = newClassFile(proxyClassName, accessFlags, Object.class.getName()); proxyClassType.addInterface(getBeanType().getName()); } else { proxyClassType = newClassFile(proxyClassName, accessFlags, getBeanType().getName()); proxyClassType.addInterface(clazz.getName()); ClassMethod staticConstructor = proxyClassType.addMethod(AccessFlag.of(AccessFlag.PUBLIC, AccessFlag.STATIC), "<clinit>", "V"); addFields(proxyClassType, initialValueBytecode); addConstructors(proxyClassType, initialValueBytecode); addMethods(proxyClassType, staticConstructor); staticConstructor.getCodeAttribute().returnInstruction(); dumpToFile(proxyClassName, proxyClassType.toBytecode()); ProtectionDomain domain = AccessController.doPrivileged(new GetProtectionDomainAction(proxiedBeanType)); domain = cache.getProtectionDomainForProxy(domain);
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 } } } }
private T createProxy(InjectionPoint injectionPoint, CreationalContext<?> creationalContext) { final T proxy = (System.getSecurityManager() == null) ? run() : AccessController.doPrivileged(this); TargetBeanInstance newTargetBeanInstance = new TargetBeanInstance(targetBeanInstance); Decorator<Object> decorator = Reflections.cast(decorators.get(counter++)); DecoratorProxyMethodHandler methodHandler = createMethodHandler(injectionPoint, creationalContext, decorator); newTargetBeanInstance.setInterceptorsHandler(methodHandler); ProxyFactory.setBeanInstance(beanManager.getContextId(), proxy, newTargetBeanInstance, bean); return proxy; }
if (getBeanType().isInterface()) { ConstructorUtils.addDefaultConstructor(proxyClassType, initialValueBytecode, !useConstructedFlag()); } else { boolean constructorFound = false; for (Constructor<?> constructor : AccessController.doPrivileged(new GetDeclaredConstructorsAction(getBeanType()))) { if ((constructor.getModifiers() & Modifier.PRIVATE) == 0) { constructorFound = true; exceptions[i] = constructor.getExceptionTypes()[i].getName(); ConstructorUtils.addConstructor(BytecodeUtils.VOID_CLASS_DESCRIPTOR, DescriptorUtils.parameterDescriptors(constructor.getParameterTypes()), exceptions, proxyClassType, initialValueBytecode, !useConstructedFlag()); addConstructorsForBeanWithPrivateConstructors(proxyClassType); throw new WeldException(e);
@Override public T newInstance(CreationalContext<T> ctx, BeanManagerImpl manager) { try { T instance = AccessController.doPrivileged(NewInstanceAction.of(proxyClass)); if (!bean.getScope().equals(Dependent.class)) { ctx.push(instance); } ProxyFactory.setBeanInstance(bean.getBeanManager().getContextId(), instance, createEnterpriseTargetBeanInstance(), bean); return instance; } catch (PrivilegedActionException e) { if (e.getCause() instanceof InstantiationException) { throw new WeldException(BeanLogger.LOG.proxyInstantiationFailed(this), e.getCause()); } else if (e.getCause() instanceof IllegalAccessException) { throw new WeldException(BeanLogger.LOG.proxyInstantiationBeanAccessFailed(this), e.getCause()); } else { throw new WeldException(e.getCause()); } } catch (Exception e) { throw BeanLogger.LOG.sessionBeanProxyInstantiationFailed(bean, proxyClass, e); } }
protected void addMethodsFromClass(ClassFile proxyClassType, ClassMethod staticConstructor) { try { Class<?> cls = getBeanType(); generateEqualsMethod(proxyClassType); generateHashCodeMethod(proxyClassType); addMethods(cls, proxyClassType, staticConstructor); if (isBeanClassAbstract && Modifier.isAbstract(cls.getModifiers())) { for (Class<?> implementedInterface : Reflections.getInterfaceClosure(cls)) { if (!additionalInterfaces.contains(implementedInterface)) { addMethods(implementedInterface, proxyClassType, staticConstructor); 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) { throw new WeldException(e);
this.contextId = contextId; this.proxiedBeanType = proxiedBeanType; this.configuration = Container.instance(contextId).deploymentManager().getServices().get(WeldConfiguration.class); addInterfacesFromTypeClosure(typeClosure, proxiedBeanType); TypeInfo typeInfo = TypeInfo.of(typeClosure); Class<?> superClass = typeInfo.getSuperClass(); addDefaultAdditionalInterfaces(); baseProxyName = proxyName; if (bean != null) { this.classLoader = resolveClassLoaderForBeanProxy(contextId, bean.getBeanClass(), typeInfo); } else { this.classLoader = resolveClassLoaderForBeanProxy(contextId, proxiedBeanType, typeInfo);
protected AbstractEEBean(Class<T> type, Callable<T> callable, BeanManagerImpl beanManager) { super(beanManager, type); this.proxy = new ProxyFactory<T>(beanManager.getContextId(), type, getTypes(), this).create(new EnterpriseTargetBeanInstance(type, new CallableMethodHandler(callable))); }
@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); } }
@Override public void initializeAfterBeanDiscovery() { this.decorators = beanManager.resolveDecorators(getTypes(), getQualifiers()); if (!decorators.isEmpty()) { this.proxyClass = new ProxyFactory<T>(getBeanManager().getContextId(), getType(), getTypes(), this).getProxyClass(); } } }
@Override public void internalInitialize(BeanDeployerEnvironment environment) { super.internalInitialize(environment); checkEEResource(); proxyFactory = new ProxyFactory<T>(getBeanManager().getContextId(), getType(), getTypes(), this); }
protected void addMethods(ClassFile proxyClassType, ClassMethod staticConstructor) { // Add all class methods for interception addMethodsFromClass(proxyClassType, staticConstructor); // Add special proxy methods addSpecialMethods(proxyClassType, staticConstructor); // Add serialization support methods addSerializationSupport(proxyClassType); }
protected void addConstructedGuardToMethodBody(final ClassMethod classMethod) { addConstructedGuardToMethodBody(classMethod, classMethod.getClassFile().getSuperclass()); }
@Override public T create(CreationalContext<T> creationalContext) { final T beanInstance = getProducer().produce(creationalContext); if (Reflections.isFinal(rawType) || Serializable.class.isAssignableFrom(beanInstance.getClass())) { return checkReturnValue(beanInstance); } else { BeanInstance proxyBeanInstance = new EnterpriseTargetBeanInstance(getTypes(), new CallableMethodHandler(new EEResourceCallable<T>(getBeanManager(), this, creationalContext, beanInstance))); return checkReturnValue(proxyFactory.create(proxyBeanInstance)); } }
@Override public void initializeAfterBeanDiscovery() { this.proxyClass = new ProxyFactory<T>(getBeanManager().getContextId(), getType(), getTypes(), this).getProxyClass(); }
public AbstractDecoratorApplyingInstantiator(String contextId, Instantiator<T> delegate, Bean<T> bean, List<Decorator<?>> decorators, Class<? extends T> implementationClass) { super(delegate); this.bean = bean; this.decorators = decorators; ProxyFactory<T> factory = new ProxyFactory<T>(contextId, implementationClass, bean.getTypes(), bean, true); // eagerly generate the proxy class this.proxyClass = factory.getProxyClass(); }
@Override protected boolean isMethodAccepted(Method method, Class<?> proxySuperclass) { return super.isMethodAccepted(method, proxySuperclass) && CommonProxiedMethodFilters.NON_PRIVATE.accept(method, proxySuperclass); } }
@Override protected T newInstance(Object[] parameterValues) { // Once the instance is created, a method handler is required regardless of whether // an actual bean instance is known yet. final T instance = super.newInstance(parameterValues); if (decorator) { BeanInstance beanInstance = null; if (hasDelegateInjectionPoint()) { Object decoratorDelegate = parameterValues[delegateInjectionPointPosition]; beanInstance = new TargetBeanInstance(decoratorDelegate); } ProxyFactory.setBeanInstance(contextId, instance, beanInstance, bean); } else { if (instance instanceof ProxyObject) { ((ProxyObject) instance).weld_setHandler(new CombinedInterceptorAndDecoratorStackMethodHandler()); // Set method handler for private methods if necessary InterceptedSubclassFactory.setPrivateMethodHandler(instance); } } return instance; }
@Override public T create(BeanInstance beanInstance) { try { final T instance = super.create(beanInstance); if (beanIdField == null) { final Field f = SecurityActions.getDeclaredField(instance.getClass(), BEAN_ID_FIELD); SecurityActions.ensureAccessible(f); beanIdField = f; } if (contextIdField == null) { final Field f = SecurityActions.getDeclaredField(instance.getClass(), CONTEXT_ID_FIELD); SecurityActions.ensureAccessible(f); contextIdField = f; } beanIdField.set(instance, beanId); contextIdField.set(instance, getContextId()); return instance; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { throw new RuntimeException(e.getCause()); } }