private String getFieldName(InjectionPoint ip) { Annotated annotated = ip.getAnnotated(); if (annotated instanceof AnnotatedField) { AnnotatedField f = (AnnotatedField) annotated; return f.getJavaMember().getName(); } if (annotated instanceof AnnotatedParameter) { AnnotatedParameter p = (AnnotatedParameter) annotated; Member member = ip.getMember(); if (member instanceof Method) { return member.getName() + "_" + p.getPosition(); } if (member instanceof Constructor) { return "new_" + p.getPosition(); } } return ip.getMember().getName(); }
@Override public Object resolveResource(InjectionPoint injectionPoint) { final Member member = injectionPoint.getMember(); AnnotatedMember<?> annotatedMember; if (injectionPoint.getAnnotated() instanceof AnnotatedField) { annotatedMember = (AnnotatedField<?>) injectionPoint.getAnnotated(); } else { annotatedMember = ((AnnotatedParameter<?>) injectionPoint.getAnnotated()).getDeclaringCallable(); } if (!annotatedMember.isAnnotationPresent(Resource.class)) { throw WeldLogger.ROOT_LOGGER.annotationNotFound(Resource.class, member); } if (member instanceof Method && ((Method) member).getParameterTypes().length != 1) { throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) member); } String name = getResourceName(injectionPoint); for (ResourceInjectionResolver resolver : resourceResolvers) { Object result = resolver.resolve(name); if (result != null) { return result; } } try { return context.lookup(name); } catch (NamingException e) { throw WeldLogger.ROOT_LOGGER.couldNotFindResource(name, injectionPoint.getMember().toString(), e); } }
/** * Get the underlying {@link Parameter}. * * @return the {@link Parameter} */ default Parameter getJavaParameter() { Member member = getDeclaringCallable().getJavaMember(); if (!(member instanceof Executable)) { throw new IllegalStateException("Parameter does not belong to an executable: " + member); } Executable executable = (Executable) member; return executable.getParameters()[getPosition()]; }
this.javaClass = type.getJavaClass(); for (AnnotatedField<? super X> field : type.getFields()) { if (fields.get(field.getJavaMember()) == null) { fields.put(field.getJavaMember(), new AnnotationBuilder()); mergeAnnotationsOnElement(field, overwrite, fields.get(field.getJavaMember())); for (AnnotatedMethod<? super X> method : type.getMethods()) { if (methods.get(method.getJavaMember()) == null) { methods.put(method.getJavaMember(), new AnnotationBuilder()); if (methodParameters.get(method.getJavaMember()).get(p.getPosition()) == null) { methodParameters.get(method.getJavaMember()).put(p.getPosition(), new AnnotationBuilder()); mergeAnnotationsOnElement(p, overwrite, methodParameters.get(method.getJavaMember()).get(p.getPosition())); for (AnnotatedConstructor<? super X> constructor : type.getConstructors()) { if (constructors.get(constructor.getJavaMember()) == null) { constructors.put(constructor.getJavaMember(), new AnnotationBuilder()); if (constructorParameters.get(constructor.getJavaMember()).get(p.getPosition()) == null) { constructorParameters.get(constructor.getJavaMember()).put(p.getPosition(), new AnnotationBuilder()); mergeAnnotationsOnElement(p, overwrite, constructorParameters.get(constructor.getJavaMember()).get(p.getPosition()));
@Override public ObserverMethodConfigurator<T> read(AnnotatedMethod<?> method) { checkArgumentNotNull(method); Set<AnnotatedParameter<?>> eventParameters = method.getParameters().stream() .filter((p) -> p.isAnnotationPresent(Observes.class) || p.isAnnotationPresent(ObservesAsync.class)).collect(Collectors.toSet()); checkEventParams(eventParameters, method.getJavaMember()); AnnotatedParameter<?> eventParameter = eventParameters.iterator().next(); Observes observesAnnotation = eventParameter.getAnnotation(Observes.class); if (observesAnnotation != null) { reception(observesAnnotation.notifyObserver()); transactionPhase(observesAnnotation.during()); } else { reception(eventParameter.getAnnotation(ObservesAsync.class).notifyObserver()); } Priority priority = method.getAnnotation(Priority.class); if (priority != null) { priority(priority.value()); } beanClass(eventParameter.getDeclaringCallable().getDeclaringType().getJavaClass()); observedType(eventParameter.getBaseType()); qualifiers(Configurators.getQualifiers(eventParameter)); return this; }
public InjectionPointHolder(String contextId, InjectionPoint ip) { super(ip); Preconditions.checkArgumentNotNull(ip, "injectionPoint"); if (ip.getBean() == null) { if (ip instanceof Serializable) { this.identifier = new SerializableInjectionPointIdentifier(ip); } else { this.identifier = new TransientInjectionPointIdentifier(ip); } } else if (ip.getAnnotated() instanceof AnnotatedField<?>) { AnnotatedField<?> field = Reflections.cast(ip.getAnnotated()); this.identifier = new FieldInjectionPointIdentifier(contextId, ip.getBean(), field); } else if (ip.getAnnotated() instanceof AnnotatedParameter<?>) { AnnotatedParameter<?> parameter = Reflections.cast(ip.getAnnotated()); if (parameter.getDeclaringCallable() instanceof AnnotatedConstructor<?>) { AnnotatedConstructor<?> constructor = Reflections.cast(parameter.getDeclaringCallable()); this.identifier = new ConstructorParameterInjectionPointIdentifier(contextId, ip.getBean(), parameter.getPosition(), constructor); } else if (parameter.getDeclaringCallable() instanceof AnnotatedMethod<?>) { AnnotatedMethod<?> method = Reflections.cast(parameter.getDeclaringCallable()); this.identifier = new MethodParameterInjectionPointIdentifier(contextId, ip.getBean(), parameter.getPosition(), method); } else { throw BeanLogger.LOG.invalidAnnotatedCallable(parameter.getDeclaringCallable()); } } else { throw BeanLogger.LOG.invalidAnnotatedOfInjectionPoint(ip.getAnnotated(), ip); } }
protected abstract boolean matches(InjectionPoint ip, AnnotatedCallable<?> annotatedCallable); }
@SpecAssertions({ @SpecAssertion(section = PROCESS_BEAN, id = "eaa"), @SpecAssertion(section = PROCESS_BEAN, id = "eab"), @SpecAssertion(section = PROCESS_BEAN, id = "edc"), @SpecAssertion(section = PROCESS_BEAN, id = "efc"), @SpecAssertion(section = PROCESS_BEAN, id = "fc"), @SpecAssertion(section = PROCESS_BEAN, id = "i"), @SpecAssertion(section = PROCESS_BEAN, id = "j"), @SpecAssertion(section = BEAN_DISCOVERY_STEPS, id = "jb"), @SpecAssertion(section = BEAN_DISCOVERY_STEPS, id = "jd") }) @Test public void testProcessProducerMethodEvent() { assertTrue(ProcessBeanObserver.getCowBean().getTypes().contains(Cow.class)); assertEquals(ProcessBeanObserver.getCowBean().getBeanClass(), Cowshed.class); assertEquals(ProcessBeanObserver.getCowMethod().getBaseType(), Cow.class); assertEquals(ProcessBeanObserver.getCowMethod().getDeclaringType().getBaseType(), Cowshed.class); // There are bugs in the API that mean generic type parameter ordering is wrong for ProcessProducerField and // ProcessProducerMethod // https://issues.jboss.org/browse/CDITCK-168 // https://issues.jboss.org/browse/WELD-586 assertEquals(ProcessBeanObserver.getCowShedProcessBeanCount(), 2); assertTrue(ProcessBeanObserver.getCowAnnotated() instanceof AnnotatedMethod<?>); assertEquals(ProcessBeanObserver.getCowMethod().getJavaMember().getName(), "getDaisy"); assertEquals(ProcessBeanObserver.getCowMethod().getJavaMember().getDeclaringClass(), Cowshed.class); AnnotatedParameter<Cow> disposedParam = ProcessBeanObserver.getCowParameter(); assertNotNull(disposedParam); assertTrue(disposedParam.isAnnotationPresent(Disposes.class)); assertEquals(disposedParam.getBaseType(), Cow.class); assertEquals(disposedParam.getDeclaringCallable().getJavaMember().getName(), "disposeOfDaisy"); assertEquals(disposedParam.getDeclaringCallable().getJavaMember().getDeclaringClass(), Cowshed.class); assertEquals(disposedParam.getDeclaringCallable().getDeclaringType().getJavaClass(), Cowshed.class); assertEquals(ProcessBeanObserver.getCowActionSeq().getData(), Arrays.asList(ProcessBeanAttributes.class.getName(), ProcessProducerMethod.class.getName())); }
private boolean annotatedTypeHasAnnotations(AnnotatedType annotatedType, Class<? extends Annotation>[] withAnnotations) if (hasAnnotation(annotatedType.getAnnotations(), withAnnotations)) Set<AnnotatedField> fields = annotatedType.getFields(); for (AnnotatedField annotatedField : fields) if (hasAnnotation(annotatedField.getAnnotations(), withAnnotations)) Set<AnnotatedMethod> annotatedMethods = annotatedType.getMethods(); for (AnnotatedMethod annotatedMethod : annotatedMethods) if (hasAnnotation(annotatedMethod.getAnnotations(), withAnnotations)) for (AnnotatedParameter annotatedParameter : (List<AnnotatedParameter>) annotatedMethod.getParameters()) if (hasAnnotation(annotatedParameter.getAnnotations(), withAnnotations)) for (AnnotatedConstructor<?> annotatedConstructor : annotatedConstructors) if (hasAnnotation(annotatedConstructor.getAnnotations(), withAnnotations)) for (AnnotatedParameter annotatedParameter : annotatedConstructor.getParameters()) if (hasAnnotation(annotatedParameter.getAnnotations(), withAnnotations))
/** * Certain beans like CDI Interceptors and Decorators * are not allowed to define producer methods. */ protected void validateNoProducerOrObserverMethod(AnnotatedType<T> annotatedType) { Set<AnnotatedMethod<? super T>> annotatedMethods = annotatedType.getMethods(); for (AnnotatedMethod<?> annotatedMethod : annotatedMethods) { if (annotatedMethod.isAnnotationPresent(Produces.class)) { throw new WebBeansConfigurationException("This class must not have a @Produces method" + annotatedMethod.getJavaMember()); } for (AnnotatedParameter<?> parameter : annotatedMethod.getParameters()) { if (parameter.isAnnotationPresent(Observes.class) || parameter.isAnnotationPresent(ObservesAsync.class)) { throw new WebBeansConfigurationException("This class must not have an @Observes nor @ObservesAsync method " + annotatedMethod.getJavaMember()); } } } Set<AnnotatedField<? super T>> annotatedFields = annotatedType.getFields(); for (AnnotatedField<? super T> annotatedField : annotatedFields) { if (annotatedField.isAnnotationPresent(Produces.class)) { throw new WebBeansConfigurationException("This class must not have a @Produces field" + annotatedField.getJavaMember()); } } }
for (Annotation annotation : field.getAnnotations()) { if (isEqualOrAnnotated(requiredAnnotation, annotation)) { return true; for (Annotation annotation : constructor.getAnnotations()) { if (isEqualOrAnnotated(requiredAnnotation, annotation)) { return true; for (AnnotatedParameter<?> parameter : constructor.getParameters()) { for (Annotation annotation : parameter.getAnnotations()) { if (isEqualOrAnnotated(requiredAnnotation, annotation)) { return true; for (Annotation annotation : method.getAnnotations()) { if (isEqualOrAnnotated(requiredAnnotation, annotation)) { return true; for (AnnotatedParameter<?> parameter : method.getParameters()) { for (Annotation annotation : parameter.getAnnotations()) { if (isEqualOrAnnotated(requiredAnnotation, annotation)) { return true;
private void introspectType() if (getBeanType().isAnnotationPresent(javax.interceptor.Interceptor.class) || getBeanType().isAnnotationPresent(javax.decorator.Decorator.class)) { _isInterceptorOrDecorator = true; return; for (AnnotatedField<? super X> field : getBeanType().getFields()) { if (field.isAnnotationPresent(Delegate.class)) { _isInterceptorOrDecorator = true; return; for (AnnotatedMethod<? super X> method : getBeanType().getMethods()) { for (AnnotatedParameter<? super X> param : method.getParameters()) { if (param.isAnnotationPresent(Delegate.class)) { _isInterceptorOrDecorator = true; return;
@Override protected InterceptionFactory<?> newInstance(InjectionPoint ip, CreationalContext<InterceptionFactory<?>> creationalContext) { AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ip.getAnnotated(); ParameterizedType parameterizedType = (ParameterizedType) annotatedParameter.getBaseType(); AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(Reflections.getRawType(parameterizedType.getActualTypeArguments()[0])); return InterceptionFactoryImpl.of(beanManager, creationalContext, annotatedType); }
@Override public ParameterInjectionPointAttributes<?, ?> createInjectionPoint(AnnotatedParameter<?> parameter) { AnnotatedTypeValidator.validateAnnotatedParameter(parameter); EnhancedAnnotatedParameter<?, ?> enhancedParameter = services.get(MemberTransformer.class).loadEnhancedParameter(parameter, getId()); return validateInjectionPoint(InferringParameterInjectionPointAttributes.of(enhancedParameter, null, parameter.getDeclaringCallable().getDeclaringType().getJavaClass(), this)); }
public AnnotatedParameterImpl(AnnotatedParameter<? super T> param, AnnotatedCallable<T> declaringCallable) { this(param.getBaseType(), param.getTypeClosure(), param.getAnnotations(), declaringCallable, param.getPosition()); }
public MethodSignatureImpl(AnnotatedMethod<?> method) { this.methodName = method.getJavaMember().getName(); this.parameterTypes = new String[method.getParameters().size()]; for (int i = 0; i < method.getParameters().size(); i++) { parameterTypes[i] = Reflections.getRawType(method.getParameters().get(i).getBaseType()).getName(); } }
protected void checkInterceptorConditions() { Set<AnnotatedMethod<? super T>> methods = webBeansContext.getAnnotatedElementFactory().getFilteredAnnotatedMethods(annotatedType); for(AnnotatedMethod<?> method : methods) { for (AnnotatedParameter<?> parameter : method.getParameters()) { if (parameter.isAnnotationPresent(Produces.class)) { throw new WebBeansConfigurationException("Interceptor class : " + annotatedType.getJavaClass() + " can not have producer methods but it has one with name : " + method.getJavaMember().getName()); } } } }
Annotation []qualifiers) for (AnnotatedMethod<? super X> beanMethod : beanType.getMethods()) { List<AnnotatedParameter<?>> params = (List) beanMethod.getParameters(); if (! param.isAnnotationPresent(Disposes.class)) continue; if (! producesBaseType.equals(param.getBaseType())) continue; Method javaMethod = beanMethod.getJavaMember(); if (beanMethod.isAnnotationPresent(Inject.class)) throw new ConfigException(L.l("{0}.{1} is an invalid @Disposes method because it has an @Inject annotation", javaMethod.getDeclaringClass().getName(),
/** * * @param injectionPoint * @param factory */ SetterResourceInjection(ParameterInjectionPoint<T, X> injectionPoint, ResourceReferenceFactory<T> factory) { super(factory); AnnotatedMethod<X> annotatedMethod = (AnnotatedMethod<X>) injectionPoint.getAnnotated().getDeclaringCallable(); accessibleMethod = AccessController.doPrivileged(new GetAccessibleCopyOfMember<Method>(annotatedMethod.getJavaMember())); }
public static Annotated getResourceAnnotated(InjectionPoint injectionPoint) { if(injectionPoint instanceof ParameterInjectionPoint) { return ((ParameterInjectionPoint<?, ?>)injectionPoint).getAnnotated().getDeclaringCallable(); } return injectionPoint.getAnnotated(); }