diff --git a/libraries-bytecode/pom.xml b/libraries-bytecode/pom.xml index eda255ccd65d..2a23a38bbe0e 100644 --- a/libraries-bytecode/pom.xml +++ b/libraries-bytecode/pom.xml @@ -62,6 +62,21 @@ system ${project.basedir}/src/main/resources/dinosaur-2.jar --> + + org.teavm + teavm-core + ${teavm.version} + + + org.teavm + teavm-classlib + ${teavm.version} + + + org.teavm + teavm-tooling + ${teavm.version} + @@ -96,6 +111,7 @@ 3.29.2-GA 5.2 + 0.10.1 \ No newline at end of file diff --git a/libraries-bytecode/src/main/java/com/baeldung/teavm/Calculator.java b/libraries-bytecode/src/main/java/com/baeldung/teavm/Calculator.java new file mode 100644 index 000000000000..62b80513d4c1 --- /dev/null +++ b/libraries-bytecode/src/main/java/com/baeldung/teavm/Calculator.java @@ -0,0 +1,47 @@ +package com.baeldung.teavm; + +import org.teavm.jso.JSExport; +import org.teavm.jso.dom.html.*; + +public class Calculator { + + public static void main(String[] args) { + HTMLDocument document = HTMLDocument.current(); + HTMLElement container = document.getElementById("calculator-container"); + + // Create input fields + HTMLInputElement input1 = (HTMLInputElement) document.createElement("input"); + input1.setType("number"); + container.appendChild(input1); + + HTMLInputElement input2 = (HTMLInputElement) document.createElement("input"); + input2.setType("number"); + container.appendChild(input2); + + // Create a button + HTMLButtonElement button = (HTMLButtonElement) document.createElement("button"); + button.appendChild(document.createTextNode("Calculate Sum")); + container.appendChild(button); + + // Create a div to display the result + HTMLElement resultDiv = document.createElement("div"); + container.appendChild(resultDiv); + + // Add click event listener to the button + button.addEventListener("click", (evt) -> { + try { + long num1 = Long.parseLong(input1.getValue()); + long num2 = Long.parseLong(input2.getValue()); + long sum = num1 + num2; + resultDiv.setTextContent("Result: " + sum); + } catch (NumberFormatException e) { + resultDiv.setTextContent("Please enter valid integer numbers."); + } + }); + } + + @JSExport + public static int sum(int a, int b) { + return a + b; + } +} \ No newline at end of file diff --git a/libraries-bytecode/src/main/java/com/baeldung/teavm/TeaVMRunner.java b/libraries-bytecode/src/main/java/com/baeldung/teavm/TeaVMRunner.java new file mode 100644 index 000000000000..1823755279f4 --- /dev/null +++ b/libraries-bytecode/src/main/java/com/baeldung/teavm/TeaVMRunner.java @@ -0,0 +1,22 @@ +package com.baeldung.teavm; + +import org.teavm.tooling.TeaVMTool; +import org.teavm.tooling.TeaVMTargetType; +import org.teavm.vm.TeaVMOptimizationLevel; + +import java.io.File; + +public class TeaVMRunner { + public static void main(String[] args) throws Exception { + TeaVMTool tool = new TeaVMTool(); + tool.setTargetDirectory(new File("target/teavm")); + tool.setTargetFileName("calculator.js"); + tool.setMainClass("com.baeldung.teavm.Calculator"); + tool.setTargetType(TeaVMTargetType.JAVASCRIPT); + tool.setOptimizationLevel(TeaVMOptimizationLevel.ADVANCED); + tool.setDebugInformationGenerated(false); + tool.setIncremental(false); + tool.setObfuscated(true); + tool.generate(); + } +} \ No newline at end of file diff --git a/libraries-bytecode/src/test/java/com/baeldung/teavm/CalculatorUnitTest.java b/libraries-bytecode/src/test/java/com/baeldung/teavm/CalculatorUnitTest.java new file mode 100644 index 000000000000..1c7afd421403 --- /dev/null +++ b/libraries-bytecode/src/test/java/com/baeldung/teavm/CalculatorUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.teavm; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorUnitTest { + + @Test + public void whenAddingTwoPositiveNumbers_thenCorrectSum() { + // when + int result = Calculator.sum(3, 7); + + // then + assertEquals(10, result); + } + + @Test + public void whenAddingNegativeAndPositiveNumber_thenCorrectSum() { + // when + int result = Calculator.sum(-5, 10); + + // then + assertEquals(5, result); + } + + @Test + public void whenAddingTwoNegativeNumbers_thenCorrectSum() { + // when + int result = Calculator.sum(-4, -6); + + // then + assertEquals(-10, result); + } +}