Skip to content
Permalink
Browse files

Add restriction check for constructor not delegating to the JsConstru…

…ctor in a JsConstructor class.

PiperOrigin-RevId: 241542975
  • Loading branch information...
rluble authored and Copybara-Service committed Apr 2, 2019
1 parent 5bb975f commit 1bd8a55b989467ff59787d3d858d66fe7b964741
@@ -1507,6 +1507,24 @@ private boolean checkJsConstructors(Type type) {
return false;
}

// TODO(b/129550499): Remove this check once NormalizeConstructors is fixed to handle arbitrary
// constructor delegation chains for JsConstructor classes.
for (Method constructor : type.getConstructors()) {
if (constructor.getDescriptor().isJsConstructor()) {
continue;
}
MethodDescriptor delegatedConstructor =
AstUtils.getConstructorInvocation(constructor).getTarget();
if (delegatedConstructor == null || !delegatedConstructor.isJsConstructor()) {
problems.error(
type.getSourcePosition(),
"Constructor '%s' should delegate to the JsConstructor '%s'. (b/129550499)",
constructor.getReadableDescription(),
jsConstructorDescriptor.getReadableDescription());
return false;
}
}

return true;
}

@@ -1091,6 +1091,15 @@ public void testJsConstructorBadSubclassFails() {
" @JsConstructor",
" public AnotherSubBuggyJsType(int a) { this(); }",
"}",
"class DelegatingConstructorChain extends BuggyJsType {",
// TODO(b/129550499): The following class should be fine because only the JsConstructor
// delegates to super. However the normalization pass does not handle those cases
// correctly.
" @JsConstructor",
" public DelegatingConstructorChain() { super();}",
" public DelegatingConstructorChain(int a) { this(); }",
" public DelegatingConstructorChain(int a, int b) { this(a); }",
"}",
"@JsType(isNative=true) class NativeType {",
" NativeType() { }",
" NativeType(int i) { }",
@@ -1134,7 +1143,9 @@ public void testJsConstructorBadSubclassFails() {
"More than one JsConstructor exists for 'AnotherSubBuggyJsType'.",
"JsConstructor 'OtherSubBuggyJsType(int)' can be a JsConstructor only if all "
+ "other constructors in the class delegate to it.",
"Class 'SomeClass2' should have a JsConstructor.");
"Class 'SomeClass2' should have a JsConstructor.",
"Constructor 'DelegatingConstructorChain(int a, int b)' should delegate to the"
+ " JsConstructor 'DelegatingConstructorChain()'. (b/129550499)");
}

public void testMultipleConstructorsNotAllDelegatedToJsConstructorFails() {

0 comments on commit 1bd8a55

Please sign in to comment.
You can’t perform that action at this time.