Vous êtes sur la page 1sur 8

ABAP Keyword Documentation

Enhancements Using BAdIs


GET BADI
CALL BADI

Enhancements Using BAdIs


BAdIs are created in Enhancement Builder as a part of the enhancement concept of ABAP Workbench. These
BAdIs, together with their calling points in ABAP programs, form explicit enhancement options of these
programs are are assigned to enhancement spots.
If BAdIs and their calling points are defined in a system, enhancements can be made in follow-on systems by
creating BAdI implementations in these systems. A BAdI consists mainly of a BAdI implementation class whose
instances enhance the functions of an ABAP program at runtime in the form of an object plug-in. In the
enhancement concept, a BAdI implementation is an enhancement implementation element and is
administrated by enhancement implementations.
A BAdI consists mainly of a BAdI interface that contains the declaration of BAdI methods, and a set of filters.
The filters enable certain BAdI implementations to be selected for use. The following settings can be made in
the definition of a BAdI, in addition to the selection of the BAdI interface:

Whether a BAdI is intended for single or multiple use. If a BAdI is intended for single use, only a single
BAdI implementation can be used in one internal session, and one BAdI implementation must be
available for each use.

A fallback BAdI implementation class can be specified to be used if no BAdI implementation matches
the filter conditions.

A BAdI can be defined as context-free or context-dependent. This setting controls the instantiation of
the object plug-ins. In context-free BAdIs, it is possible to define whether an object plug-in can be
generated only once or multiple times within an internal session. For context-dependent BAdIs, the
object plug-ins are associated with a context object.

Unlike earlier BAdIs, the new BAdIs are supported directly by the ABAP runtime environment with the following
ABAP statements:

GET BADI
Generates a BAdI object as a handle for object plug-ins.

CALL BADI
Calls BAdI methods in object plug-ins.

The BAdIs of the enhancement concept are also freely switchable using switches from Switch Framework.
Notes

For a BAdI intended for single use, it is best to always enter a fallback implementation class and to
implement it in the same system as the BAdI.

See also Enhancements with BAdIs.

GET BADI
Short Reference

Syntax
GET BADI { badi [FILTERS f1 = x1 f2 = x2 ...] }
| { badi TYPE (name)
[ {FILTERS f1 = x1 f2 = x2 ...}
| {FILTER-TABLE ftab}] }
[CONTEXT con].

Extras:
1. ... FILTERS f1 = x1 f2 = x2 ...
2. ... FILTER-TABLE ftab
3. ... CONTEXT con
Effect
This statement generates a new BAdI object and sets the BAdI reference to the object in a BAdI reference
variable badi. This statement has a static and a dynamic variant.

In the case of the static variant, addition TYPE is not declared. The static type of the reference variable
badi must be a BAdI. This determines which BAdI will be used.

In the case of the dynamic variant, a character-like data object name is declared for addition TYPE.
When the statement is executed, this object must contain the name of a BAdI. The static type of the
reference variable badi must be a superclass for all CL_BADI_BASE BAdI classes.

To declare values for the BAdI's filters, you can declare the addition FILTERS in the static variant, or addition
FILTER-TABLE in the dynamic variant.
In the case of a context-free BAdI, addition CONTEXT cannot be declared. In the case of a context-specific
BAdI, however, it must be declared.
After instancing, the program searches for BAdI implementation classes for the BAdI as follows:

1.

Only BAdI implementation classes are selected that are assigned to a BAdI implementation that has
the status active.

2.

Only BAdI implementation classes are selected that are assigned to a BAdI implementation (an
enhancement) that have a switch in the status on. If no switch is assigned to a BAdI implementation, it
is treated in the same way as an implementation with a switch in the status on.

3.

All BAdI implementations are selected that satisfy the above requirements and for which the filter
condition of the BAdI implementation matches the values declared after FILTERS or in ftab. If no
BAdI implementations are found with the filter specifications, the system searches for BAdI
implementations that are marked as standard implementations. If there are also none of these, the
fallback BAdI implementation class of the BAdI is used (if one exists).

4.

If, for a BAdI defined for single use, multiple BAdI implementation classes with matching filter
conditions have been found, then a conflict solution is carried out. A successful conflict solution results
in exactly one priority conflict-solving enhancement implementation and only its BAdI implementation
class is selected. If several conflict-solving enhancement implementations have the same priority or if
there are no conflict-solving enhancement implementations at all, then a correct conflict solution is not
possible and several BAdI implementation classes will remain in the hit list.

If the resulting hit list is suitable for the definition of the BAdI which means:

