forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new @CustomInject annotation to control when the Application is…
… injected. Fixes #3122. RELNOTES=Add @CustomInject PiperOrigin-RevId: 419019914
- Loading branch information
1 parent
667f1c5
commit 36c17bb
Showing
11 changed files
with
338 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
java/dagger/hilt/android/internal/migration/HasCustomInject.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,25 @@ | ||
/* | ||
* Copyright (C) 2020 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.internal.migration; | ||
|
||
/** | ||
* Do not use except in Hilt generated code. Internal interface for application's using | ||
* {@code CustomInject}. | ||
*/ | ||
public interface HasCustomInject { | ||
void customInject(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.migration; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* When used on a {@link dagger.hilt.android.HiltAndroidApp}-annotated application, this causes the | ||
* application to no longer inject itself in onCreate and instead allows it to be injected at some | ||
* other time. | ||
* | ||
* <p>When using this annotation, you can use {@link CustomInjection#inject} to inject the | ||
* application class. Additionally, this annotation will also cause a method, {@code customInject} | ||
* to be generated in the Hilt base class as well, that behaves the same as | ||
* {@link CustomInjection#inject}. The method is available to users that extend the Hilt base class | ||
* directly and don't use the Gradle plugin. | ||
* | ||
* <p> Example usage: | ||
* | ||
* <pre><code> | ||
* {@literal @}CustomInject | ||
* {@literal @}HiltAndroidApp(Application.class) | ||
* public final class MyApplication extends Hilt_MyApplication { | ||
* | ||
* {@literal @}Inject Foo foo; | ||
* | ||
* {@literal @}Override | ||
* public void onCreate() { | ||
* // Injection would normally happen in this super.onCreate() call, but won't now because this | ||
* // is using CustomInject. | ||
* super.onCreate(); | ||
* doSomethingBeforeInjection(); | ||
* // This call now injects the fields in the Application, like the foo field above. | ||
* CustomInject.inject(this); | ||
* } | ||
* } | ||
* </code></pre> | ||
*/ | ||
@Target(ElementType.TYPE) | ||
public @interface CustomInject {} |
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,43 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.migration; | ||
|
||
import android.app.Application; | ||
import androidx.annotation.NonNull; | ||
import dagger.hilt.android.internal.migration.HasCustomInject; | ||
import dagger.hilt.internal.Preconditions; | ||
|
||
/** | ||
* Utility methods for injecting the application when using {@link CustomInject}. | ||
* | ||
* @see OptionalInject | ||
*/ | ||
public final class CustomInjection { | ||
|
||
/** Injects the passed in application. */ | ||
public static void inject(@NonNull Application app) { | ||
Preconditions.checkNotNull(app); | ||
Preconditions.checkArgument( | ||
app instanceof HasCustomInject, | ||
"'%s' is not a custom inject application. Check that you have annotated" | ||
+ " the application with both @HiltAndroidApp and @CustomInject.", | ||
app.getClass()); | ||
((HasCustomInject) app).customInject(); | ||
} | ||
|
||
private CustomInjection() {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android; | ||
|
||
import android.app.Application; | ||
import dagger.Module; | ||
import dagger.Provides; | ||
import dagger.hilt.InstallIn; | ||
import dagger.hilt.android.migration.CustomInject; | ||
import dagger.hilt.android.migration.CustomInjection; | ||
import dagger.hilt.components.SingletonComponent; | ||
import javax.inject.Inject; | ||
|
||
/** | ||
* Classes for CustomInjectTest. This is in a separate build target because otherwise | ||
* robolectric does not recognize the application class as extending application due to order of | ||
* class generation. | ||
*/ | ||
final class CustomInjectClasses { | ||
|
||
@Module | ||
@InstallIn(SingletonComponent.class) | ||
static final class TestModule { | ||
@Provides | ||
static Integer provideInt() { | ||
return 9; | ||
} | ||
} | ||
|
||
@CustomInject | ||
@HiltAndroidApp(Application.class) | ||
static final class TestApplication extends Hilt_CustomInjectClasses_TestApplication { | ||
|
||
@Inject Integer intValue; | ||
|
||
void inject() { | ||
CustomInjection.inject(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.