@Override public T produce(Object receiver, CreationalContext<T> creationalContext) { // unwrap if we have a proxy if (receiver instanceof TargetInstanceProxy) { receiver = Reflections.<TargetInstanceProxy<T>> cast(receiver).getTargetInstance(); } try { return cast(accessibleField.get(receiver)); } catch (IllegalAccessException e) { throw UtilLogger.LOG.accessErrorOnField(accessibleField.getName(), accessibleField.getDeclaringClass(), e); } }
public Class<?>[] extractInterceptorClasses(Annotated annotated) { Annotation annotation = annotated.getAnnotation(INTERCEPTORS_ANNOTATION_CLASS); if (annotation != null) { try { return (Class<?>[]) interceptorsValueMethod.invoke(annotation); } catch (Exception e) { throw UtilLogger.LOG.annotationValuesInaccessible(e); } } return null; }
private TypeInfo add(Type type) { if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isInterface()) { interfaces.add(clazz); } else { classes.add(clazz); } } else if (type instanceof ParameterizedType) { add(((ParameterizedType) type).getRawType()); } else { throw UtilLogger.LOG.cannotProxyNonClassType(type); } return this; }
@Override public void processMethod(EnhancedAnnotatedMethod<?, ? super T> method) { if (method.isAnnotationPresent(Inject.class)) { if (method.getAnnotation(Produces.class) != null) { throw UtilLogger.LOG.initializerCannotBeProducer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw UtilLogger.LOG.initializerCannotBeDisposalMethod(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Observes.class).size() > 0) { throw EventLogger.LOG.invalidInitializer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.isGeneric()) { throw UtilLogger.LOG.initializerMethodIsGeneric(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } if (!method.isStatic()) { currentLevel.add(InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.INITIALIZER, method, declaringBean, type.getJavaClass(), null, manager)); } } }
@Override public RuntimeException apply(Type eventType) { Type resolvedType = Types.getCanonicalType(eventType); /* * If the runtime type of the event object contains a type variable, the container must throw an IllegalArgumentException. */ if (Types.containsTypeVariable(resolvedType)) { return UtilLogger.LOG.typeParameterNotAllowedInEventType(eventType); } /* * If the runtime type of the event object is assignable to the type of a container lifecycle event, IllegalArgumentException is thrown. */ Class<?> resolvedClass = Reflections.getRawType(eventType); for (Class<?> containerEventType : Observers.CONTAINER_LIFECYCLE_EVENT_CANONICAL_SUPERTYPES) { if (containerEventType.isAssignableFrom(resolvedClass)) { return UtilLogger.LOG.eventTypeNotAllowed(eventType); } } return NO_EXCEPTION_MARKER; } }
public static <T> EnhancedAnnotatedConstructor<T> getBeanConstructor(EnhancedAnnotatedType<T> type) { Collection<EnhancedAnnotatedConstructor<T>> initializerAnnotatedConstructors = type .getEnhancedConstructors(Inject.class); BeanLogger.LOG.foundInjectableConstructors(initializerAnnotatedConstructors, type); EnhancedAnnotatedConstructor<T> constructor = null; if (initializerAnnotatedConstructors.size() > 1) { throw UtilLogger.LOG.ambiguousConstructor(type, initializerAnnotatedConstructors); } else if (initializerAnnotatedConstructors.size() == 1) { constructor = initializerAnnotatedConstructors.iterator().next(); BeanLogger.LOG.foundOneInjectableConstructor(constructor, type); } else if (type.getNoArgsEnhancedConstructor() != null) { constructor = type.getNoArgsEnhancedConstructor(); BeanLogger.LOG.foundDefaultConstructor(constructor, type); } if (constructor != null) { if (!constructor.getEnhancedParameters(Disposes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Disposes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } if (!constructor.getEnhancedParameters(Observes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Observes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } } return constructor; }
public static Set<Annotation> mergeInQualifiers(BeanManagerImpl manager, Collection<Annotation> qualifiers, Annotation[] newQualifiers) { Set<Annotation> result = new HashSet<Annotation>(); if (qualifiers != null && !(qualifiers.isEmpty())) { result.addAll(qualifiers); } if (newQualifiers != null && newQualifiers.length > 0) { final MetaAnnotationStore store = manager.getServices().get(MetaAnnotationStore.class); Set<Annotation> checkedNewQualifiers = new HashSet<Annotation>(); for (Annotation qualifier : newQualifiers) { if (!store.getBindingTypeModel(qualifier.annotationType()).isValid()) { throw UtilLogger.LOG.annotationNotQualifier(qualifier); } Class<? extends Annotation> annotationType = qualifier.annotationType(); if (!annotationType.isAnnotationPresent(Repeatable.class)) { for (Annotation annotation : checkedNewQualifiers) { if(annotationType.equals(annotation.annotationType())) { throw UtilLogger.LOG.redundantQualifier(qualifier, Arrays.toString(newQualifiers)); } } } checkedNewQualifiers.add(qualifier); } result.addAll(checkedNewQualifiers); } return result; }
protected Object enumValue(Class<?> clazz, String memberName) { Preconditions.checkArgumentNotNull(memberName, "memberName"); if (!clazz.isEnum()) { throw UtilLogger.LOG.classNotEnum(clazz); } try { Field field = clazz.getField(memberName); return field.get(null); } catch (SecurityException e) { return null; } catch (NoSuchFieldException e) { return null; } catch (java.lang.IllegalArgumentException e) { return null; } catch (IllegalAccessException e) { return null; } }
@Override public void inject(T instance, CreationalContext<T> ctx) { super.inject(instance, ctx); if (accessibleField != null && instance instanceof DecoratorProxy) { // this code is only applicable if the delegate is injected into a field // as the proxy can't intercept the delegate when setting the field // we need to now read the delegate from the field // this is only needed for fields, as constructor and method injection are handed // at injection time Object delegate; try { delegate = accessibleField.get(instance); } catch (IllegalAccessException e) { throw UtilLogger.LOG.accessErrorOnField(accessibleField.getName(), accessibleField.getDeclaringClass(), e); } final ProxyMethodHandler handler = new ProxyMethodHandler(beanManager.getContextId(), new TargetBeanInstance(delegate), getBean()); ((ProxyObject) instance).weld_setHandler(handler); } }
@Override public void processMethod(EnhancedAnnotatedMethod<?, ? super T> method) { if (method.isAnnotationPresent(Inject.class)) { if (method.getAnnotation(Produces.class) != null) { throw UtilLogger.LOG.initializerCannotBeProducer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw UtilLogger.LOG.initializerCannotBeDisposalMethod(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Observes.class).size() > 0) { throw EventLogger.LOG.invalidInitializer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.isGeneric()) { throw UtilLogger.LOG.initializerMethodIsGeneric(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } if (!method.isStatic()) { currentLevel.add(InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.INITIALIZER, method, declaringBean, type.getJavaClass(), null, manager)); } } }
public static <T> EnhancedAnnotatedConstructor<T> getBeanConstructor(EnhancedAnnotatedType<T> type) { Collection<EnhancedAnnotatedConstructor<T>> initializerAnnotatedConstructors = type .getEnhancedConstructors(Inject.class); BeanLogger.LOG.foundInjectableConstructors(initializerAnnotatedConstructors, type); EnhancedAnnotatedConstructor<T> constructor = null; if (initializerAnnotatedConstructors.size() > 1) { throw UtilLogger.LOG.ambiguousConstructor(type, initializerAnnotatedConstructors); } else if (initializerAnnotatedConstructors.size() == 1) { constructor = initializerAnnotatedConstructors.iterator().next(); BeanLogger.LOG.foundOneInjectableConstructor(constructor, type); } else if (type.getNoArgsEnhancedConstructor() != null) { constructor = type.getNoArgsEnhancedConstructor(); BeanLogger.LOG.foundDefaultConstructor(constructor, type); } if (constructor != null) { if (!constructor.getEnhancedParameters(Disposes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Disposes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } if (!constructor.getEnhancedParameters(Observes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Observes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } if (!constructor.getEnhancedParameters(ObservesAsync.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@ObservesAsync", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } } return constructor; }
@Override public RuntimeException apply(Type eventType) { Type resolvedType = Types.getCanonicalType(eventType); /* * If the runtime type of the event object contains a type variable, the container must throw an IllegalArgumentException. */ if (Types.containsTypeVariable(resolvedType)) { return UtilLogger.LOG.typeParameterNotAllowedInEventType(eventType); } /* * If the runtime type of the event object is assignable to the type of a container lifecycle event, IllegalArgumentException is thrown. */ Class<?> resolvedClass = Reflections.getRawType(eventType); for (Class<?> containerEventType : Observers.CONTAINER_LIFECYCLE_EVENT_CANONICAL_SUPERTYPES) { if (containerEventType.isAssignableFrom(resolvedClass)) { return UtilLogger.LOG.eventTypeNotAllowed(eventType); } } return NO_EXCEPTION_MARKER; } }
public static Set<Annotation> mergeInQualifiers(BeanManagerImpl manager, Collection<Annotation> qualifiers, Annotation[] newQualifiers) { Set<Annotation> result = new HashSet<Annotation>(); if (qualifiers != null && !(qualifiers.isEmpty())) { result.addAll(qualifiers); } if (newQualifiers != null && newQualifiers.length > 0) { final MetaAnnotationStore store = manager.getServices().get(MetaAnnotationStore.class); Set<Annotation> checkedNewQualifiers = new HashSet<Annotation>(); for (Annotation qualifier : newQualifiers) { if (!store.getBindingTypeModel(qualifier.annotationType()).isValid()) { throw UtilLogger.LOG.annotationNotQualifier(qualifier); } Class<? extends Annotation> annotationType = qualifier.annotationType(); if (!annotationType.isAnnotationPresent(Repeatable.class)) { for (Annotation annotation : checkedNewQualifiers) { if(annotationType.equals(annotation.annotationType())) { throw UtilLogger.LOG.redundantQualifier(qualifier, Arrays.toString(newQualifiers)); } } } checkedNewQualifiers.add(qualifier); } result.addAll(checkedNewQualifiers); } return result; }
protected Object enumValue(Class<?> clazz, String memberName) { Preconditions.checkArgumentNotNull(memberName, "memberName"); if (!clazz.isEnum()) { throw UtilLogger.LOG.classNotEnum(clazz); } try { Field field = clazz.getField(memberName); return field.get(null); } catch (SecurityException e) { return null; } catch (NoSuchFieldException e) { return null; } catch (java.lang.IllegalArgumentException e) { return null; } catch (IllegalAccessException e) { return null; } }
@Override public void inject(T instance, CreationalContext<T> ctx) { super.inject(instance, ctx); if (accessibleField != null && instance instanceof DecoratorProxy) { // this code is only applicable if the delegate is injected into a field // as the proxy can't intercept the delegate when setting the field // we need to now read the delegate from the field // this is only needed for fields, as constructor and method injection are handed // at injection time Object delegate; try { delegate = accessibleField.get(instance); } catch (IllegalAccessException e) { throw UtilLogger.LOG.accessErrorOnField(accessibleField.getName(), accessibleField.getDeclaringClass(), e); } final ProxyMethodHandler handler = new ProxyMethodHandler(beanManager.getContextId(), new TargetBeanInstance(delegate), getBean()); ((ProxyObject) instance).setHandler(handler); } }
@Override public void processMethod(EnhancedAnnotatedMethod<?, ? super T> method) { if (method.isAnnotationPresent(Inject.class)) { if (method.getAnnotation(Produces.class) != null) { throw UtilLogger.LOG.initializerCannotBeProducer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw UtilLogger.LOG.initializerCannotBeDisposalMethod(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Observes.class).size() > 0) { throw EventLogger.LOG.invalidInitializer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.isGeneric()) { throw UtilLogger.LOG.initializerMethodIsGeneric(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } if (!method.isStatic()) { currentLevel.add(InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.INITIALIZER, method, declaringBean, type.getJavaClass(), null, manager)); } } }
public static <T> EnhancedAnnotatedConstructor<T> getBeanConstructor(EnhancedAnnotatedType<T> type) { Collection<EnhancedAnnotatedConstructor<T>> initializerAnnotatedConstructors = type .getEnhancedConstructors(Inject.class); BeanLogger.LOG.foundInjectableConstructors(initializerAnnotatedConstructors, type); EnhancedAnnotatedConstructor<T> constructor = null; if (initializerAnnotatedConstructors.size() > 1) { throw UtilLogger.LOG.ambiguousConstructor(type, initializerAnnotatedConstructors); } else if (initializerAnnotatedConstructors.size() == 1) { constructor = initializerAnnotatedConstructors.iterator().next(); BeanLogger.LOG.foundOneInjectableConstructor(constructor, type); } else if (type.getNoArgsEnhancedConstructor() != null) { constructor = type.getNoArgsEnhancedConstructor(); BeanLogger.LOG.foundDefaultConstructor(constructor, type); } if (constructor != null) { if (!constructor.getEnhancedParameters(Disposes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Disposes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } if (!constructor.getEnhancedParameters(Observes.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@Observes", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } if (!constructor.getEnhancedParameters(ObservesAsync.class).isEmpty()) { throw BeanLogger.LOG.parameterAnnotationNotAllowedOnConstructor("@ObservesAsync", constructor, Formats.formatAsStackTraceElement(constructor.getJavaMember())); } } return constructor; }
@Override public RuntimeException apply(Type eventType) { Type resolvedType = Types.getCanonicalType(eventType); /* * If the runtime type of the event object contains a type variable, the container must throw an IllegalArgumentException. */ if (Types.containsTypeVariable(resolvedType)) { return UtilLogger.LOG.typeParameterNotAllowedInEventType(eventType); } /* * If the runtime type of the event object is assignable to the type of a container lifecycle event, IllegalArgumentException is thrown. */ Class<?> resolvedClass = Reflections.getRawType(eventType); for (Class<?> containerEventType : Observers.CONTAINER_LIFECYCLE_EVENT_CANONICAL_SUPERTYPES) { if (containerEventType.isAssignableFrom(resolvedClass)) { return UtilLogger.LOG.eventTypeNotAllowed(eventType); } } return NO_EXCEPTION_MARKER; } }
@Override public T produce(Object receiver, CreationalContext<T> creationalContext) { // unwrap if we have a proxy if (receiver instanceof TargetInstanceProxy) { receiver = Reflections.<TargetInstanceProxy<T>> cast(receiver).weld_getTargetInstance(); } try { return cast(accessibleField.get(receiver)); } catch (IllegalAccessException e) { throw UtilLogger.LOG.accessErrorOnField(accessibleField.getName(), accessibleField.getDeclaringClass(), e); } }
public static Set<Annotation> mergeInQualifiers(BeanManagerImpl manager, Collection<Annotation> qualifiers, Annotation[] newQualifiers) { Set<Annotation> result = new HashSet<Annotation>(); if (qualifiers != null && !(qualifiers.isEmpty())) { result.addAll(qualifiers); } if (newQualifiers != null && newQualifiers.length > 0) { final MetaAnnotationStore store = manager.getServices().get(MetaAnnotationStore.class); Set<Annotation> checkedNewQualifiers = new HashSet<Annotation>(); for (Annotation qualifier : newQualifiers) { if (!store.getBindingTypeModel(qualifier.annotationType()).isValid()) { throw UtilLogger.LOG.annotationNotQualifier(qualifier); } final Class<? extends Annotation> annotationType = qualifier.annotationType(); for (Annotation annotation : checkedNewQualifiers) { if(annotationType.equals(annotation.annotationType())) { throw UtilLogger.LOG.redundantQualifier(qualifier, Arrays.toString(newQualifiers)); } } checkedNewQualifiers.add(qualifier); } result.addAll(checkedNewQualifiers); } return result; }