For a BAdI defined for a single use, the hit list must contain exactly one BAdI implementation class,

For a BAdI defined for multiple use, the hit list may contain several or no BAdI implementation classes,

then references to object plug-ins of the found BAdI implementations are saved in the BAdI object. Object plugins that do not yet exist are generated for this purpose. Whether an existing object plug-in is generated or
reused, or whether a new object plug-in is generated, depends on whether the BAdI is context-free or contextdependent, and depends on the CONTEXT addition. The precise semantics are described in the addition.
If the hit list contains no or multiple BAdI implementations although the BAdI is defined for single use, an
exception occurs. If the BAdI reference variable badi is given a valid BAdI reference before the statement in
an exception case, this is retained, otherwise it is initialized.
Notes

It is not possible to access BAdI objects directly using BAdI references. The references are only used
to call the BAdI methods in the referenced object plug-ins with the statement CALL BADI. Otherwise,
BAdI reference variables can be used in the same operand positions as normal object reference
variables. In particular, this means that assignments and comparisons are possible.

The method NUMBER_OF_IMPLEMENTATIONS of the class CL_BADI_QUERY returns the number


of BAdI implementations that are saved in a BAdI object.

The statement GET BADI prepares an enhancement spot invocation, and can therefore be seen as
part of an explicit enhancement option in the terminology of the enhancement concept.

To prevent the exception for BAdIs that are defined for single use in systems in which no
corresponding enhancement is carried out, it is recommended to specify a fallback BAdI
implementation class for these BAdIs. The fallback BAdI implementation class belongs to the BAdI and
is independent of enhancements.

To access object plug-ins directly requires the corresponding references. These can be supplied, for
example, by BAdI methods.

The BAdI reference cannot be used to handle any instance events of a BAdI. Instead, when the event
handler is registered, either the addition FOR ALL INSTANCES must be specified using SET
HANDLER, or an reference to an object plug-in must be entered directly.

Addition 1
... FILTERS f1 = x1 f2 = x2 ...

Effect
After addition FILTERS, the elementary data objects x1, x2, ... must be declared for all f1, f2, ... filters of the
BAdI. If a filter in the BAdI has the property Constant Filter Value at Call, only literals and constants can be
declared. The data objects must be compatible with the data type of the filters. FILTERS cannot be declared
for a BAdI without filters.

Addition 2
... FILTER-TABLE ftab

Effect
Using the special internal table ftab, this addition assigns values to the filters of a dynamically declared BAdI.
The internal table must have table type BADI_FILTER_BINDINGS from ABAP Dictionary. When statement GET
BADI is executed, the table must contain exactly one row for each of the BAdI's filters. The table columns are:

NAME of type c and length 30


for the name of the filter in uppercase letters. Declaring a nonexistent filter raises a non-handleable
exception.

VALUE of type REF TO data


A reference to a matching data object. The value of the data object that the reference variable in
VALUE refers to is assigned to the filter declared in NAME.

The column NAME is the unique key of the sorted table ftab.

Addition 3
... CONTEXT con

Effect
Using the addition CONTEXT, for context-dependent BAdIs, an object reference variable con must be specified,
the static type of which is, or includes, the tag interface IF_BADI_CONTEXT, and which contains a reference to

a BAdI context object. If con is initial, an exception is raised. The addition CONTEXT cannot be entered for
context-free BAdIs.
The addition CONTEXT controls the generation of the object plug-ins as follows:

Without the addition CONTEXT, hence for context-free BAdIs, the generation of the object plug-ins is
based on the setting of the BAdI. Either new plug-ins are generated every time the statement GET
BADI is executed, or an object plug-in that has already been generated in the current internal session
is reused, if it is required again. An object plug-in of this type is a singleton in terms of its BAdI
implementation class.

With the addition CONTEXT, and hence for context-dependent BAdIs, the same object plug-ins are
always used for the same content of con. These object plug-ins are singletons in terms of their BAdI
implementation class and a BAdI context object.

If a BAdI implementation class implements multiple BAdI interfaces and GET BADI is executed within one
internal session for several of these BAdIs, then multiple BAdI objects can point to the same object plug-in.
This enables the sharing of data between different BAdIs. For context-free BAdIs, this is only the case for
reuse. For context-dependent BAdIs, multiple BAdI objects from the same context can point to the same object
plug-ins.
Notes

The fact that object plug-ins can either be newly generated or reused can also be expressed in terms
of an object plug-in being stateful or stateless with reference to a BAdI or a context.

