-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shrinker-test examples with never-referenced constructors
R8 can do some optimizations that assume values of types whose constructors are never referenced are `null`. (A bit confused why the rules here are sufficient to avoid that optimization in the case of a class whose sole constructor has arguments and is unreferenced, since the keep rule only refers to `<init>()` instead of `<init>(...)`. Does R8 have special behavior for `<init>()` where attempting to keep that makes it assume a class is constructable even if it doesn't have a no-args constructor?) Also rename some test classes/names to refer to `NoArgs` instead of `Default` constructors. The examples here care about whether a no-argument constructor is present. While a no-argument constructor (explicitly defined or not) is used by default in some circumstances, the Java documentation consistently uses "default constructor" to refer only to constructors whose implementation is implicitly provided by the complier (all default constructors are no-argument constructors, but not vice versa).
- Loading branch information
Showing
10 changed files
with
137 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
shrinker-test/src/main/java/com/example/ClassWithUnreferencedHasArgsConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Class without no-args constructor, but with field annotated with | ||
* {@link SerializedName}. The constructor should not actually be used in the | ||
* code, but this shouldn't lead to R8 concluding that values of the type are | ||
* not constructible and therefore must be null. | ||
*/ | ||
public class ClassWithUnreferencedHasArgsConstructor { | ||
@SerializedName("myField") | ||
public int i; | ||
|
||
// Specify explicit constructor with args to remove implicit no-args default constructor | ||
public ClassWithUnreferencedHasArgsConstructor(int i) { | ||
this.i = i; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
shrinker-test/src/main/java/com/example/ClassWithUnreferencedNoArgsConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Class with no-args constructor and with field annotated with | ||
* {@link SerializedName}. The constructor should not actually be used in the | ||
* code, but this shouldn't lead to R8 concluding that values of the type are | ||
* not constructible and therefore must be null. | ||
*/ | ||
public class ClassWithUnreferencedNoArgsConstructor { | ||
@SerializedName("myField") | ||
public int i; | ||
|
||
public ClassWithUnreferencedNoArgsConstructor() { | ||
i = -3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters