Skip to content

Commit 5e713e3

Browse files
spfinkmergify[bot]
authored andcommitted
feat(java): use immutable java implementations of JSII primitive collection types array and map (#765)
1 parent f3b98b5 commit 5e713e3

157 files changed

Lines changed: 1185 additions & 288 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/jsii-calc/lib/compliance.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,3 +1860,24 @@ export class InterfacesMaker {
18601860

18611861
private constructor() { }
18621862
}
1863+
1864+
export class ClassWithCollections {
1865+
public map: { [key: string]: string };
1866+
public array: string[];
1867+
1868+
public static staticMap:{ [key: string]: string } = {'key1': 'value1', 'key2': 'value2'};
1869+
public static staticArray: string[] = ["one", "two"];
1870+
1871+
constructor(map: { [key: string]: string }, array: string[]) {
1872+
this.map = map;
1873+
this.array = array;
1874+
}
1875+
1876+
static createAList(): string[] {
1877+
return ["one", "two"];
1878+
}
1879+
1880+
static createAMap(): { [key: string]: string } {
1881+
return {'key1': 'value1', 'key2': 'value2'};
1882+
}
1883+
}

packages/jsii-calc/test/assembly.jsii

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,168 @@
17211721
}
17221722
]
17231723
},
1724+
"jsii-calc.ClassWithCollections": {
1725+
"assembly": "jsii-calc",
1726+
"docs": {
1727+
"stability": "experimental"
1728+
},
1729+
"fqn": "jsii-calc.ClassWithCollections",
1730+
"initializer": {
1731+
"docs": {
1732+
"stability": "experimental"
1733+
},
1734+
"parameters": [
1735+
{
1736+
"name": "map",
1737+
"type": {
1738+
"collection": {
1739+
"elementtype": {
1740+
"primitive": "string"
1741+
},
1742+
"kind": "map"
1743+
}
1744+
}
1745+
},
1746+
{
1747+
"name": "array",
1748+
"type": {
1749+
"collection": {
1750+
"elementtype": {
1751+
"primitive": "string"
1752+
},
1753+
"kind": "array"
1754+
}
1755+
}
1756+
}
1757+
]
1758+
},
1759+
"kind": "class",
1760+
"locationInModule": {
1761+
"filename": "lib/compliance.ts",
1762+
"line": 1864
1763+
},
1764+
"methods": [
1765+
{
1766+
"docs": {
1767+
"stability": "experimental"
1768+
},
1769+
"locationInModule": {
1770+
"filename": "lib/compliance.ts",
1771+
"line": 1876
1772+
},
1773+
"name": "createAList",
1774+
"returns": {
1775+
"type": {
1776+
"collection": {
1777+
"elementtype": {
1778+
"primitive": "string"
1779+
},
1780+
"kind": "array"
1781+
}
1782+
}
1783+
},
1784+
"static": true
1785+
},
1786+
{
1787+
"docs": {
1788+
"stability": "experimental"
1789+
},
1790+
"locationInModule": {
1791+
"filename": "lib/compliance.ts",
1792+
"line": 1880
1793+
},
1794+
"name": "createAMap",
1795+
"returns": {
1796+
"type": {
1797+
"collection": {
1798+
"elementtype": {
1799+
"primitive": "string"
1800+
},
1801+
"kind": "map"
1802+
}
1803+
}
1804+
},
1805+
"static": true
1806+
}
1807+
],
1808+
"name": "ClassWithCollections",
1809+
"properties": [
1810+
{
1811+
"docs": {
1812+
"stability": "experimental"
1813+
},
1814+
"locationInModule": {
1815+
"filename": "lib/compliance.ts",
1816+
"line": 1869
1817+
},
1818+
"name": "staticArray",
1819+
"static": true,
1820+
"type": {
1821+
"collection": {
1822+
"elementtype": {
1823+
"primitive": "string"
1824+
},
1825+
"kind": "array"
1826+
}
1827+
}
1828+
},
1829+
{
1830+
"docs": {
1831+
"stability": "experimental"
1832+
},
1833+
"locationInModule": {
1834+
"filename": "lib/compliance.ts",
1835+
"line": 1868
1836+
},
1837+
"name": "staticMap",
1838+
"static": true,
1839+
"type": {
1840+
"collection": {
1841+
"elementtype": {
1842+
"primitive": "string"
1843+
},
1844+
"kind": "map"
1845+
}
1846+
}
1847+
},
1848+
{
1849+
"docs": {
1850+
"stability": "experimental"
1851+
},
1852+
"locationInModule": {
1853+
"filename": "lib/compliance.ts",
1854+
"line": 1866
1855+
},
1856+
"name": "array",
1857+
"type": {
1858+
"collection": {
1859+
"elementtype": {
1860+
"primitive": "string"
1861+
},
1862+
"kind": "array"
1863+
}
1864+
}
1865+
},
1866+
{
1867+
"docs": {
1868+
"stability": "experimental"
1869+
},
1870+
"locationInModule": {
1871+
"filename": "lib/compliance.ts",
1872+
"line": 1865
1873+
},
1874+
"name": "map",
1875+
"type": {
1876+
"collection": {
1877+
"elementtype": {
1878+
"primitive": "string"
1879+
},
1880+
"kind": "map"
1881+
}
1882+
}
1883+
}
1884+
]
1885+
},
17241886
"jsii-calc.ClassWithDocs": {
17251887
"assembly": "jsii-calc",
17261888
"docs": {
@@ -9453,5 +9615,5 @@
94539615
}
94549616
},
94559617
"version": "0.16.0",
9456-
"fingerprint": "D99pLaaW2xH2uuyUa8hgakiEso/wXbcMNkP9RmOTwgo="
9618+
"fingerprint": "SyvtIxuwfgMPg4aYpb3VR2M4Ra4b+iLnrBwHjsLb12g="
94579619
}

