新しい Java クラスの作成
- 最終更新日2022/05/19
IntelliJ を使用して、新しい Java クラスと新しいディレクトリを作成し、他のビルド ファイルを構成します。
前提条件
以下のタスクの手順を完了します。ビルド ファイルの構成。
手順
- 
            新しい [Java クラス] を作成し、com.automationanywhere.botcommand パッケージを右クリックして、[新規] > [Java クラス] を順に選択します。新しいクラスの名前 GetFileDetails を入力します。
            - 
                  
                     Concatenate. java から @BotCommandをコピーし、新しい GetFileDetails.java ファイルに貼り付けます。import static com.automationanywhere.commandsdk.model.DataType.STRING; //BotCommand makes a class eligible for being considered as an action. @BotCommand //CommandPks adds required information to be dispalable on GUI. @CommandPkg( //Unique name inside a package and label to display. name = "concatenate", label = "[[Concatenate.label]]", node_label = "[[Concatenate.node_label]]", description = "[[Concatenate.description]]", icon = "pkg.svg", //Return type information. return_type ensures only the right kind of variable is provided on the UI. return_label = "[[Concatenate.return_label]]", return_type = STRING, return_required = true) public class Concatenate { //Messages read from full qualified property file name and provide i18n capability. private static final Messages MESSAGES = MessagesFactory .getMessages("com.automationanywhere.botcommand.samples.messages"); //Identify the entry point for the action. Returns a Value<String> because the return type is String. @Execute public Value<String> action( //Idx 1 would be displayed first, with a text box for entering the value. @Idx(index = "1", type = TEXT) //UI labels. @Pkg(label = "[[Concatenate.firstString.label]]") //Ensure that a validation error is thrown when the value is null. @NotEmpty String firstString, @Idx(index = "2", type = TEXT) @Pkg(label = "[[Concatenate.secondString.label]]") @NotEmpty String secondString) { //Internal validation, to disallow empty strings. No null check needed as we have NotEmpty on firstString. if ("".equals(firstString.trim())) throw new BotCommandException(MESSAGES.getString("emptyInputString", "firstString")); if ("".equals(secondString.trim())) throw new BotCommandException(MESSAGES.getString("emptyInputString", "secondString")); //Business logic String result = firstString + secondString; //Return StringValue. return new StringValue(result);
- 
                  次のような @CommandPkg パラメーターを更新します。name、label、node_label、description、およびicon。
- 
                  
                     return_labelおよびreturn_typeを更新します。
- 
                  
                     NumberValueアクション、内部検証、ビジネス ロジック、および戻り値を追加します。
- Concatenated. java ファイル、および samples.commands. basic ディレクトリ、sample.commands ディレクトリを削除します。
 
- 
                  
                     Concatenate. java から 
- 
            [en_US.json] ファイルを構成し、[src] > [メイン] > [リソース] > [ロケール] > [en_US.json] に移動して、ラベルと説明の値の後に次のフィールドを追加し、ファイルから他のパラメーターを削除します。
            { "label": "File Details", "description": "Returns basic file details", "GetFileDetails.label": "File Size", "GetFileDetails.description": "Return the size of the selected file in bytes", "GetFileDetails.node_label": "File Size in Bytes", "GetFileDetails.return_label": "File Size", "GetFileDetails.return_label_description": "Return in bytes", "GetFileDetails.filePath.label": "Select a File for analysis" }
- パッケージを削除し、[src] > [メイン] > [java] > [com.automationanyhwere.botcommand] に移動し、samples.commands を削除して、サンプルパッケージを削除します。
- 
            Github から新しいアイコンをインポートし、CommandPkg 注釈を更新します。
            - Bot フローで使用するアイコンをダウンロードし、.svg ファイルとして保存します。
- 画像のファイルを [src] > [メイン] > [リソース] > [アイコン] フォルダーにコピーします。
 
- 
            新しいディレクトリを作成します。[src] に移動して右クリックし、[新規] > [ディレクトリ] を選択します。
            - 
                  [名前] フィールドに「test\java」と入力します。または、test\javaを選択し、「TestFileSize」という名前を入力します。
- TestFileSize クラスでテスト注釈を構成します。
- 
                  
                     @testパブリック クラスを作成し、GetFileDetailsオブジェクトを作成して、アクションを呼び出します。
- オプション: IntelliJ で TestGetFileDetails を実行してテストします。
 
- 
                  [名前] フィールドに「test\java」と入力します。または、
- 
            
               TestFileSize ファイルを構成し、TestFileSize を開いて、次のコードをコピーして貼り付けます。
            { @Test public void TestGetFileDetails() { String filePath = "src\\main\\resources\\icons\\sample.svg"; //Create GetFileDetails Object GetFileDetails getFileDetails = new GetFileDetails(); //invoke action NumberValue output = getFileDetails.action(filePath); Assert.assertEquals(output.getAsDouble(), 5027.0); }
- プロジェクトを保存します。[ファイル] > [すべてを保存]。
- [すべての Gradle プロジェクトを再読み込み] をクリックし、[Gradle タスクの実行] をクリックして、[A2019FileDetails] プロジェクトが選択されていることを確認します。
- 
            [なんでも実行] ウィンドウで、「gradle clean build shadowJar」と入力します。
            実行後、以下のメッセージが表示されます。BUILD SUCCESSFUL in 8s <秒数>