@Override public final WeldException unableToGetFieldOnDeserialization(final Object param1, final Object param2, final Throwable cause) { final WeldException result = new WeldException(java.text.MessageFormat.format(unableToGetFieldOnDeserialization$str(), param1, param2), cause); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String incorrectNumberOfAnnotatedParametersMethod = "WELD-000614: Incorrect number of AnnotatedParameters {0} on AnnotatedMethod {1}. AnnotatedMethod has {2} as parameters but should have {3} as parameters";
@Override public String getLocalizedMessage() { return getMessage(); }
/** * Does not perform {@link PrivilegedAction} unless necessary. * * @param javaClass * @param methodName * @param parameterTypes * @return returns a method from the class or any class/interface in the inheritance hierarchy * @throws NoSuchMethodException */ static Method lookupMethod(Class<?> javaClass, String methodName, Class<?>[] parameterTypes) throws NoSuchMethodException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(new MethodLookupAction(javaClass, methodName, parameterTypes)); } catch (PrivilegedActionException e) { if (e.getCause() instanceof NoSuchMethodException) { throw (NoSuchMethodException) e.getCause(); } throw new WeldException(e.getCause()); } } else { return MethodLookupAction.lookupMethod(javaClass, methodName, parameterTypes); } }
/** * @param javaClass * @return a new instance of the given class * @throws InstantiationException * @throws IllegalAccessException */ static <T> T newInstance(Class<T> javaClass) throws InstantiationException, IllegalAccessException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(NewInstanceAction.of(javaClass)); } catch (PrivilegedActionException e) { throw new WeldException(e.getCause()); } } else { return javaClass.newInstance(); } }
static <T> T newConstructorInstance(Class<T> javaClass, Class<?>[] constructorParamTypes, Object... constructorParamInstances) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(ConstructorNewInstanceAction.of(javaClass, constructorParamTypes, constructorParamInstances)); } catch (PrivilegedActionException e) { throw new WeldException(e.getCause()); } } else { return javaClass.getConstructor(constructorParamTypes).newInstance(constructorParamInstances); } }
@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); } }
public Class<?> loadBeanClass(final String className) { try { return (Class<?>) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { return Class.forName(className, true, getClassLoader(this.getClass())); } }); } catch (PrivilegedActionException pae) { throw new WeldException(BeanMessage.CANNOT_LOAD_CLASS, className, pae.getException()); } }
/** * Adds methods requiring special implementations rather than just * delegation. * * @param proxyClassType the Javassist class description for the proxy type */ protected void addSpecialMethods(ClassFile proxyClassType, ClassMethod staticConstructor) { try { // Add special methods for interceptors for (Method method : LifecycleMixin.class.getMethods()) { BeanLogger.LOG.addingMethodToProxy(method); MethodInformation methodInfo = new RuntimeMethodInformation(method); createInterceptorBody(proxyClassType.addMethod(method), methodInfo, false, staticConstructor); } Method getInstanceMethod = TargetInstanceProxy.class.getMethod("weld_getTargetInstance"); Method getInstanceClassMethod = TargetInstanceProxy.class.getMethod("weld_getTargetClass"); generateGetTargetInstanceBody(proxyClassType.addMethod(getInstanceMethod)); generateGetTargetClassBody(proxyClassType.addMethod(getInstanceClassMethod)); Method setMethodHandlerMethod = ProxyObject.class.getMethod("weld_setHandler", MethodHandler.class); generateSetMethodHandlerBody(proxyClassType.addMethod(setMethodHandlerMethod)); Method getMethodHandlerMethod = ProxyObject.class.getMethod("weld_getHandler"); generateGetMethodHandlerBody(proxyClassType.addMethod(getMethodHandlerMethod)); } catch (Exception e) { throw new WeldException(e); } }
public static <T> T getOuterDelegate(Bean<T> bean, T instance, CreationalContext<T> creationalContext, Class<T> proxyClass, InjectionPoint originalInjectionPoint, BeanManagerImpl manager, List<Decorator<?>> decorators) { TargetBeanInstance beanInstance = new TargetBeanInstance(bean, instance); DecorationHelper<T> decorationHelper = new DecorationHelper<T>(beanInstance, bean, proxyClass, manager, manager.getServices().get(ContextualStore.class), decorators); DecorationHelper.push(decorationHelper); try { final T outerDelegate = decorationHelper.getNextDelegate(originalInjectionPoint, creationalContext); if (outerDelegate == null) { throw new WeldException(BeanLogger.LOG.proxyInstantiationFailed(bean)); } return outerDelegate; } finally { DecorationHelper.pop(); } }
/** * Invokes the method on a given instance passing in given parameters. If the invocation yields * {@link InvocationTargetException}, the exception is unwrapped. * * It is a responsibility of the caller to make sure that the method is accessible to the caller. */ public static <T> T invokeAndUnwrap(final Object instance, final Method method, final Object... parameters) throws Throwable { try { return cast(method.invoke(instance, parameters)); } catch (IllegalArgumentException e) { throw ReflectionLogger.LOG.illegalArgumentExceptionOnReflectionInvocation(instance.getClass(), instance, method, Arrays.toString(parameters), e); } catch (IllegalAccessException e) { throw new WeldException(e); } catch (InvocationTargetException e) { throw e.getCause(); } }
@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); } }
private static ProxyInstantiator newInstance(String implementation) throws InstantiationException, IllegalAccessException { if (DefaultProxyInstantiator.class.getName().equals(implementation)) { return DefaultProxyInstantiator.INSTANCE; } Class<? extends ProxyInstantiator> clazz = Reflections.loadClass(implementation, new ClassLoaderResourceLoader(ProxyInstantiator.class.getClassLoader())); if (clazz == null) { throw new WeldException("Unable to load ProxyInstantiator implementation: " + implementation); } return clazz.newInstance(); } }
/** * Does not perform {@link PrivilegedAction} unless necessary. * * @param javaClass * @param methodName * @param parameterTypes * @return a method from the class or any class/interface in the inheritance hierarchy * @throws NoSuchMethodException */ static Method lookupMethod(Class<?> javaClass, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(new MethodLookupAction(javaClass, methodName, parameterTypes)); } catch (PrivilegedActionException e) { if (e.getCause() instanceof NoSuchMethodException) { throw (NoSuchMethodException) e.getCause(); } throw new WeldException(e.getCause()); } } else { return MethodLookupAction.lookupMethod(javaClass, methodName, parameterTypes); } }
/** * @param javaClass * @return a new instance of the given class * @throws InstantiationException * @throws IllegalAccessException */ static <T> T newInstance(Class<T> javaClass) throws InstantiationException, IllegalAccessException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(NewInstanceAction.of(javaClass)); } catch (PrivilegedActionException e) { throw new WeldException(e.getCause()); } } else { return javaClass.newInstance(); } }
static <T> T newConstructorInstance(Class<T> javaClass, Class<?>[] constructorParamTypes, Object... constructorParamInstances) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { if (System.getSecurityManager() != null) { try { return AccessController.doPrivileged(ConstructorNewInstanceAction.of(javaClass, constructorParamTypes, constructorParamInstances)); } catch (PrivilegedActionException e) { throw new WeldException(e.getCause()); } } else { return javaClass.getConstructor(constructorParamTypes).newInstance(constructorParamInstances); } }
@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); } }
/** * Adds methods requiring special implementations rather than just * delegation. * * @param proxyClassType the Javassist class description for the proxy type */ protected void addSpecialMethods(ClassFile proxyClassType, ClassMethod staticConstructor) { try { // Add special methods for interceptors for (Method method : LifecycleMixin.class.getMethods()) { BeanLogger.LOG.addingMethodToProxy(method); MethodInformation methodInfo = new RuntimeMethodInformation(method); createInterceptorBody(proxyClassType.addMethod(method), methodInfo, false, staticConstructor); } Method getInstanceMethod = TargetInstanceProxy.class.getMethod("weld_getTargetInstance"); Method getInstanceClassMethod = TargetInstanceProxy.class.getMethod("weld_getTargetClass"); generateGetTargetInstanceBody(proxyClassType.addMethod(getInstanceMethod)); generateGetTargetClassBody(proxyClassType.addMethod(getInstanceClassMethod)); Method setMethodHandlerMethod = ProxyObject.class.getMethod("weld_setHandler", MethodHandler.class); generateSetMethodHandlerBody(proxyClassType.addMethod(setMethodHandlerMethod)); Method getMethodHandlerMethod = ProxyObject.class.getMethod("weld_getHandler"); generateGetMethodHandlerBody(proxyClassType.addMethod(getMethodHandlerMethod)); } catch (Exception e) { throw new WeldException(e); } }
public static <T> T getOuterDelegate(Bean<T> bean, T instance, CreationalContext<T> creationalContext, Class<T> proxyClass, InjectionPoint originalInjectionPoint, BeanManagerImpl manager, List<Decorator<?>> decorators) { TargetBeanInstance beanInstance = new TargetBeanInstance(bean, instance); DecorationHelper<T> decorationHelper = new DecorationHelper<T>(beanInstance, bean, proxyClass, manager, manager.getServices().get(ContextualStore.class), decorators); DecorationHelper.push(decorationHelper); try { final T outerDelegate = decorationHelper.getNextDelegate(originalInjectionPoint, creationalContext); if (outerDelegate == null) { throw new WeldException(BeanLogger.LOG.proxyInstantiationFailed(bean)); } return outerDelegate; } finally { DecorationHelper.pop(); } }
/** * Invokes the method on a given instance passing in given parameters. If the invocation yields * {@link InvocationTargetException}, the exception is unwrapped. * * It is a responsibility of the caller to make sure that the method is accessible to the caller. */ public static <T> T invokeAndUnwrap(final Object instance, final Method method, final Object... parameters) throws Throwable { try { return cast(method.invoke(instance, parameters)); } catch (IllegalArgumentException e) { throw ReflectionLogger.LOG.illegalArgumentExceptionOnReflectionInvocation(instance.getClass(), instance, method, Arrays.toString(parameters), e); } catch (IllegalAccessException e) { throw new WeldException(e); } catch (InvocationTargetException e) { throw e.getCause(); } }
@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); } }