Skip to content

Commit

Permalink
fix(java): correctly search for protected override implementations (#905
Browse files Browse the repository at this point in the history
)

The code looking for overrides implementations was using the
`Class#getMethods` method, but this only considers `public` members of
the class. Chanegd to `#getDeclaredMethods` which accounts for *all* the
methods.

Fixes #903
  • Loading branch information
RomainMuller authored Oct 24, 2019
1 parent 8bacfb1 commit e3f0f6c
Show file tree
Hide file tree
Showing 23 changed files with 1,999 additions and 2,755 deletions.
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.*;
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

0 comments on commit e3f0f6c

Please sign in to comment.