Crear nueva clase de Java
Descargar como PDF
- Última actualización2022/05/19
Use IntelliJ para crear una nueva clase de Java y un nuevo directorio, y configure otros archivos de compilación.
Antes de empezar
Complete los pasos en la siguiente tarea: Configuración de los archivos de compilación.
Procedimiento
-
Crear una nueva clase de Java, hacer clic con el botón derecho del ratón en el paquete com.automationanywhere.botcommand y seleccionar Nueva > Clase de Java. Ingresar el nombre para la nueva clase GetFileDetails.
-
Copiar
@BotCommand
de Concatenate. java y pegarlo en el nuevo archivo 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);
-
Actualizar los parámetros de @CommandPkg como:
name
,label
,node_label
,description
yicon
. -
Actualizar
return_label
yreturn_type
. -
Agregar la acción
NumberValue
, la validación interna, la lógica de negocio y el valor de retorno. - Eliminar el archivo Concatenado. java, el directorio samples.commands. basic y el directorio sample.commands.
-
Copiar
-
Configurar el archivo es_US.json: vaya a src > principal > recursos > locales > es_US.json y agregue los siguientes campos después de los valores de la etiqueta y la descripción, y elimine otros parámetros del archivo.
{ "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" }
- Eliminar los paquetes: vaya a src > principal > java > com.automationanyhwere.botcommand y elimine samples.commands y los paquetes samples.
-
Importe nuevos íconos desde Github y actualice la anotación CommandPkg.
- Descargue los íconos que desee usar en su flujo de bots y guárdelos como archivos .svg.
- Copie ambos archivos de imagen en la carpeta src > principal > recursos > íconos.
-
Cree un nuevo directorio: vaya a src, haga clic con el botón derecho y seleccione Nuevo > Directorio.
-
En el campo Nombre, introduzca test\java. Como alternativa, seleccione
test\java
e ingrese el nombre TestFileSize. - Configure las anotaciones de prueba en la clase de Java TestFileSize.
-
Cree una clase pública
@test
cree un objetoGetFileDetails
e invoque la acción. - Opcional: Ejecute TestGetFileDetails en IntelliJ para probarlo.
-
En el campo Nombre, introduzca test\java. Como alternativa, seleccione
-
Configure el archivo TestFileSize, abra el archivo TestFileSize y copie y pegue el siguiente código:
{ @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); }
- Guarde el proyecto: Archivo > Guardar todo.
- Haga clic en Vuelva a cargar todos los proyectos Gradle y luego haga clic en Ejecutar tarea Gradle, luego verifique que esté seleccionado el proyecto A2019FileDetails.
-
En la ventana Ejecutar cualquier cosa, introduzca gradle clean build shadowJar.
Una vez que se ejecuta, se muestra el siguiente mensaje: SE CREA CORRECTAMENTE en 8 s <number of seconds>