4.3 Transformations catalog

 

The design of the catalog for a transformation in this work and the implementation of transformations for DPT-tool separates the preconditions as described above. The source code language of the program for the transformation  is Java (1.1)

 

4.3.1 Low-Level refactorings

 

Low-level transformations define some simple code manipulation routines for standard program entities like classes, methods or variables. Standard operations for the program entities are:

·      Create new entity

·      Delete entity

·      Copy entity

·      Move entity

·      Rename entity

Although the implementation can be very complex (e.g. „rename entity") for the user it should look simple, otherwise it wouldn't be a low-level transformation. It is better to implement many simple transformations instead of one huge „monster".

 

Example:  „Copy"-operation for a program-entity shouldn't change access rights or the context of the copied entity! All additional features can be done later, or can be implemented in separate transformations, or can be completed by the user in one „composed transformation" (See section 4.4 „Composite transformation")

 

4.3.1.1 Creational transformations

 

Creational transformations insert a new entity into the program. It can be a class, a variable, or a method. These kind of code-wizards are very popular in many development tools. Although the creational  transformations are not very useful as „stand alone„ refactorings (programmers would prefer to type a new entity) they are necessary for the composition of more powerful and complex transformations.

 

Because the programmer readiness to use creational transformations is low, the user-interaction should be as simple as possible. This fact affects the definition and implementation of creational refactorings. Some of  the refactoring books[Opdyke92] have definitions like the following "Create class"- transformation:

 

1) Get class name and modifiers.

2) Get list of implemented interfaces

3) Get super class

4) Get list of derived classes

5) Get class constructors

6) Get list of instance variables

7) Get list of class methods

8) Check and create class

 

This definition is correct but the implementation would be useless because programmer will get tired of filling out all of those parameters and if the transaction fails (for example, if the class-name already exists) the user will never use it again.

 

Conclusion:    Reduce the „great expectations" for the new program entity

and let the user define additional features later.

 

Here is the new „create class"- transformation :

1) Get class name and modifiers.

2) Check and create class

 

If the user wants a super-class or interfaces for the new class he can use „specialization"

or „implements" -transformations which also create a new class but with expected extensions. Another possibility for the user is to create a new class and to add the new features to this class later (see „Inherit-plain" Chapter 4.3.3 and chapter 4.4 „Composed transformations“).

 

4.3.1.1.1 New class

 

Description:    Transformation will create a new class

                        with no interfaces or super class

                        with no locally defined variables

                        with no locally defined methods

Optional:        Default-constructor(with no parameters) can be created.

                        New class can be an interface

Conditions:    Program does not have this class.

DPT:              DPT checks the condition. 

Parameters:    String (full class name), String (modifier),

                        int Option(creates default constructor/creates interface)

Output:           String (class name). Output can be used in composite-transformation.

Program:        New class will be added, no instances. Behavior does not change.

 

(Figure 4.1 GUI: DTP-Dialog „Assistant“)

4.3.1.1.2 New method

 

Description:   Transformation will create a new method for a given class

                        with no exceptions, with no locally defined variables

                        with default body(if method has type->default return statement).

                        An identical super-class method will be handled as overridden.

Optional:        Method-parameters.

Conditions:    1) Program does not have this method-name(full path + header).

                            Same as: class does not have this method

                        2) Return-type and parameters are valid classes or basic types.

(minor)            3) Check: New method is abstract and class is real

 (minor)           4) Check: Class is interface and has implementers

            5) Check: Class is abstract or interface (no body for method)

DPT :              1) DPT checks the condition.

                        2) DPT does not check type of parameters or return-type!

(Parameter can be an included class(e.g. String) and its implementation is not available.)

                        3) DPT asks user to determine creation of abstract method in real class

                        4) DPT asks user to determine creation of new method

5) DPT asks user to determine creation of new method

                        in abstract class or interface.

Parameters:    String (full-name), String (modifier), String (type),Vector(parameters)

Output:           String (method name).Output can be used in composite transformation.

Program:        New method will be added.

Default: (method is not an override-method, class is not an interface)

                        No method-calls exist. Behavior does not change.

                        If minor condition is not accomplished:

-Class is an interface with existing implementers:

                        Error: subclasses have to be abstract or implement new method.

- Method is an override-method with existing calls of the method.

                        Behavior changes. Method-body has to be implemented.

                        (Example: Figure 4.1 shows the DPT-Assistant for this transformation )

 

4.1.1.1.3 New variable

 

Description:   Transformation will create a new variable with no references

 Optional:       Default-initialization can be created.

                        Access methods can be created(get/set)

Conditions:    1) Program does not have this variable-name(full path).

                            same as: class does not have this member

                        2) Variable-type is a valid class or basic type.  

DPT:              1) DPT checks the condition. 

                        2) DPT checks the type of variable

                           If type is basic -> basic type initialization.

                           If type is class and it has default constructor -> default initialization

                           If type is unknown class(e.g. unreachable import) -> null-pointer

