Skip to content

Commit f798bde

Browse files
rlublecopybara-github
authored andcommitted
Add the call to preserve field for the autovalue optimization to the primary constructor instead of the last one.
PiperOrigin-RevId: 863377837
1 parent 714fb57 commit f798bde

16 files changed

+27
-23
lines changed

transpiler/java/com/google/j2cl/transpiler/ast/Type.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
21+
import static com.google.common.base.Predicates.not;
2122
import static com.google.common.collect.ImmutableList.toImmutableList;
2223

2324
import com.google.common.base.Predicates;
@@ -314,6 +315,14 @@ public Method getDefaultConstructor() {
314315
.orElse(null);
315316
}
316317

318+
/** Returns the primary constructor if the the type has one. */
319+
@Nullable
320+
public Method getPrimaryConstructor() {
321+
var superDelegatingConstructors =
322+
getConstructors().stream().filter(not(AstUtils::hasThisCall)).collect(toImmutableList());
323+
return superDelegatingConstructors.size() != 1 ? null : superDelegatingConstructors.getFirst();
324+
}
325+
317326
@Override
318327
public String getSimpleJsName() {
319328
return typeDeclaration.getSimpleJsName();

transpiler/java/com/google/j2cl/transpiler/passes/JsInteropRestrictionsChecker.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static com.google.common.base.Preconditions.checkState;
20-
import static com.google.common.collect.ImmutableList.toImmutableList;
2120
import static com.google.common.collect.ImmutableMap.toImmutableMap;
2221
import static com.google.common.collect.MoreCollectors.onlyElement;
2322
import static com.google.j2cl.transpiler.ast.TypeDescriptors.isJavaLangRecord;
@@ -2102,13 +2101,8 @@ private static MethodDescriptor getPrimaryConstructorDescriptor(final Type type)
21022101
.collect(onlyElement());
21032102
}
21042103

2105-
ImmutableList<Method> superDelegatingConstructors =
2106-
type.getConstructors().stream()
2107-
.filter(Predicates.not(AstUtils::hasThisCall))
2108-
.collect(toImmutableList());
2109-
return superDelegatingConstructors.size() != 1
2110-
? null
2111-
: superDelegatingConstructors.getFirst().getDescriptor();
2104+
var primaryConstructor = type.getPrimaryConstructor();
2105+
return primaryConstructor != null ? primaryConstructor.getDescriptor() : null;
21122106
}
21132107

