Skip to content

Latest commit

 

History

History
102 lines (66 loc) · 5.5 KB

inheritance.asciidoc

File metadata and controls

102 lines (66 loc) · 5.5 KB

Inheritance

A bean may inherit type-level metadata and members from its superclasses.

Inheritance of type-level metadata by beans from their superclasses is controlled via use of the Java @Inherited meta-annotation. Type-level metadata is never inherited from interfaces implemented by a bean.

Member-level metadata is not inherited. However, injected fields, initializer methods, lifecycle callback methods and non-static observer methods are inherited by beans from their superclasses.

The implementation of a bean may be extended by the implementation of a second bean. This specification recognizes two distinct scenarios in which this situation occurs:

  • The second bean specializes the first bean in certain deployment scenarios. In these deployments, the second bean completely replaces the first, fulfilling the same role in the system.

  • The second bean is simply reusing the Java implementation, and otherwise bears no relation to the first bean. The first bean may not even have been designed for use as a contextual object.

The two cases are quite dissimilar.

By default, Java implementation reuse is assumed. In this case, the two beans have different roles in the system, and may both be available in a particular deployment.

The bean developer may explicitly specify that the second bean specializes the first. Then the second bean inherits, and may not override, the qualifiers and bean name of the first bean. The second bean is able to serve the same role in the system as the first. In a particular deployment, only one of the two beans may fulfill that role.

Specialization is only present in {cdi_full} and is specified therein.

Inheritance of type-level metadata

Suppose a class X is extended directly or indirectly by the bean class of a managed bean Y.

  • If X is annotated with a qualifier type, stereotype or interceptor binding type Z then Y inherits the annotation if and only if Z declares the @Inherited meta-annotation and neither Y nor any intermediate class that is a subclass of X and a superclass of Y declares an annotation of type Z. (This behavior is defined by the Java Language Specification.)

  • If X is annotated with a scope type Z then Y inherits the annotation if and only if Z declares the @Inherited meta-annotation and neither Y nor any intermediate class that is a subclass of X and a superclass of Y declares a scope type. (This behavior is different to what is defined in the Java Language Specification.)

A scope type explicitly declared by X and inherited by Y from X takes precedence over default scopes of stereotypes declared or inherited by Y.

For annotations defined by the application or third-party extensions, it is recommended that:

  • scope types should be declared @Inherited,

  • qualifier types should not be declared @Inherited,

  • interceptor binding types should be declared @Inherited, and

  • stereotypes may be declared @Inherited, depending upon the semantics of the stereotype.

All scope types, qualifier types, and interceptor binding types defined by this specification adhere to these recommendations.

The stereotypes defined by this specification are not declared @Inherited.

However, in special circumstances, these recommendations may be ignored.

Note that the @Named annotation is not declared @Inherited and bean names are not inherited unless specialization is used.

Inheritance of member-level metadata

Suppose a class X is extended directly or indirectly by the bean class of a managed bean Y.

  • If X declares an injected field x then Y inherits x. (This behavior is defined by the Common Annotations for the Java Platform specification.)

  • If X declares an initializer, non-static observer, @PostConstruct or @PreDestroy method x() then Y inherits x() if and only if neither Y nor any intermediate class that is a subclass of X and a superclass of Y overrides the method x(). (This behavior is defined by the Common Annotations for the Java Platform specification.)

  • If X declares a non-static method x() annotated with an interceptor binding type Z then Y inherits the binding if and only if neither Y nor any intermediate class that is a subclass of X and a superclass of Y overrides the method x(). (This behavior is defined by the Common Annotations for the Java Platform specification.)

  • If X declares a non-static producer or disposer method x() then Y does not inherit this method. (This behavior is different to what is defined in the Common Annotations for the Java Platform specification.)

  • If X declares a non-static producer field x then Y does not inherit this field. (This behavior is different to what is defined in the Common Annotations for the Java Platform specification.)

If X is a generic type, and an injection point or observer method declared by X is inherited by Y, and the declared type of the injection point or event parameter contains type variables declared by X, the type of the injection point or event parameter inherited in Y is the declared type, after substitution of actual type arguments declared by Y or any intermediate class that is a subclass of X and a superclass of Y.

For example, the bean DaoClient has an injection point of type Dao<T>.

public class DaoClient<T> {

    @Inject Dao<T> dao;
    ...

}

This injection point is inherited by UserDaoClient, but the type of the inherited injection point is Dao<User>.

public class UserDaoClient
        extends DaoClient<User> { ... }