packages/jsii-java-runtime-test/pom.xml.t.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
3232
<version>4.12</version>
3333
<scope>test</scope>
3434
</dependency>
35+
36+
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
37+
<dependency>
38+
<groupId>org.hamcrest</groupId>
39+
<artifactId>hamcrest-all</artifactId>
40+
<version>1.3</version>
41+
<scope>test</scope>
42+
</dependency>
3543
</dependencies>
3644
3745
<build>

packages/jsii-java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import software.amazon.jsii.tests.calculator.AsyncVirtualMethods;
1313
import software.amazon.jsii.tests.calculator.Calculator;
1414
import software.amazon.jsii.tests.calculator.CalculatorProps;
15+
import software.amazon.jsii.tests.calculator.ClassWithCollections;
1516
import software.amazon.jsii.tests.calculator.ClassWithJavaReservedWords;
1617
import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties;
1718
import software.amazon.jsii.tests.calculator.Constructors;
@@ -70,10 +71,15 @@
7071
import java.io.IOException;
7172
import java.time.Instant;
7273
import java.util.Arrays;
74+
import java.util.Collections;
7375
import java.util.HashMap;
7476
import java.util.List;
7577
import java.util.Map;
7678

79+
import static org.hamcrest.MatcherAssert.assertThat;
80+
import static org.hamcrest.Matchers.contains;
81+
import static org.hamcrest.Matchers.hasEntry;
82+
import static org.hamcrest.Matchers.is;
7783
import static org.junit.Assert.assertEquals;
7884
import static org.junit.Assert.assertFalse;
7985
import static org.junit.Assert.assertNotEquals;
@@ -1268,6 +1274,92 @@ public void canLoadEnumValues() {
12681274
assertNotNull(EnumDispenser.randomIntegerLikeEnum());
12691275
}
12701276

