protected void defineDisposalMethod()

in webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BaseProducerFactory.java [74:169]


    protected void defineDisposalMethod()
    {
        AnnotatedMember<? super P> producer = producerType();
        Set<Annotation> producerQualifiers = webBeansContext.getAnnotationManager().getQualifierAnnotations(producer.getAnnotations());
        if (producerQualifiers.size() == 1 && producerQualifiers.iterator().next().annotationType().equals(Default.class))
        {
            producerQualifiers = Collections.emptySet();
        }
        Set<Annotation> producerQualifiersWithoutNamed = new HashSet<>();
        for (Annotation qualifier: producerQualifiers)
        {
            if (!qualifier.annotationType().equals(Named.class))
            {
                producerQualifiersWithoutNamed.add(qualifier);
            }
        }

        AnnotatedType declaringType = producer.getDeclaringType();
        Type producerBaseType = producerType().getBaseType();
        Set<AnnotatedMethod<? super P>> annotatedMethods =
                webBeansContext.getAnnotatedElementFactory().getFilteredAnnotatedMethods(declaringType);

        AnnotatedMethod<? super P> anyDisposal = null;

        for (AnnotatedMethod<? super P> annotatedMethod : annotatedMethods)
        {
            if (annotatedMethod.getDeclaringType().equals(declaringType))
            {
                for (AnnotatedParameter<? super P> annotatedParameter : annotatedMethod.getParameters())
                {
                    if (annotatedParameter.isAnnotationPresent(Disposes.class))
                    {
                        if (!GenericsUtil.satisfiesDependency(false, true, producerBaseType, annotatedParameter.getBaseType(), new HashMap<>()))
                        {
                            continue;
                        }

                        Set<Annotation> producerQualifiersToCompare = producerQualifiers;
                        Set<Annotation> disposalQualifiers = webBeansContext.getAnnotationManager().getQualifierAnnotations(annotatedParameter.getAnnotations());
                        if (disposalQualifiers.size() == 1 && disposalQualifiers.iterator().next().annotationType().equals(Default.class))
                        {
                            disposalQualifiers = Collections.emptySet();
                        }
                        if (disposalQualifiers.size() == producerQualifiersToCompare.size() - 1)
                        {
                            // when @Named is present at the producer it may be ignored at the disposal
                            producerQualifiersToCompare = producerQualifiersWithoutNamed;
                        }
                        if (disposalQualifiers.size() != producerQualifiersToCompare.size())
                        {
                            continue;
                        }
                        boolean same = true;
                        for (Annotation disposalQualifier : disposalQualifiers)
                        {
                            boolean found = false;
                            for (Annotation producerQualifier : producerQualifiers)
                            {
                                if (AnnotationUtil.isCdiAnnotationEqual(producerQualifier, disposalQualifier))
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                same = false;
                                break;
                            }
                        }
                        if (!same)
                        {
                            if (disposalQualifiers.size() == 1 && AnyLiteral.INSTANCE.equals(disposalQualifiers.iterator().next()))
                            {
                                anyDisposal = annotatedMethod;
                            }
                            continue;
                        }
                        if (disposalMethod != null)
                        {
                            throw new WebBeansConfigurationException("There are multiple disposal method for the producer method : "
                                    + disposalMethod.getJavaMember().getName() + " in class : "
                                    + annotatedMethod.getDeclaringType().getJavaClass());
                        }
                        validateDisposalMethod(declaringType, annotatedMethod);
                        disposalMethod = annotatedMethod;
                    }
                }
            }
        }
        if (disposalMethod == null && anyDisposal != null)
        {
            validateDisposalMethod(declaringType, anyDisposal);
            disposalMethod = anyDisposal;
        }
    }