创建新的 Java 类
- Updated: 2022/05/19
使用 IntelliJ 创建新的 Java 类和新目录,并配置其他构建文件。
先决条件
完成以下任务中的步骤:配置构建文件。
过程
-
创建新的 Java 类,右键单击 com.automationanywhere.botcommand 包,然后选择 新建 > Java 类。 输入新建类 GetFileDetails 的名称。
-
从
@BotCommand
复制到 Concatenate.java 并将其粘贴到新的 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 目录。
-
从
-
配置 en_US.json 文件:前往 src > main > resources > locales > en_US.json,在标签和描述值之后添加以下字段,并从文件中删除其他参数。
{ "label": "文件详情", "description": "返回基本文件详细信息", "GetFileDetails.label": "文件大小", "GetFileDetails.description": "返回所选文件的大小(以字节为单位)", "GetFileDetails.node_label": "文件大小(字节)", "GetFileDetails.return_label": "文件大小", "GetFileDetails.return_label_description": "以字节为单位返回", "GetFileDetails.filePath.label": "选择一个文件进行分析" }
- 删除包:前往 src > main > java > com.automationanyhwere.botcommand,并删除 samples.commands 和 samples 包。
-
从 Github 导入新图标并更新 CommandPkg 注释。
- 下载您要在机器人流程中使用的任何图标,并将它们保存为 .svg 文件。
- 将您的图像文件复制到 src > main > resources > icons 文件夹。
-
创建一个新目录:前往 src,右键单击,选择 新建 > 目录。
-
在名称字段中,输入 test\java。 或者,选择
test\java
,并输入名称 TestFileSize。 - 在 TestFileSize java 类中配置测试注释。
-
创建
@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。
运行后会显示以下消息: 8 秒内构建成功 <number of seconds>