void validate()

in scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java [649:790]


    void validate(final ComponentMetadata componentMetadata )
    {
        final DSVersion dsVersion = componentMetadata.getDSVersion();

        if ( m_name == null )
        {
            // 112.10 name attribute is optional, defaults to interface since DS 1.1
            if ( !dsVersion.isDS11() )
            {
                throw componentMetadata.validationFailure( "A name must be declared for the reference" );
            }
            setName( getInterface() );
        }

        if ( m_interface == null )
        {
            throw componentMetadata.validationFailure( "An interface must be declared for the reference" );
        }


        if ( m_cardinality == null )
        {
            setCardinality( CARDINALITY_1_1 );
        }
        else if ( !CARDINALITY_VALID.contains( m_cardinality ) )
        {
            throw componentMetadata.validationFailure( "Cardinality must be one of " + CARDINALITY_VALID );
        }

        if ( m_policy == null )
        {
            setPolicy( POLICY_STATIC );
        }
        else if ( !POLICY_VALID.contains( m_policy ) )
        {
            throw componentMetadata.validationFailure( "Policy must be one of " + POLICY_VALID );
        }

        if ( m_policy_option == null )
        {
            setPolicyOption( POLICY_OPTION_RELUCTANT );
        }
        else if ( !POLICY_OPTION_VALID.contains( m_policy_option ) )
        {
            throw componentMetadata.validationFailure( "Policy option must be one of " + POLICY_OPTION_VALID );
        }
        else if ( !dsVersion.isDS12() && !POLICY_OPTION_RELUCTANT.equals( m_policy_option ) )
        {
            throw componentMetadata.validationFailure( "Policy option must be reluctant for DS < 1.2" );
        }

        if (m_scopeName != null) {
        	    if ( !dsVersion.isDS13() )
        	    {
        		    throw componentMetadata.validationFailure( "reference scope can be set only for DS >= 1.3");
        	    }
            	try
        	    {
        		    m_scope = ReferenceScope.valueOf(m_scopeName);
        	    }
        	    catch (final IllegalArgumentException e)
        	    {
        		    throw componentMetadata.validationFailure( "reference scope must be 'bundle' or 'prototype' not " + m_scopeName);
        	    }
        }

        // checks for event based injection
        // updated method is only supported in namespace xxx and later
        if ( m_updated != null && !(dsVersion.isDS12() || dsVersion == DSVersion.DS11Felix) )
        {
            // FELIX-3648 validation must fail (instead of just ignore)
            throw componentMetadata.validationFailure( "updated method declaration requires DS 1.2 or later namespace " );
        }

        // checks for field injection
        if ( m_field != null )
        {
            // field reference requires DS 1.3
            if ( !dsVersion.isDS13() )
            {
                throw componentMetadata.validationFailure( "Field reference requires DS >= 1.3" );
            }

            // field strategy
            if ( m_field_option == null )
            {
                setFieldOption( FIELD_STRATEGY_REPLACE );
            }
            else if ( !FIELD_STRATEGY_VALID.contains( m_field_option ) )
            {
                throw componentMetadata.validationFailure( "Field strategy must be one of " + FIELD_STRATEGY_VALID );
            }
            if ( !m_isMultiple )
            {
                // update is not allowed for unary references
                if ( m_field_option.equals(FIELD_STRATEGY_UPDATE) )
                {
                    throw componentMetadata.validationFailure( "Field strategy update not allowed for unary field references." );
                }
            }

            // field value type
            if (m_collection_type != null
                && !FIELD_VALUE_TYPE_VALID.contains(m_collection_type))
            {
                throw componentMetadata.validationFailure(
                    "Field value type must be one of " + FIELD_VALUE_TYPE_VALID);
            }
        }

        if ( m_parameter != null )
        {
            // parameter requires DS 1.4
            if ( !dsVersion.isDS14() )
            {
                throw componentMetadata.validationFailure( "Reference parameter requires DS >= 1.4" );
            }
            try
            {
              	m_parameterIndex = Integer.valueOf(m_parameter);
            }
            catch ( final NumberFormatException nfe)
            {
                throw componentMetadata.validationFailure( "Reference parameter is not a number: " + m_parameter );
            }
            if ( m_parameterIndex < 0 )
            {
                throw componentMetadata.validationFailure( "Reference parameter value must be zero or higher: " + m_parameter );
            }
            // parameter value type
            if (m_collection_type == null)
            {
                setCollectionType(FIELD_VALUE_TYPE_SERVICE);
            }
            else if (!FIELD_VALUE_TYPE_VALID.contains(m_collection_type))
            {
                throw componentMetadata.validationFailure(
                    "Collection value type must be one of " + FIELD_VALUE_TYPE_VALID);
            }
        }
        m_validated = true;
    }