Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): correctly search for protected override implementations #905

Merged
merged 4 commits into from
Oct 24, 2019
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
20 changes: 20 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,3 +1880,23 @@ export class ClassWithCollections {
return {'key1': 'value1', 'key2': 'value2'};
}
}

/**
* @see https://github.com/aws/jsii/issues/903
*/
export class OverridableProtectedMember {
protected readonly overrideReadOnly: string = 'Baz';
protected overrideReadWrite: string = 'zinga!';

public valueFromProtected(): string {
return this.overrideMe();
}

public switchModes(): void {
this.overrideReadWrite = 'zaar...';
}

protected overrideMe(): string {
return this.overrideReadOnly + this.overrideReadWrite;
}
}
91 changes: 90 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -6846,6 +6846,95 @@
}
]
},
"jsii-calc.OverridableProtectedMember": {
"assembly": "jsii-calc",
"docs": {
"see": "https://github.com/aws/jsii/issues/903",
"stability": "experimental"
},
"fqn": "jsii-calc.OverridableProtectedMember",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1887
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1899
},
"name": "overrideMe",
"protected": true,
"returns": {
"type": {
"primitive": "string"
}
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1895
},
"name": "switchModes"
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1891
},
"name": "valueFromProtected",
"returns": {
"type": {
"primitive": "string"
}
}
}
],
"name": "OverridableProtectedMember",
"properties": [
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1888
},
"name": "overrideReadOnly",
"protected": true,
"type": {
"primitive": "string"
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1889
},
"name": "overrideReadWrite",
"protected": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.OverrideReturnsObject": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -9610,5 +9699,5 @@
}
},
"version": "0.19.0",
"fingerprint": "nQMF6ipA96E38PvG3NsgKOJIb3ZEit4jNrxZAkgRlbs="
"fingerprint": "KFKFyrJ+3+cfNKWPiNetngWkPFI32ydxfJyaR43/wDI="
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;
import software.amazon.jsii.JsiiEngine;
import software.amazon.jsii.JsiiException;
import software.amazon.jsii.tests.calculator.AbstractClass;
import software.amazon.jsii.tests.calculator.AbstractClassReturner;
import software.amazon.jsii.tests.calculator.Add;
import software.amazon.jsii.tests.calculator.AllTypes;
import software.amazon.jsii.tests.calculator.AllTypesEnum;
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.ClassWithCollections;
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;
import software.amazon.jsii.tests.calculator.DerivedStruct;
import software.amazon.jsii.tests.calculator.DiamondInheritanceTopLevelStruct;
import software.amazon.jsii.tests.calculator.DoNotOverridePrivates;
import software.amazon.jsii.tests.calculator.DoubleTrouble;
import software.amazon.jsii.tests.calculator.EnumDispenser;
import software.amazon.jsii.tests.calculator.EraseUndefinedHashValues;
import software.amazon.jsii.tests.calculator.EraseUndefinedHashValuesOptions;
import software.amazon.jsii.tests.calculator.GiveMeStructs;
import software.amazon.jsii.tests.calculator.GreetingAugmenter;
import software.amazon.jsii.tests.calculator.IFriendlier;
import software.amazon.jsii.tests.calculator.IFriendlyRandomGenerator;
import software.amazon.jsii.tests.calculator.IInterfaceWithProperties;
import software.amazon.jsii.tests.calculator.IPublicInterface;
import software.amazon.jsii.tests.calculator.IRandomNumberGenerator;
import software.amazon.jsii.tests.calculator.InbetweenClass;
import software.amazon.jsii.tests.calculator.IInterfaceImplementedByAbstractClass;
import software.amazon.jsii.tests.calculator.JSObjectLiteralForInterface;
import software.amazon.jsii.tests.calculator.JSObjectLiteralToNative;
import software.amazon.jsii.tests.calculator.JSObjectLiteralToNativeClass;
import software.amazon.jsii.tests.calculator.JavaReservedWords;
import software.amazon.jsii.tests.calculator.JsiiAgent;
import software.amazon.jsii.tests.calculator.Multiply;
import software.amazon.jsii.tests.calculator.Negate;
import software.amazon.jsii.tests.calculator.NodeStandardLibrary;
import software.amazon.jsii.tests.calculator.NullShouldBeTreatedAsUndefined;
import software.amazon.jsii.tests.calculator.NullShouldBeTreatedAsUndefinedData;
import software.amazon.jsii.tests.calculator.NumberGenerator;
import software.amazon.jsii.tests.calculator.OptionalStruct;
import software.amazon.jsii.tests.calculator.PartiallyInitializedThisConsumer;
import software.amazon.jsii.tests.calculator.Polymorphism;
import software.amazon.jsii.tests.calculator.Power;
import software.amazon.jsii.tests.calculator.PublicClass;
import software.amazon.jsii.tests.calculator.ReferenceEnumFromScopedPackage;
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;
import software.amazon.jsii.tests.calculator.UsesInterfaceWithProperties;
import software.amazon.jsii.tests.calculator.VariadicMethod;
import software.amazon.jsii.tests.calculator.*;
eladb marked this conversation as resolved.
Show resolved Hide resolved
import software.amazon.jsii.tests.calculator.composition.CompositeOperation;
import software.amazon.jsii.tests.calculator.lib.EnumFromScopedModule;
import software.amazon.jsii.tests.calculator.lib.IFriendly;
import software.amazon.jsii.tests.calculator.lib.MyFirstStruct;
import software.amazon.jsii.tests.calculator.lib.Number;
import software.amazon.jsii.tests.calculator.lib.StructWithOnlyOptionals;
import software.amazon.jsii.tests.calculator.lib.Value;
import software.amazon.jsii.tests.calculator.ConstructorPassesThisOut;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -88,6 +34,7 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("deprecated")
public class ComplianceTest {
/**
* Verify that we can marshal and unmarshal objects without type information.
Expand Down Expand Up @@ -640,10 +587,16 @@ public void syncOverrides_callsSuper() {

@Test(expected = JsiiException.class)
public void fail_syncOverrides_callsDoubleAsync_method() {
SyncOverrides obj = new SyncOverrides();
obj.callAsync = true;
try {
JsiiEngine.setQuietMode(true);

SyncOverrides obj = new SyncOverrides();
obj.callAsync = true;

obj.callerIsMethod();
obj.callerIsMethod();
} finally {
JsiiEngine.setQuietMode(false);
}
}

@Test(expected = JsiiException.class)
Expand Down Expand Up @@ -1360,6 +1313,48 @@ public void mapReturnedByMethodCanBeRead() {
assertThat(result.size(), is(2));
}

@Test
public void canOverrideProtectedMethod() {
final String challenge = "Cthulhu Fhtagn!";
final OverridableProtectedMember overridden = new OverridableProtectedMember() {
@Override
protected String overrideMe() {
return challenge;
}
};
assertEquals(challenge, overridden.valueFromProtected());
}

@Test
public void canOverrideProtectedGetter() {
final String challenge = "Cthulhu Fhtagn!";
final OverridableProtectedMember overridden = new OverridableProtectedMember() {
@Override
protected String getOverrideReadOnly() {
return "Cthulhu ";
}

@Override
protected String getOverrideReadWrite() {
return "Fhtagn!";
}
};
assertEquals(challenge, overridden.valueFromProtected());
}

@Test
public void canOverrideProtectedSetter() {
final String challenge = "Bazzzzzzzzzzzaar...";
final OverridableProtectedMember overridden = new OverridableProtectedMember() {
@Override
protected void setOverrideReadWrite(String value) {
super.setOverrideReadWrite("zzzzzzzzz" + value);
}
};
overridden.switchModes();
assertEquals(challenge, overridden.valueFromProtected());
}

static class PartiallyInitializedThisConsumerImpl extends PartiallyInitializedThisConsumer {
@Override
public String consumePartiallyInitializedThis(final ConstructorPassesThisOut obj,
Expand Down
Loading