21142108
private void checkJsConstructorSubtype(Type type) {

transpiler/java/com/google/j2cl/transpiler/passes/OptimizeAutoValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ private static void preserveFields(Type type, Collection<FieldDescriptor> exclud
535535
.build()
536536
.makeStatement(SourcePosition.NONE);
537537

538-
// Hack: Using the last constructor here since AutoValue constructor is appended to end.
539-
Iterables.getLast(type.getConstructors()).getBody().getStatements().add(preserveCall);
538+
// Add the call to preserve fields in the primary constructor since all other constructors
539+
// will delegate to it.
540+
type.getPrimaryConstructor().getBody().getStatements().add(preserveCall);
540541
}
541542

542543
private static void addExcludedFieldsDeclaration(

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_closure/AutoValueJsType.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AutoValueJsType extends ValueType {
2222
/** @nodts */
2323
$ctor__autovalue_AutoValueJsType__void() {
2424
this.$ctor__javaemul_internal_ValueType__void();
25+
$J2CL_PRESERVE$(this.f_field__autovalue_AutoValue_AutoValueJsType_, this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_);
2526
}
2627
/** @nodts @return {!AutoValueJsType} */
2728
static $create__int__int(/** number */ field, /** number */ withJsMethod) {
@@ -35,7 +36,6 @@ class AutoValueJsType extends ValueType {
3536
this.$ctor__autovalue_AutoValueJsType__void();
3637
this.f_field__autovalue_AutoValue_AutoValueJsType_ = field;
3738
this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_ = withJsMethod;
38-
$J2CL_PRESERVE$(this.f_field__autovalue_AutoValue_AutoValueJsType_, this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_);
3939
}
4040
/** @return {number} */
4141
getField() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_closure/DefaultConstructorAutoValue.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class DefaultConstructorAutoValue extends ValueType {
2222
/** @nodts */
2323
$ctor__autovalue_DefaultConstructorAutoValue__void() {
2424
this.$ctor__javaemul_internal_ValueType__void();
25+
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_DefaultConstructorAutoValue_);
2526
}
2627
/** @nodts @return {DefaultConstructorAutoValue} */
2728
static m_create__autovalue_DefaultConstructorAutoValue() {
@@ -39,7 +40,6 @@ class DefaultConstructorAutoValue extends ValueType {
3940
$ctor__autovalue_DefaultConstructorAutoValue__boolean__void(/** boolean */ booleanField) {
4041
this.$ctor__autovalue_DefaultConstructorAutoValue__void();
4142
this.f_booleanField__autovalue_AutoValue_DefaultConstructorAutoValue_ = booleanField;
42-
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_DefaultConstructorAutoValue_);
4343
}
4444
/** @nodts @return {boolean} */
4545
m_getBooleanField__boolean() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_closure/DefaultNonEmptyConstructorAutoValue.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class DefaultNonEmptyConstructorAutoValue extends ValueType {
2626
$ctor__autovalue_DefaultNonEmptyConstructorAutoValue__void() {
2727
this.$ctor__javaemul_internal_ValueType__void();
2828
this.f_field__autovalue_DefaultNonEmptyConstructorAutoValue_ = 1;
29+
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_DefaultNonEmptyConstructorAutoValue_);
2930
}
3031
/** @nodts @return {DefaultNonEmptyConstructorAutoValue} */
3132
static m_create__autovalue_DefaultNonEmptyConstructorAutoValue() {
@@ -43,7 +44,6 @@ class DefaultNonEmptyConstructorAutoValue extends ValueType {
4344
$ctor__autovalue_DefaultNonEmptyConstructorAutoValue__boolean__void(/** boolean */ booleanField) {
4445
this.$ctor__autovalue_DefaultNonEmptyConstructorAutoValue__void();
4546
this.f_booleanField__autovalue_AutoValue_DefaultNonEmptyConstructorAutoValue_ = booleanField;
46-
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_DefaultNonEmptyConstructorAutoValue_);
4747
}
4848
/** @nodts @return {boolean} */
4949
m_getBooleanField__boolean() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_j2kt_web/AutoValueJsType.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AutoValueJsType extends ValueType {
2222
/** @nodts */
2323
$ctor__autovalue_AutoValueJsType__void() {
2424
this.$ctor__javaemul_internal_ValueType__void();
25+
$J2CL_PRESERVE$(this.f_field__autovalue_AutoValue_AutoValueJsType_, this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_);
2526
}
2627
/** @nodts @return {!AutoValueJsType} */
2728
static $create__int__int(/** number */ field, /** number */ withJsMethod) {
@@ -35,7 +36,6 @@ class AutoValueJsType extends ValueType {
3536
this.$ctor__autovalue_AutoValueJsType__void();
3637
this.f_field__autovalue_AutoValue_AutoValueJsType_ = field;
3738
this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_ = withJsMethod;
38-
$J2CL_PRESERVE$(this.f_field__autovalue_AutoValue_AutoValueJsType_, this.f_withJsMethod__autovalue_AutoValue_AutoValueJsType_);
3939
}
4040
/** @return {number} */
4141
getField() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_j2kt_web/AutoValueWithBuilder.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AutoValueWithBuilder extends ValueType {
2424
/** @nodts */
2525
$ctor__autovalue_AutoValueWithBuilder__void() {
2626
this.$ctor__javaemul_internal_ValueType__void();
27+
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_AutoValueWithBuilder_, this.f_nullableField__autovalue_AutoValue_AutoValueWithBuilder_);
2728
}
2829
/** @nodts @return {AutoValueWithBuilder} */
2930
static m_create_pp_autovalue__autovalue_AutoValueWithBuilder() {
@@ -42,7 +43,6 @@ class AutoValueWithBuilder extends ValueType {
4243
this.$ctor__autovalue_AutoValueWithBuilder__void();
4344
this.f_booleanField__autovalue_AutoValue_AutoValueWithBuilder_ = booleanField;
4445
this.f_nullableField__autovalue_AutoValue_AutoValueWithBuilder_ = nullableField;
45-
$J2CL_PRESERVE$(this.f_booleanField__autovalue_AutoValue_AutoValueWithBuilder_, this.f_nullableField__autovalue_AutoValue_AutoValueWithBuilder_);
4646
}
4747
/** @nodts @return {boolean} */
4848
m_getBooleanField__boolean() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_j2kt_web/AutoValueWithExtensions.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class AutoValueWithExtensions extends ValueType {
2121
/** @nodts */
2222
$ctor__autovalue_AutoValueWithExtensions__void() {
2323
this.$ctor__javaemul_internal_ValueType__void();
24+
$J2CL_PRESERVE$(this.f_intField__autovalue____AutoValue_AutoValueWithExtensions_, this.f_stringField__autovalue____AutoValue_AutoValueWithExtensions_);
2425
}
2526
/** @abstract @nodts @return {?string} */
2627
m_toPrettyString_pp_autovalue__java_lang_String() {}
@@ -35,7 +36,6 @@ class AutoValueWithExtensions extends ValueType {
3536
this.f_intField__autovalue____AutoValue_AutoValueWithExtensions_ = intField;
3637
Objects.m_requireNonNull__java_lang_Object__java_lang_Object(stringField);
3738
this.f_stringField__autovalue____AutoValue_AutoValueWithExtensions_ = stringField;
38-
$J2CL_PRESERVE$(this.f_intField__autovalue____AutoValue_AutoValueWithExtensions_, this.f_stringField__autovalue____AutoValue_AutoValueWithExtensions_);
3939
}
4040
/** @nodts @return {number} */
4141
m_getIntField__int() {

transpiler/javatests/com/google/j2cl/readable/java/autovalue/output_j2kt_web/AutoValueWithFields.impl.java.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AutoValueWithFields extends Parent {
2929
/** @nodts */
3030
$ctor__autovalue_AutoValueWithFields__void() {
3131
this.$ctor__autovalue_Parent__void();
32+
$J2CL_PRESERVE$(this.f_intField__autovalue_AutoValue_AutoValueWithFields_);
3233
}
3334
/** @nodts @return {!AutoValueWithFields} */
3435
static $create__int(/** number */ intField) {
@@ -41,7 +42,6 @@ class AutoValueWithFields extends Parent {
4142
$ctor__autovalue_AutoValueWithFields__int__void(/** number */ intField) {
4243
this.$ctor__autovalue_AutoValueWithFields__void();
4344
this.f_intField__autovalue_AutoValue_AutoValueWithFields_ = intField;
44-
$J2CL_PRESERVE$(this.f_intField__autovalue_AutoValue_AutoValueWithFields_);
4545
}
4646
/** @nodts @return {number} */
4747
m_getIntField__int() {

0 commit comments

Comments
 (0)