Static import constructs
The static import constructs, introduced in J2SE 5.0, allow unqualified access to static members of a class. Rhapsody® models static imports and generates appropriate code. In addition, the reverse engineering feature can handle static imports in Java code, and the roundtripping feature can handle changes to static import statements.
Rhapsody allows
you to model both static import of individual class members (import static
java.lang.Math.PI
) and static import of all static members of a class (import
static java.lang.Math.*
).
Modeling of static imports is based on use of the StaticImport stereotype in the PredefinedTypesJava package. The StaticImport stereotype inherits from the Usage stereotype.
Adding static imports to a model
- Create a dependency in the browser or by drawing a dependency in an object model diagram. The dependency can be from a class to a class or from a class to an individual static attribute or operation.
- Open the Features window for the dependency you created, and apply the StaticImport stereotype to it.
When you next generate code, the code for the dependant class contains the appropriate static import statement.
StaticImport dependency
If you create a StaticImport dependency between a class and an attribute or method that is not static, Rhapsody generates the corresponding static import statement but it issues a warning that you have specified a static import for a non-static class member.
Handling of static imports in reverse engineering and roundtripping
If you reverse engineer code that contains static import statements, Rhapsody creates dependencies that have the StaticImport stereotype applied to them.
The roundtripping feature can handle the addition of static import statements to your code, as well as changes to static import statements, including switching regular import statements to static import statements, and the reverse.
If you delete static import statements from your code, the roundtripping behavior depends upon
the value of the JAVA_Roundtrip::Update::AcceptChanges
property.