Skip to content

Commit 32bc117

Browse files
bmaizelsRomainMuller
authored andcommitted
feat(java): detect & rename members named after reserved words (#705)
1 parent ca85a01 commit 32bc117

File tree

47 files changed

+1264
-207
lines changed

Some content is hidden

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

47 files changed

+1264
-207
lines changed

packages/jsii-calc/lib/compliance.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,27 @@ export interface DiamondInheritanceTopLevelStruct extends DiamondInheritanceFirs
17911791
readonly topLevelProperty: string;
17921792
}
17931793

1794+
export interface StructWithJavaReservedWords {
1795+
readonly default: string;
1796+
readonly assert?: string;
1797+
1798+
// These properties are designed to break the naive implementation of equals() and hashcode() using the standard template
1799+
readonly result?: string;
1800+
readonly that?: string;
1801+
}
1802+
1803+
export class ClassWithJavaReservedWords {
1804+
readonly int: string;
1805+
1806+
public constructor(int: string) {
1807+
this.int = int;
1808+
}
1809+
1810+
public import(assert: string): string {
1811+
return this.int + assert;
1812+
}
1813+
}
1814+
17941815
/**
17951816
* Just because we can.
17961817
*

packages/jsii-calc/test/assembly.jsii

Lines changed: 150 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,73 @@
17421742
},
17431743
"name": "ClassWithDocs"
17441744
},
1745+
"jsii-calc.ClassWithJavaReservedWords": {
1746+
"assembly": "jsii-calc",
1747+
"docs": {
1748+
"stability": "experimental"
1749+
},
1750+
"fqn": "jsii-calc.ClassWithJavaReservedWords",
1751+
"initializer": {
1752+
"docs": {
1753+
"stability": "experimental"
1754+
},
1755+
"parameters": [
1756+
{
1757+
"name": "int",
1758+
"type": {
1759+
"primitive": "string"
1760+
}
1761+
}
1762+
]
1763+
},
1764+
"kind": "class",
1765+
"locationInModule": {
1766+
"filename": "lib/compliance.ts",
1767+
"line": 1803
1768+
},
1769+
"methods": [
1770+
{
1771+
"docs": {
1772+
"stability": "experimental"
1773+
},
1774+
"locationInModule": {
1775+
"filename": "lib/compliance.ts",
1776+
"line": 1810
1777+
},
1778+
"name": "import",
1779+
"parameters": [
1780+
{
1781+
"name": "assert",
1782+
"type": {
1783+
"primitive": "string"
1784+
}
1785+
}
1786+
],
1787+
"returns": {
1788+
"type": {
1789+
"primitive": "string"
1790+
}
1791+
}
1792+
}
1793+
],
1794+
"name": "ClassWithJavaReservedWords",
1795+
"properties": [
1796+
{
1797+
"docs": {
1798+
"stability": "experimental"
1799+
},
1800+
"immutable": true,
1801+
"locationInModule": {
1802+
"filename": "lib/compliance.ts",
1803+
"line": 1804
1804+
},
1805+
"name": "int",
1806+
"type": {
1807+
"primitive": "string"
1808+
}
1809+
}
1810+
]
1811+
},
17451812
"jsii-calc.ClassWithMutableObjectLiteralProperty": {
17461813
"assembly": "jsii-calc",
17471814
"docs": {
@@ -7978,7 +8045,7 @@
79788045
"kind": "class",
79798046
"locationInModule": {
79808047
"filename": "lib/compliance.ts",
7981-
"line": 1799
8048+
"line": 1820
79828049
},
79838050
"methods": [
79848051
{
@@ -7987,7 +8054,7 @@
79878054
},
79888055
"locationInModule": {
79898056
"filename": "lib/compliance.ts",
7990-
"line": 1808
8057+
"line": 1829
79918058
},
79928059
"name": "howManyVarArgsDidIPass",
79938060
"parameters": [
@@ -8019,7 +8086,7 @@
80198086
},
80208087
"locationInModule": {
80218088
"filename": "lib/compliance.ts",
8022-
"line": 1800
8089+
"line": 1821
80238090
},
80248091
"name": "roundTrip",
80258092
"parameters": [
@@ -8046,6 +8113,85 @@
80468113
],
80478114
"name": "StructPassing"
80488115
},
8116+
"jsii-calc.StructWithJavaReservedWords": {
8117+
"assembly": "jsii-calc",
8118+
"datatype": true,
8119+
"docs": {
8120+
"stability": "experimental"
8121+
},
8122+
"fqn": "jsii-calc.StructWithJavaReservedWords",
8123+
"kind": "interface",
8124+
"locationInModule": {
8125+
"filename": "lib/compliance.ts",
8126+
"line": 1794
8127+
},
8128+
"name": "StructWithJavaReservedWords",
8129+
"properties": [
8130+
{
8131+
"abstract": true,
8132+
"docs": {
8133+
"stability": "experimental"
8134+
},
8135+
"immutable": true,
8136+
"locationInModule": {
8137+
"filename": "lib/compliance.ts",
8138+
"line": 1795
8139+
},
8140+
"name": "default",
8141+
"type": {
8142+
"primitive": "string"
8143+
}
8144+
},
8145+
{
8146+
"abstract": true,
8147+
"docs": {
8148+
"stability": "experimental"
8149+
},
8150+
"immutable": true,
8151+
"locationInModule": {
8152+
"filename": "lib/compliance.ts",
8153+
"line": 1796
8154+
},
8155+
"name": "assert",
8156+
"optional": true,
8157+
"type": {
8158+
"primitive": "string"
8159+
}
8160+
},
8161+
{
8162+
"abstract": true,
8163+
"docs": {
8164+
"stability": "experimental"
8165+
},
8166+
"immutable": true,
8167+
"locationInModule": {
8168+
"filename": "lib/compliance.ts",
8169+
"line": 1799
8170+
},
8171+
"name": "result",
8172+
"optional": true,
8173+
"type": {
8174+
"primitive": "string"
8175+
}
8176+
},
8177+
{
8178+
"abstract": true,
8179+
"docs": {
8180+
"stability": "experimental"
8181+
},
8182+
"immutable": true,
8183+
"locationInModule": {
8184+
"filename": "lib/compliance.ts",
8185+
"line": 1800
8186+
},
8187+
"name": "that",
8188+
"optional": true,
8189+
"type": {
8190+
"primitive": "string"
8191+
}
8192+
}
8193+
]
8194+
},
80498195
"jsii-calc.Sum": {
80508196
"assembly": "jsii-calc",
80518197
"base": "jsii-calc.composition.CompositeOperation",
@@ -9215,5 +9361,5 @@
92159361
}
92169362
},
92179363
"version": "0.15.0",
9218-
"fingerprint": "SqE3g3zYpTXR3veJ0hoo+OuH7uDYav7rVxMrKNh88I4="
9364+
"fingerprint": "s29ilwOj9pcb3e3u8f49bqxkFzU9a52FAr4mHWpRuYA="
92199365
}

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

Lines changed: 46 additions & 3 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.ClassWithJavaReservedWords;
1516
import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties;
1617
import software.amazon.jsii.tests.calculator.Constructors;
1718
import software.amazon.jsii.tests.calculator.DataRenderer;
@@ -50,6 +51,7 @@
5051
import software.amazon.jsii.tests.calculator.ReturnsPrivateImplementationOfInterface;
5152
import software.amazon.jsii.tests.calculator.StableStruct;
5253
import software.amazon.jsii.tests.calculator.Statics;
54+
import software.amazon.jsii.tests.calculator.StructWithJavaReservedWords;
5355
import software.amazon.jsii.tests.calculator.Sum;
5456
import software.amazon.jsii.tests.calculator.SyncVirtualMethods;
5557
import software.amazon.jsii.tests.calculator.UnionProperties;
@@ -73,6 +75,7 @@
7375

7476
import static org.junit.Assert.assertEquals;
7577
import static org.junit.Assert.assertFalse;
78+
import static org.junit.Assert.assertNotEquals;
7679
import static org.junit.Assert.assertNotNull;
7780
import static org.junit.Assert.assertNull;
7881
import static org.junit.Assert.assertSame;
@@ -1018,9 +1021,49 @@ public void consts() {
10181021
@Test
10191022
public void reservedKeywordsAreSlugifiedInMethodNames() {
10201023
JavaReservedWords obj = new JavaReservedWords();
1021-
obj.import_();
1022-
obj.const_();
1023-
assertEquals("hello", obj.getWhile()); // properties do not need to be slufieid
1024+
obj.doImport();
1025+
obj.doConst();
1026+
assertEquals("hello", obj.getWhileValue()); // properties should also be 'slugified'
1027+
}
1028+
1029+
@Test
1030+
public void reservedKeywordsAreSlugifiedInStructProperties() {
1031+
StructWithJavaReservedWords struct = StructWithJavaReservedWords.builder()
1032+
.assertValue("one")
1033+
.defaultValue("two")
1034+
.build();
1035+
1036+
assertEquals("one", struct.getAssertValue());
1037+
assertEquals("two", struct.getDefaultValue());
1038+
}
1039+
1040+
@Test
1041+
public void reservedKeywordsAreSlugifiedInClassProperties() {
1042+
ClassWithJavaReservedWords obj = new ClassWithJavaReservedWords("one");
1043+
1044+
String result = obj.doImport("two");
1045+
1046+
assertEquals("onetwo", result);
1047+
}
1048+
1049+
@Test
1050+
public void hashCodeIsResistantToPropertyShadowingResultVariable() {
1051+
StructWithJavaReservedWords first = StructWithJavaReservedWords.builder().defaultValue("one").build();
1052+
StructWithJavaReservedWords second = StructWithJavaReservedWords.builder().defaultValue("one").build();
1053+
StructWithJavaReservedWords third = StructWithJavaReservedWords.builder().defaultValue("two").build();
1054+
1055+
assertEquals(first.hashCode(), second.hashCode());
1056+
assertNotEquals(first.hashCode(), third.hashCode());
1057+
}
1058+
1059+
@Test
1060+
public void equalsIsResistantToPropertyShadowingResultVariable() {
1061+
StructWithJavaReservedWords first = StructWithJavaReservedWords.builder().defaultValue("one").build();
1062+
StructWithJavaReservedWords second = StructWithJavaReservedWords.builder().defaultValue("one").build();
1063+
StructWithJavaReservedWords third = StructWithJavaReservedWords.builder().defaultValue("two").build();
1064+
1065+
assertEquals(first, second);
1066+
assertNotEquals(first, third);
10241067
}
10251068

10261069
@Test

0 commit comments

Comments
 (0)