Developing Java Code
Use the code entry tabs in the Java view to write and compile Java code that defines transformation behavior for specific transformation events.
You can develop code snippets on the code entry tabs in any order. You can view, but not edit, the full Java code on the Full Code tab.
After you develop code snippets, you can compile the code snippets or the full Java code and view the results of the compilation in the Results window in the Compilation properties in the Java view.
Each code entry tab contains components that you use to write, view, and compile Java code:
- Code properties
- Provides controls that let you view and enter Java code, including Java transformation API methods. The following table describes the controls that are available in Code properties:
Control | Description |
---|
Navigator | Shows input ports, output ports, and callable Java transformation API methods. Click an item in the navigator to display a description of the item. Double-click an item to add it to the Java code window. Alternatively, you can drag an item from the navigator into the Java code window. The navigator is available on the following code entry tabs: - - Helpers
- - On Input
- - At End
|
Java code window | Enables you to view or enter Java code for the transformation. The Java code window displays Java code by using the basic Java syntax highlighting. The Java code window is available on the following code entry tabs: - - Imports
- - Helpers
- - On Input
- - At End
- - Functions
- - Optimizer Interfaces
- - Full Code
|
New Function command | Opens the Define Function dialog box that you use to define functions that invoke Java expressions. The Function command is available on the Functions tab. |
Editing toolbar | Enables you to click tool icons, such as cut, copy, and paste, to edit Java code. The editing toolbar is available on the following code entry tabs: - - Imports
- - Helpers
- - On Input
- - At End
- - Functions
- - Optimizer Interfaces
|
- Compilation properties
- Provides controls to compile and debug Java code. The following table describes the controls in the Compilation properties:
Control | Description |
---|
Compile command | Compiles the Java code for the transformation. |
Results window | Displays the compilation results for the Java transformation class and enables you to find the source of errors in the code. To find an error in the code, right-click an error message in the Results window and select to view the error in the snippet code or in the full code. You can also double-click an error message in the Results window to find its source. |
Creating Java Code Snippets
To create Java code snippets to define transformation behavior, use the Java Code window on the code entry tabs.
1. Click the appropriate code entry tab.
The following table describes the tasks that you can complete on the code entry tabs in the Java view:
Tab | Description |
---|
Imports | Imports third-party, built-in, and custom Java packages for an active or passive Java transformation. After you import packages, you can use them on the other code entry tabs. |
Helpers | Declares user-defined variables and methods for the Java transformation class in an active or passive Java transformation. After you declare variables and methods, you can use them in any other code entry tab except the Imports tab. |
On Input | Defines how an active or passive Java transformation behaves when it receives an input row. The Java code that you define on this tab runs one time for each input row. On this tab, you can also access and use input and output port data, variables, and Java transformation API methods. |
At End | Defines how an active or passive Java transformation behaves after it processes all input data. On this tab, you can also set output data for active transformations, and call Java transformation API methods. |
Functions | Defines functions that invoke expressions in a Java transformation with the Java programming language. For example, you can define a function that invokes an expression that looks up the values of input or output ports or looks up the values of Java transformation variables. On the Functions tab, you can manually define functions or click New Function to invoke the Define Function dialog box, which enables you to easily define a function. |
Optimizer Interfaces | Defines early selection or push-into filter optimization. Select the optimization method in the navigator. Update the code snippets to enable the optimization. Define the input ports and associated output ports to push filter logic through. |
Full Code | Read-only. On this tab, you can view and compile the full class code for the Java transformation. |
2. To access input or output column variables in the snippet, expand the Input or Output list in the navigator and double-click the name of the port.
3. To call a Java transformation API in the snippet, expand the Callable APIs list in the navigator and double-click the name of the method. If necessary, configure the appropriate input values for the method.
4. Write appropriate Java code, based on the code entry tab type.
View the full class code for the Java transformation in the Java code windowon the Full Code tab.
Importing Java Packages
On the Imports tab, you can import Java packages for active or passive Java transformations.
You can import third-party, built-in, or custom Java packages. After you import Java packages, you can use the imported packages on the other code entry tabs.
Note: On the Imports tab, you cannot declare or use static variables, instance variables, or user methods.
In the Developer tool, when you export or import metadata that contains a Java transformation, the jar or class files that contain the third-party or custom packages required by the Java transformation are not included in the export or import.
If you import metadata that contains a Java transformation, you must copy the jar or class files that contain the required third-party or custom packages to the Developer tool client and Data Integration Service node.
For example, to import the Java I/O package, enter the following code on the Imports tab:
import java.io.*;
When you import non-standard Java packages, add the package or class to the classpath in the Java transformation.
Defining Helper Code
On the Helpers tab, you can declare user-defined variables and methods for the Java transformation class in active or passive Java transformations.
After you declare variables and methods on the Helpers tab, you can use the variables and methods on any code entry tab except the Imports tab.
On the Helpers tab, you can declare the following types of code, variables, and methods:
- •Static code and static variables.
Within a static block, you can declare static variables and static code. All instances of a reusable Java transformation in a mapping share static code and variables. Static code runs before any other code in a Java transformation.
For example, the following code declares a static variable to store the error threshold for all instances of a Java transformation in a mapping:
static int errorThreshold;
Use this variable to store the error threshold for the transformation and access it from all instances of the Java transformation in a mapping.
- •Instance variables.
Multiple instances of a reusable Java transformation in a mapping do not share instance variables. Declare instance variables with a prefix to avoid conflicts and initialize non-primitive instance variables.
For example, the following code uses a boolean variable to decide whether to generate an output row:
// boolean to decide whether to generate an output row
// based on validity of input
private boolean generateRow;
- •User-defined static or instance methods.
Extends the functionality of the Java transformation. Java methods declared on the Helpers tab can use or modify output variables or locally declared instance variables. You cannot access input variables from Java methods on the Helpers tab.
For example, use the following code on the Helpers tab to declare a function that adds two integers:
private int myTXAdd (int num1,int num2)
{
return num1+num2;
}