The tag interface IF_BADI_CONTEXT can be implemented by any class. Possible applications range
from the implementation in a local class without other components that have the sole purpose of
enabling internal program context administration for BAdIs, to the implementation in a global
application class in which the objects are specifically enhanced.

Exceptions
Catchable Exceptions
CX_BADI_CONTEXT_ERROR

Cause: A context error has occurred in the dynamic variant of the statement.

CX_BADI_FILTER_ERROR

Cause: Incorrect information was specified regarding the BAdI's filters in the dynamic variant of the
statement.

CX_BADI_INITIAL_CONTEXT

Cause: The reference variable declared after CONTEXT is initial.

CX_BADI_MULTIPLY_IMPLEMENTED

Cause: Multiple BAdI implementation classes were found, although the BAdI is defined for single use.
Subclass of CX_BADI_NOT_SINGLE_USE.

CX_BADI_NOT_IMPLEMENTED

Cause: No BAdI implementation class was found, although the BAdI is defined for single use.
Subclass of CX_BADI_NOT_SINGLE_USE.

CX_BADI_UNKNOWN_ERROR

Cause: The BAdI declared in the dynamic variant does not exist.

CALL BADI
Short Reference

Syntax
CALL BADI { badi->meth
parameter_list }
| { badi->(meth_name) {parameter_list
|parameter_tables} }.
Effect
Calls a BAdI method. The statement has a static and a dynamic variant. In both variants a BAdI reference
variable must be specified for badi.

In the static variant, a BAdI reference variable of the static type of a concrete BAdI class must be
specified for badi, and for meth a BAdI method of the corresponding BAdI must be entered directly.

In the dynamic variant, a BAdI reference variable of the static type of the abstract super class
CL_BADI_BASE must be specified for badi. For meth_name, a character-like data object must be
specified that must contain the name of a BAdI method in uppercase when the statement is executed.

With regards to the addressing of BAdI methods, the BAdI reference variable behaves like an interface
reference variable with the static type of the affected BAdI interface. A BAdI method declared as a component
of the corresponding BAdI interface is addressed directly using its name. BAdI methods declared in component
interfaces of the BAdI interface can be addressed using the interface component selector or any alias names.
The additions parameter_list or parameter_tables assign actual parameter to the formal parameters of
the BAdI method or handle non-class-based exceptions exactly as described in CALL METHOD. The exceptions
that can occur in dynamic accesses are also the same as those of CALL METHOD.
The effect of the statement CALL BADI is that the specified method is called in all object plug-ins to which the
BAdI object referenced by the badi refers.

If the BAdI is defined for single use, then badi must contain a valid BAdI reference for a static BAdI
call. If badi is initial, a handleable exception is raised.

If the BAdI is defined for multiple use, then badi must contain a valid BAdI reference or can be initial
for a static BAdI call. If the referenced BAdI object refers to multiple object plug-ins ,then the call order
is the same for every CALL BADI statement. The exact call order can be determined in the definition
of the corresponding BAdI implementations, if the predefined BAdI BADI_SORTER of the
enhancement spot of the same name was implemented for the current BAdI. If the referenced BAdI
object does not reference object plug-ins, or the badi is initial, then the statement has no effect.

In a dynamic BAdI call, a handleable exception is always raised if there is an invalid BAdI reference in
badi.

If a method is added to a BAdI afterwards, it can be missing in a BAdI implementation. In this case the call is
executed as if the method existed with an empty implementation. Actual parameters that are bound to
EXPORTING or RETURNING parameters passed by value are initialized. All other actual parameters remain
unchanged.
System Fields
As for a regular CALL METHOD, the system field sy-subrc is either set to 0 or, when handling non-classbased exceptions, it is set to the value specified after EXCEPTIONS.
Notes

BAdI objects and BAdI references can only be generated using the statement GET BADI.

In the terminology of the enhancement concept, the statement CALL BADI is known as an
enhancement spot element invocation.

The call of a BAdI method of a BAdI defined for single use behaves as a method call with CALL
METHOD: the called method must exist. In contrast, the call of a BAdI method defined for multiple use
rather corresponds to the triggering of an event with RAISE EVENT: One or more methods can exist,
or no methods at all.

Exceptions
Catchable Exceptions
CX_BADI_INITIAL_REFERENCE

Cause: The reference variable badi is initial either for the static call, although the BAdI was defined
for single use, or for the dynamic call.

CX_SY_DYN_CALL_ILLEGAL_METHOD

Cause: Method does not exist at the dynamic call


Runtime Error: DYN_CALL_METH_NOT_FOUND

Vous aimerez peut-être aussi