Skip to content

Commit

Permalink
Wrap static _Factory.INSTANCE fields in an InstanceHolder class.
Browse files Browse the repository at this point in the history
This allows Proguard to freely move the static newInstance methods into other classes and avoids class loading the _Factory class when it's not needed.

RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=273320029
  • Loading branch information
bcorso authored and nick-someone committed Oct 8, 2019
1 parent 0d9fdb3 commit a258d37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
11 changes: 8 additions & 3 deletions java/dagger/internal/codegen/writing/FactoryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ private void addCreateMethod(ProvisionBinding binding, TypeSpec.Builder factoryB
if (!bindingTypeElementTypeVariableNames(binding).isEmpty()) {
// If the factory has type parameters, ignore them in the field declaration & initializer
instanceFieldBuilder.addAnnotation(suppressWarnings(RAWTYPES));

createMethodBuilder.addAnnotation(suppressWarnings(UNCHECKED));
}
createMethodBuilder.addStatement("return INSTANCE");
factoryBuilder.addField(instanceFieldBuilder.build());

ClassName instanceHolderName = nameGeneratedType(binding).nestedClass("InstanceHolder");
createMethodBuilder.addStatement("return $T.INSTANCE", instanceHolderName);
factoryBuilder.addType(
TypeSpec.classBuilder(instanceHolderName)
.addModifiers(PRIVATE, STATIC, FINAL)
.addField(instanceFieldBuilder.build())
.build());
break;
case CLASS_CONSTRUCTOR:
List<ParameterSpec> params = constructorParams(binding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,24 @@ public final class InjectConstructorFactoryGeneratorTest {
"",
GENERATED_CODE_ANNOTATIONS,
"public final class GenericClass_Factory<T> implements Factory<GenericClass<T>> {",
" @SuppressWarnings(\"rawtypes\")",
" private static final GenericClass_Factory INSTANCE = new GenericClass_Factory();",
"",
" @Override",
" public GenericClass<T> get() {",
" return newInstance();",
" }",
"",
" @SuppressWarnings(\"unchecked\")",
" public static <T> GenericClass_Factory<T> create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static <T> GenericClass<T> newInstance() {",
" return new GenericClass<T>();",
" }",
"",
" private static final class InstanceHolder {",
" @SuppressWarnings(\"rawtypes\")",
" private static final GenericClass_Factory INSTANCE = new GenericClass_Factory();",
" }",
"}");
assertAbout(javaSource()).that(file)
.processedWith(new ComponentProcessor())
Expand Down Expand Up @@ -1399,19 +1401,21 @@ public void noDeps() {
"",
GENERATED_CODE_ANNOTATIONS,
"public final class SimpleType_Factory implements Factory<SimpleType> {",
" private static final SimpleType_Factory INSTANCE = new SimpleType_Factory();",
"",
" @Override public SimpleType get() {",
" return newInstance();",
" }",
"",
" public static SimpleType_Factory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static SimpleType newInstance() {",
" return new SimpleType();",
" }",
"",
" private static final class InstanceHolder {",
" private static final SimpleType_Factory INSTANCE = new SimpleType_Factory();",
" }",
"}");
assertAbout(javaSource())
.that(simpleType)
Expand Down Expand Up @@ -1445,19 +1449,21 @@ public void noDeps() {
"",
GENERATED_CODE_ANNOTATIONS,
"public final class OuterType_A_Factory implements Factory<OuterType.A> {",
" private static final OuterType_A_Factory INSTANCE = new OuterType_A_Factory();",
"",
" @Override public OuterType.A get() {",
" return newInstance();",
" }",
"",
" public static OuterType_A_Factory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static OuterType.A newInstance() {",
" return new OuterType.A();",
" }",
"",
" private static final class InstanceHolder {",
" private static final OuterType_A_Factory INSTANCE = new OuterType_A_Factory();",
" }",
"}");
assertAbout(javaSources()).that(ImmutableList.of(nestedTypesFile))
.processedWith(new ComponentProcessor())
Expand Down
24 changes: 14 additions & 10 deletions javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1114,22 +1114,24 @@ public void genericSubclassedModule() {
GENERATED_CODE_ANNOTATIONS,
"public final class ParameterizedModule_ProvideMapStringNumberFactory",
" implements Factory<Map<String, Number>> {",
" private static final ParameterizedModule_ProvideMapStringNumberFactory INSTANCE =",
" new ParameterizedModule_ProvideMapStringNumberFactory();",
"",
" @Override",
" public Map<String, Number> get() {",
" return provideMapStringNumber();",
" }",
"",
" public static ParameterizedModule_ProvideMapStringNumberFactory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static Map<String, Number> provideMapStringNumber() {",
" return Preconditions.checkNotNull(ParameterizedModule.provideMapStringNumber(),",
" " + NPE_FROM_PROVIDES_METHOD + ");",
" }",
"",
" private static final class InstanceHolder {",
" private static final ParameterizedModule_ProvideMapStringNumberFactory INSTANCE =",
" new ParameterizedModule_ProvideMapStringNumberFactory();",
" }",
"}");

JavaFileObject provideNonGenericTypeFactory =
Expand All @@ -1144,22 +1146,24 @@ public void genericSubclassedModule() {
GENERATED_CODE_ANNOTATIONS,
"public final class ParameterizedModule_ProvideNonGenericTypeFactory",
" implements Factory<Object> {",
" private static final ParameterizedModule_ProvideNonGenericTypeFactory INSTANCE = ",
" new ParameterizedModule_ProvideNonGenericTypeFactory();",
"",
" @Override",
" public Object get() {",
" return provideNonGenericType();",
" }",
"",
" public static ParameterizedModule_ProvideNonGenericTypeFactory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static Object provideNonGenericType() {",
" return Preconditions.checkNotNull(ParameterizedModule.provideNonGenericType(),",
" " + NPE_FROM_PROVIDES_METHOD + ");",
" }",
"",
" private static final class InstanceHolder {",
" private static final ParameterizedModule_ProvideNonGenericTypeFactory INSTANCE =",
" new ParameterizedModule_ProvideNonGenericTypeFactory();",
" }",
"}");

JavaFileObject provideNonGenericTypeWithDepsFactory =
Expand Down Expand Up @@ -1418,7 +1422,7 @@ public void proxyMethodsConflictWithOtherFactoryMethods() {
" }",
"",
" public static TestModule_GetFactory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static int proxyGet() {",
Expand All @@ -1441,7 +1445,7 @@ public void proxyMethodsConflictWithOtherFactoryMethods() {
" }",
"",
" public static TestModule_CreateFactory create() {",
" return INSTANCE;",
" return InstanceHolder.INSTANCE;",
" }",
"",
" public static boolean proxyCreate() {",
Expand Down

0 comments on commit a258d37

Please sign in to comment.