Skip to content

Commit

Permalink
more it tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xamde committed Oct 3, 2023
1 parent ed763f0 commit 9b3e059
Show file tree
Hide file tree
Showing 28 changed files with 748 additions and 0 deletions.
100 changes: 100 additions & 0 deletions j2cl-maven-plugin/src/it/lib-with-jre-supersource/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## Problem Description
When the user tries to supply JRE types **on Java 9+**, they can currently not be used.

## Example
Two projects:
- app - `.war`, client code, using `super`
- super - customer, user-supplied JRE emulation classes

The relevant lines in the `build.log` are

1) First the compile starts fine ...
```
[INFO] Starting lib-with-jre-supersource:super:1.0/bytecode
[INFO] lib-with-jre-supersource:super:1.0/bytecode: No JRE emulation for this step on Java >= 9: java.specification.version:17
```
2) ... then `javac` determines that `java.util.UUID` was already in another _module_, namely in `java.base`.
```
[ERROR] lib-with-jre-supersource:super:1.0/bytecode:
/C:/_data_/_p_/_git/GitHub/j2clmavenplugin/j2cl-maven-plugin/target
/it-tests/lib-with-jre-supersource/super/src/main/java/java/util/UUID.java:16
package exists in another module: java.base
```
3) After loading the emulated UUID failed, the original UUID causes problems in J2CL.
```
[ERROR] lib-with-jre-supersource:super:1.0/bytecode:
/C:/_data_/_p_/_git/GitHub/j2clmavenplugin/j2cl-maven-plugin/target
/it-tests/lib-with-jre-supersource/super/src/main/java/java/util/UUID.java:43
no suitable constructor found for UUID(no arguments)
constructor java.util.UUID.UUID(byte[]) is not applicable
(actual and formal argument lists differ in length)
constructor java.util.UUID.UUID(long,long) is not applicable
(actual and formal argument lists differ in length)
```

NOTE: The exact same error can happen not only in `bytecode` but also in `stripped_bytecode`.

## Solution Ideas
To actually make `javac` read the new UUID class from our supplied `super`-library, we must:

- Use `--patch-module` to add `lib-with-jre-supersource-super-1.0.jar` as a JAR on the patch-module-path
- Allow reads from any module to `java.base`


--patch-module java.base={{bootClassPath}}
--add-reads java.base=ALL-UNNAMED


**And because we don't know which author of JAR dependencies intend on overwriting JRE built-in classes (this set varies between JDK versions), we must put _all_ JARs into the bootClassPath?**

**But we don't even know, which Java modules is targeted, so need to repeat all JARs to all --patch-module modules?**

**And we must open all modules?**
Which java modules besides base exist? `java --list-modules` tells us:

- java.base
- java.compiler
- java.datatransfer
- java.desktop
- java.instrument
- java.logging
- java.management
- java.management.rmi
- java.naming
- java.net.http
- java.prefs
- java.rmi
- java.scripting
- java.se
- java.security.jgss
- java.security.sasl
- java.smartcardio
- java.sql
- java.sql.rowset
- java.transaction.xa
- java.xml
- java.xml.crypto

## Conclusion
We should add options in the maven plugin to state entries for `--patch-module` and `--add-reads`.

<supersource>
<module>
<name>java.base</name>
<groupId>com.example</groupId>
<artifactId>libA</artifactId>
<versions>1.0</version>
<module>
<module>
<name>java.net.http</name>
<groupId>com.example</groupId>
<artifactId>libB</artifactId>
<versions>1.0</version>
<module>
</supersource>

which results in

--patch-module java.base=.m2/com/example/com-example-libA-1.0.jar java.net.http=.m2/com/example/com-example-libB-1.0.jar
--add-reads java.base=ALL-UNNAMED java.net.http=ALL-UNNAMED

56 changes: 56 additions & 0 deletions j2cl-maven-plugin/src/it/lib-with-jre-supersource/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>lib-with-jre-supersource</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<properties>
<elemental2.version>1.1.0</elemental2.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>lib-with-jre-supersource</groupId>
<artifactId>super</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.google.elemental2</groupId>
<artifactId>elemental2-dom</artifactId>
<version>${elemental2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Fix https://stackoverflow.com/questions/66920567/error-injecting-org-apache-maven-plugin-war-warmojo -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>google-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/google-snapshots/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example;

import elemental2.dom.DomGlobal;
import java.util.UUID;

public class App {

public void onModuleLoad() {
DomGlobal.console.log("Client code runs");
UUID uuid = UUID.fromString("hello");
DomGlobal.console.log("Now we need emulated code. A custom UUID: "+uuid);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* #%L
* Connected
* %%
* Copyright (C) 2017 Vertispan
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

// Defer this command, since this will be folded into the FlowChartEntryPoint js impl,
// and if it runs right away, will not have its dependencies resolved yet (at least while
// running in BUNDLE).
setTimeout(function(){
// Call the java "constructor" method, `new` will only work if it is a @JsType, or maybe
// once optimized. Without this, in BUNDLE mode, `new` doesn't include the clinit, so
// static imports haven't been resolved yet.
var ep = App.$create__();
// Invoke onModuleLoad to start the app.
ep.m_onModuleLoad__()
}, 0);

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<web-app></web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="application/javascript" src="app/app.js"></script>
</head>
<body></body>
</html>
16 changes: 16 additions & 0 deletions j2cl-maven-plugin/src/it/lib-with-jre-supersource/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>lib-with-jre-supersource</groupId>
<artifactId>lib-with-jre-supersource</artifactId>

<version>1.0</version>
<packaging>pom</packaging>

<modules>
<module>super</module>
<module>app</module>
</modules>

</project>
44 changes: 44 additions & 0 deletions j2cl-maven-plugin/src/it/lib-with-jre-supersource/super/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>lib-with-jre-supersource</groupId>
<version>1.0</version>
<artifactId>super</artifactId>
<packaging>jar</packaging>

<properties>
<elemental2.version>1.1.0</elemental2.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.google.jsinterop</groupId>
<artifactId>jsinterop-annotations</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>google-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/google-snapshots/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.util;

import java.io.Serializable;

/**
* Hand-written UUID class. Taken from DominoKit org\dominokit\domino-jackson-super\1.0.0-RC3\domino-jackson-super-1.0.0-RC3-sources
* and simplified.
*
* Note the JDK version has e.g. a "public long node()" method which returns 48 bits of the UUID.
* This version lacks that method.
* This version lacks validation, so UUID.fromString("hello") works.
* The JDK version throws IllegalArgumentException, e.g. if the dashes are missing.
*/
public class UUID implements Serializable, Comparable<UUID> {

private static final long serialVersionUID = 7373345728974414241L;

private String value;

private UUID() {}

/**
* Emulated version without validation
* @param uuidString
* @return
*/
public static UUID fromString(String uuidString) {
final UUID uuid = new UUID();
uuid.value = uuidString;
return uuid;
}

@Override
public int compareTo(UUID arg0) {
return value.compareTo(arg0.value);
}

@Override
public int hashCode() {
return value.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
UUID other = (UUID) obj;
if (value == null) {
if (other.value != null) return false;
} else if (!value.equals(other.value)) return false;
return true;
}

@Override
public String toString() {
return value;
}
}
2 changes: 2 additions & 0 deletions j2cl-maven-plugin/src/it/shared-lib-externalizable/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This tests extends the basic "shared-lib-reactor" test and uses `java.io.Externalizable`.
That class has different versions in standard JRE (two methods) and in emulated JRE (no methods).
Loading

0 comments on commit 9b3e059

Please sign in to comment.