Skip to content

Commit

Permalink
feat(java): detect & rename members named after reserved words (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaizels authored and RomainMuller committed Aug 14, 2019
1 parent ca85a01 commit 32bc117
Show file tree
Hide file tree
Showing 47 changed files with 1,264 additions and 207 deletions.
21 changes: 21 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,27 @@ export interface DiamondInheritanceTopLevelStruct extends DiamondInheritanceFirs
readonly topLevelProperty: string;
}

export interface StructWithJavaReservedWords {
readonly default: string;
readonly assert?: string;

// These properties are designed to break the naive implementation of equals() and hashcode() using the standard template
readonly result?: string;
readonly that?: string;
}

export class ClassWithJavaReservedWords {
readonly int: string;

public constructor(int: string) {
this.int = int;
}

public import(assert: string): string {
return this.int + assert;
}
}

/**
* Just because we can.
*
Expand Down
154 changes: 150 additions & 4 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,73 @@
},
"name": "ClassWithDocs"
},
"jsii-calc.ClassWithJavaReservedWords": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.ClassWithJavaReservedWords",
"initializer": {
"docs": {
"stability": "experimental"
},
"parameters": [
{
"name": "int",
"type": {
"primitive": "string"
}
}
]
},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1803
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1810
},
"name": "import",
"parameters": [
{
"name": "assert",
"type": {
"primitive": "string"
}
}
],
"returns": {
"type": {
"primitive": "string"
}
}
}
],
"name": "ClassWithJavaReservedWords",
"properties": [
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1804
},
"name": "int",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.ClassWithMutableObjectLiteralProperty": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -7978,7 +8045,7 @@
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1799
"line": 1820
},
"methods": [
{
Expand All @@ -7987,7 +8054,7 @@
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1808
"line": 1829
},
"name": "howManyVarArgsDidIPass",
"parameters": [
Expand Down Expand Up @@ -8019,7 +8086,7 @@
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1800
"line": 1821
},
"name": "roundTrip",
"parameters": [
Expand All @@ -8046,6 +8113,85 @@
],
"name": "StructPassing"
},
"jsii-calc.StructWithJavaReservedWords": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.StructWithJavaReservedWords",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1794
},
"name": "StructWithJavaReservedWords",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1795
},
"name": "default",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1796
},
"name": "assert",
"optional": true,
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1799
},
"name": "result",
"optional": true,
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1800
},
"name": "that",
"optional": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.Sum": {
"assembly": "jsii-calc",
"base": "jsii-calc.composition.CompositeOperation",
Expand Down Expand Up @@ -9215,5 +9361,5 @@
}
},
"version": "0.15.0",
"fingerprint": "SqE3g3zYpTXR3veJ0hoo+OuH7uDYav7rVxMrKNh88I4="
"fingerprint": "s29ilwOj9pcb3e3u8f49bqxkFzU9a52FAr4mHWpRuYA="
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import software.amazon.jsii.tests.calculator.AsyncVirtualMethods;
import software.amazon.jsii.tests.calculator.Calculator;
import software.amazon.jsii.tests.calculator.CalculatorProps;
import software.amazon.jsii.tests.calculator.ClassWithJavaReservedWords;
import software.amazon.jsii.tests.calculator.ClassWithPrivateConstructorAndAutomaticProperties;
import software.amazon.jsii.tests.calculator.Constructors;
import software.amazon.jsii.tests.calculator.DataRenderer;
Expand Down Expand Up @@ -50,6 +51,7 @@
import software.amazon.jsii.tests.calculator.ReturnsPrivateImplementationOfInterface;
import software.amazon.jsii.tests.calculator.StableStruct;
import software.amazon.jsii.tests.calculator.Statics;
import software.amazon.jsii.tests.calculator.StructWithJavaReservedWords;
import software.amazon.jsii.tests.calculator.Sum;
import software.amazon.jsii.tests.calculator.SyncVirtualMethods;
import software.amazon.jsii.tests.calculator.UnionProperties;
Expand All @@ -73,6 +75,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -1018,9 +1021,49 @@ public void consts() {
@Test
public void reservedKeywordsAreSlugifiedInMethodNames() {
JavaReservedWords obj = new JavaReservedWords();
obj.import_();
obj.const_();
assertEquals("hello", obj.getWhile()); // properties do not need to be slufieid
obj.doImport();
obj.doConst();
assertEquals("hello", obj.getWhileValue()); // properties should also be 'slugified'
}

@Test
public void reservedKeywordsAreSlugifiedInStructProperties() {
StructWithJavaReservedWords struct = StructWithJavaReservedWords.builder()
.assertValue("one")
.defaultValue("two")
.build();

assertEquals("one", struct.getAssertValue());
assertEquals("two", struct.getDefaultValue());
}

@Test
public void reservedKeywordsAreSlugifiedInClassProperties() {
ClassWithJavaReservedWords obj = new ClassWithJavaReservedWords("one");

String result = obj.doImport("two");

assertEquals("onetwo", result);
}

@Test
public void hashCodeIsResistantToPropertyShadowingResultVariable() {
StructWithJavaReservedWords first = StructWithJavaReservedWords.builder().defaultValue("one").build();
StructWithJavaReservedWords second = StructWithJavaReservedWords.builder().defaultValue("one").build();
StructWithJavaReservedWords third = StructWithJavaReservedWords.builder().defaultValue("two").build();

assertEquals(first.hashCode(), second.hashCode());
assertNotEquals(first.hashCode(), third.hashCode());
}

@Test
public void equalsIsResistantToPropertyShadowingResultVariable() {
StructWithJavaReservedWords first = StructWithJavaReservedWords.builder().defaultValue("one").build();
StructWithJavaReservedWords second = StructWithJavaReservedWords.builder().defaultValue("one").build();
StructWithJavaReservedWords third = StructWithJavaReservedWords.builder().defaultValue("two").build();

assertEquals(first, second);
assertNotEquals(first, third);
}

@Test
Expand Down
Loading

0 comments on commit 32bc117

Please sign in to comment.