1277+
@Test(expected = UnsupportedOperationException.class)
1278+
public void listInClassCannotBeModified() {
1279+
List<String> modifiableList = Arrays.asList("one", "two");
1280+
1281+
ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList);
1282+
1283+
classWithCollections.getArray().add("three");
1284+
}
1285+
1286+
@Test
1287+
public void listInClassCanBeReadCorrectly() {
1288+
List<String> modifiableList = Arrays.asList("one", "two");
1289+
1290+
ClassWithCollections classWithCollections = new ClassWithCollections(Collections.emptyMap(), modifiableList);
1291+
1292+
assertThat(classWithCollections.getArray(), contains("one", "two"));
1293+
}
1294+
1295+
@Test(expected = UnsupportedOperationException.class)
1296+
public void mapInClassCannotBeModified() {
1297+
Map<String, String> modifiableMap = new HashMap<>();
1298+
modifiableMap.put("key", "value");
1299+
1300+
ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList());
1301+
1302+
classWithCollections.getMap().put("keyTwo", "valueTwo");
1303+
}
1304+
1305+
@Test
1306+
public void mapInClassCanBeReadCorrectly() {
1307+
Map<String, String> modifiableMap = new HashMap<>();
1308+
modifiableMap.put("key", "value");
1309+
1310+
ClassWithCollections classWithCollections = new ClassWithCollections(modifiableMap, Collections.emptyList());
1311+
1312+
Map<String, String> result = classWithCollections.getMap();
1313+
assertThat(result, hasEntry("key", "value"));
1314+
assertThat(result.size(), is(1));
1315+
}
1316+
1317+
@Test(expected = UnsupportedOperationException.class)
1318+
public void staticListInClassCannotBeModified() {
1319+
ClassWithCollections.getStaticArray().add("three");
1320+
}
1321+
1322+
@Test
1323+
public void staticListInClassCanBeReadCorrectly() {
1324+
assertThat(ClassWithCollections.getStaticArray(), contains("one", "two"));
1325+
}
1326+
1327+
@Test(expected = UnsupportedOperationException.class)
1328+
public void staticMapInClassCannotBeModified() {
1329+
ClassWithCollections.getStaticMap().put("keyTwo", "valueTwo");
1330+
}
1331+
1332+
@Test
1333+
public void staticMapInClassCanBeReadCorrectly() {
1334+
Map<String, String> result = ClassWithCollections.getStaticMap();
1335+
assertThat(result, hasEntry("key1", "value1"));
1336+
assertThat(result, hasEntry("key2", "value2"));
1337+
assertThat(result.size(), is(2));
1338+
}
1339+
1340+
@Test(expected = UnsupportedOperationException.class)
1341+
public void arrayReturnedByMethodCannotBeModified() {
1342+
ClassWithCollections.createAList().add("three");
1343+
}
1344+
1345+
@Test
1346+
public void arrayReturnedByMethodCanBeRead() {
1347+
assertThat(ClassWithCollections.createAList(), contains("one", "two"));
1348+
}
1349+
1350+
@Test(expected = UnsupportedOperationException.class)
1351+
public void mapReturnedByMethodCannotBeModified() {
1352+
ClassWithCollections.createAMap().put("keyThree", "valueThree");
1353+
}
1354+
1355+
@Test
1356+
public void mapReturnedByMethodCanBeRead() {
1357+
Map<String, String> result = ClassWithCollections.createAMap();
1358+
assertThat(result, hasEntry("key1", "value1"));
1359+
assertThat(result, hasEntry("key2", "value2"));
1360+
assertThat(result.size(), is(2));
1361+
}
1362+
12711363
static class PartiallyInitializedThisConsumerImpl extends PartiallyInitializedThisConsumer {
12721364
@Override
12731365
public String consumePartiallyInitializedThis(final ConstructorPassesThisOut obj,

packages/jsii-java-runtime/pom.xml.t.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
5151
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5252
<!-- Versions of the dependencies -->
5353
<findbugs.version>[3.0.2,3.1.0)</findbugs.version>
54+
<hamcrest.version>1.3</hamcrest.version>
5455
<jackson-core.version>[2.9.9,2.10.0)</jackson-core.version>
5556
<jackson-databind.version>[2.9.9.2,2.10.0)</jackson-databind.version>
5657
<javax.annotations.version>[1.3.2,1.4.0)</javax.annotations.version>
@@ -103,6 +104,14 @@ process.stdout.write(`<?xml version="1.0" encoding="UTF-8"?>
103104
<version>\${junit.version}</version>
104105
<scope>test</scope>
105106
</dependency>
107+
108+
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
109+
<dependency>
110+
<groupId>org.hamcrest</groupId>
111+
<artifactId>hamcrest-all</artifactId>
112+
<version>\${hamcrest.version}</version>
113+
<scope>test</scope>
114+
</dependency>
106115
107116
<!-- https://mvnrepository.com/artifact/org.mjockito/mockito-core -->
108117
<dependency>

0 commit comments

Comments
 (0)