uCosminexus Application Server, EJB Container Functionality Guide
Generics is the functionality that can be used with J2SE 5.0 or later. When you use different objects with types or methods, you can use Generics to guarantee safety of types at the compile time. You can also use Generics to create a program for handling types as parameters, irrespective of the data types.
A type that is changed to a parameter is referenced as a type parameter. A type parameter is used for declaring classes, interfaces, methods, or a constructor using Generics. For the definition List<E> extends Collection<E>, <E> corresponds to a type parameter.
Also, the specification of a specific parameter type for a class, interface, method, or constructor using Generics is called parameterization. For example, "List<String>" and "Collection<Integer>" are parameterized classes.
With interfaces, type parameters can be used in method parameters, returns, and throws. The type parameters to be used are declared in the interface definition.
The following table describes the types of interfaces in which type parameters can be used for each Enterprise Bean type:
Table 4-5 Types of interfaces in which type parameters can be used
Type of interface | Enterprise Bean type | ||
---|---|---|---|
Session Bean | Entity Bean | Message-driven Bean | |
Business interface | P | -- | -- |
Home interface | N | N | -- |
Component interface | N | N | -- |
Message listener interface | -- | -- | Y |
Others (any arbitrary interface) | Y | Y | Y |
When a type parameter, defined in an interface, is parameterized in a business interface and the method of the parameterized business interface is redefined, an error might occur based on the combination of the location, wherein the type parameter is used, and the interface type. If an error occurs, take action by deleting the redefined method.
The following table describes the combinations that might result in an error, when a method is redefined in the business interface:
Table 4-6 Combination resulting in an error when a method is redefined in the business interface
Location wherein the type parameter is used | Local interface | Remote interface |
---|---|---|
Returns | Y | Y |
Parameters | -- | -- |
Throws | Y | Y |
An error occurs when both the following conditions are applicable:
The following is a coding example to show the occurrence of an error. In the following example, the definition of the method String get(Float args) must be deleted from the definition of MyInterface to recover from the error:
public interface SuperInterface<T> { String get(T args); } // Redefine the method in which the parameterized SuperInterface is inherited. public interface MyInterface extends SuperInterface<Float> { // To recover from the error, the following redefined method definition must be deleted. String get(Float args); } |
With classes, type parameters can be used within the method and the method signature.
The following table describes the types of classes in which type parameters can be used for each Enterprise Bean type:
Table 4-7 Types of classes in which type parameters can be used
Type of class | Enterprise Bean type | ||
---|---|---|---|
Session Bean | Entity Bean | Message-driven Bean | |
Enterprise Bean class | Y | Y | Y |
Interceptor class | Y | -- | -- |
Others (any arbitrary class) | Y | Y | Y |
You cannot specify an annotation# corresponding to Application Server in a generic method and a generic constructer. If you specify such an annotation, the annotation is ignored.
An example of a generic method and a generic constructor is given below.
# For details on annotations corresponding to Application Server, see 2.1 Supported range of corresponding applications in the uCosminexus Application Server API Reference Guide.
class MyClass{ // Generic constructor <T1> MyClass(Collection<T1> c){}; // Generic method <T2> void MyMethod(Collection<T2> c){}; } |
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.