uCosminexus Application Server, EJB Container Functionality Guide

[Contents][Glossary][Index][Back][Next]

4.2.19 Precautions related to use Generics

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.

Organization of this subsection
(1) Interfaces in which type parameters can be used
(2) Classes in which type parameters can be used
(3) Other precautions

(1) Interfaces in which type parameters can be used

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.

(a) Types of interfaces in which type parameters can be used

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

Legend:
Y: Can be used.
P: Can be used. However, a declared type parameter must be parameterized in the business interface. If the declared type parameter is not parameterized, either an error will occur or the operation will not be guaranteed.
N: Cannot be used.
--: Cannot be used (Java EE specifications).

(b) Precautions when a type parameter is used in a business interface

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

Legend:
Y: The application is started successfully.
--: An error occurs when starting the application.

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);
}
 

(2) Classes in which type parameters can be used

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

Legend:
Y: Can be used.
--: Cannot be used (Application Server does not support this combination).

(3) Other precautions

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){};
 
}