这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions libraries-bytecode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/dinosaur-2.jar</systemPath>
</dependency>-->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${teavm.version}</version>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${teavm.version}</version>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-tooling</artifactId>
<version>${teavm.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -96,6 +111,7 @@
<properties>
<javaassist.version>3.29.2-GA</javaassist.version>
<asm.version>5.2</asm.version>
<teavm.version>0.10.1</teavm.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}