Parameters:    String (full variable name), String (modifier),

                        int Option(create access functions/initialization)

Output:           String (variable). Output can be used in composite-transformation.        

Program:        New variable will be added, no references. Behavior does not change.

 

4.3.1.2 Remove- transformations

 

Remove transformations delete an existing entity from the program. The removable entity can be a class, a variable or a method. In opposition to creational operations, the remove-transformation may cause errors if the entity has references. To preserve the behavior of the program the entity must be unreferenced. One of the problems is in determining when an entity is really unreferenced and sometimes the user may want to remove a referenced entity, too! There are several special cases for the derived methods and variables to demonstrate this problem.

Example: If an unreferenced method implementation(declared in interface) will be removed from an abstract class the program will work fine, but if the class isn’t abstract, then the compiler will miss the implementation of the method declared in the interface.

 

4.3.1.2.1Remove class

 

Description:   Transformation will remove an existing class

Conditions:    Program does not have references to this class.

DPT:               DPT checks the condition. 

Parameters:    String (full class name)

Output:           None.

Program:        Behavior does not change if condition is accomplished.

Note:                Compiler dependent program-behavior if the class is the only one class

                        in the package and the package is imported by other classes.

 

4.3.1.2.2 Remove method

 

Description:   Transformation will remove an existing method from a given class

Conditions:    Program does not have references to this method.

            Method is not an implementation of interface method

DPT :              DPT does not check the conditions.

                        Reason: DPT-Parser can't resolve all references to this member correctly

Parameters:    String (full method name)

Output:           String (class name). Output can be used in composite-transformation.

Program:        Behavior does not change if condition is accomplished.

Note:                One of the problems with transformation: "remove set of methods" is that

it is very difficult to check internal references of "to be deleted" methods and this procedure depends also on the input-sequence

 

4.3.1.2.3 Remove variable

 

Description:   Transformation will remove an existing variable from a  class

Conditions:    Program does not have references to this variable.

DPT :              DPT does not check the condition.

                        Reason: DPT-Parser can't resolve all references to this member correctly

Parameters:    String (full variable name)

Output:           String (class name). Output can be used in composite-transformation.

Program:        Behavior does not change if condition is accomplished.

 

4.3.1.3 Copy - transformations

 

Copy transformations create a copy of an entity for a different entity owner without changing the entity. An interesting precondition of this operation is that the entity shouldn't reference any members of the current owner. Although the entity could have those references the user may want to copy this entity.

 

Example: The user wants to copy a method in a class in another package. The method references current package specific import. Following the "preserved behavior"-rule transformation will break on precondition-check. It would be much more useful to inform the user about this problem and let the user decide what to do.

 

Copy transformation is different than "creational-" and "remove-" transformation because it concerns about the target-owner.

 

Example: Transformation "copy a method" from class to interface won’t copy the method-body. Transformation "copy a method" to class would prompt user if method is  abstract and new owner(class/interface) is not.

 

4.3.1.3.1 Copy class

 

Description:   Transformation will copy an existing class to target package

Conditions:    1) Target package exists and does not have this class.  

                        2) Class does not have current package-specific internal references.

DPT:               1) DPT checks the condition.

                        2) DPT checks the condition, if not accomplished, prompts for next step.

Parameters:    String (target package), String (class name)

Output:           String (new class). Output can be used in composite-transformation.

Program:        New unreferenced class with no instance will be added to program. 

                        Behavior does not change if conditions are accomplished.

(Figure 4.2 UML: Copy class)

 

4.3.1.3.2 Copy method

 

Description:   Transformation will copy an existing method to target class

Conditions:    1) Target class exists and does not have this method.    

 (minor)           2) Check: method is abstract and target class is not (or interface)

 (minor)           3) Check: target class is an Interface or abstract and method is not

                        4) Method does not have current class-specific internal references.

DPT:               1) DPT checks the condition.

                        2) DPT will prompt the user if this condition is not accomplished.

                        3) DPT will prompt the user if this condition is not accomplished.

                        4) DPT does not check the condition.

                        Reason: DPT-Parser can't resolve all references to this member correctly

Parameters:    String (target class), String (full method name)

Output:           String (new method). Output can be used in composite-transformation.

Program:        New method will be added to the program, no calls to the method.

Behavior does not change if conditions are accomplished.

 

4.3.1.3.3 Copy variable

 

Description:   Transformation will copy an existing variable to target class

Conditions:    1) Target class exists and does not have this variable.

                        2) Check: target class is an interface.

                        3) Variable does not have current class-specific internal references.

DPT:               1) DPT checks the condition.

                        2) DPT will prompt user if this condition is not accomplished.

                        3) DPT does not check the condition.

                        Reason: DPT-Parser can't resolve all references to this member correctly

Parameters:    String (target class), String (full variable name)

Output:           String (new variable).Output can be used in composite-transformation.

Program:        New unreferenced variable will be added to class.

                        Behavior does not change if condition is accomplished.