From 5b92abdc4762f788b1fdfc3ad94ab64684c527c9 Mon Sep 17 00:00:00 2001 From: Anthony Faucogney Date: Mon, 30 Nov 2020 22:48:17 +0100 Subject: [PATCH 1/2] add androidx.annotation dep add nullability annotations to Toothpick api that is used by KTP --- build.gradle | 1 + deps.gradle | 2 + smoothie-androidx/build.gradle | 4 -- .../SmoothieAndroidXActivityModule.java | 3 +- .../AndroidXFragmentManagerProvider.java | 4 +- .../AndroidXLoaderManagerProvider.java | 4 +- .../smoothie/viewmodel/ViewModelProvider.java | 1 + .../smoothie/viewmodel/ViewModelUtil.java | 5 +-- smoothie-support/build.gradle | 4 -- smoothie/build.gradle | 6 +-- .../module/SmoothieActivityModule.java | 3 +- .../module/SmoothieApplicationModule.java | 11 +++-- .../provider/AccountManagerProvider.java | 4 +- .../provider/AssetManagerProvider.java | 4 +- .../provider/ContentResolverProvider.java | 4 +- .../provider/FragmentManagerProvider.java | 4 +- .../smoothie/provider/HandlerProvider.java | 2 + .../provider/LayoutInflaterProvider.java | 4 +- .../provider/LoaderManagerProvider.java | 4 +- .../provider/PackageManagerProvider.java | 4 +- .../smoothie/provider/ResourcesProvider.java | 4 +- .../provider/SharedPreferencesProvider.java | 8 +++- .../provider/SystemServiceProvider.java | 4 +- toothpick-runtime/build.gradle | 1 + .../src/main/java/toothpick/InjectorImpl.java | 3 +- .../src/main/java/toothpick/Toothpick.java | 27 +++++------ .../configuration/Configuration.java | 13 ++++-- .../RuntimeCheckConfiguration.java | 7 +-- toothpick/build.gradle | 1 + .../src/main/java/toothpick/Injector.java | 4 +- .../main/java/toothpick/MemberInjector.java | 4 +- toothpick/src/main/java/toothpick/Scope.java | 45 +++++++++++++------ .../main/java/toothpick/config/Binding.java | 30 ++++++++++--- .../main/java/toothpick/config/Module.java | 5 ++- 34 files changed, 155 insertions(+), 79 deletions(-) diff --git a/build.gradle b/build.gradle index 3d0e09e6..2bf75a69 100644 --- a/build.gradle +++ b/build.gradle @@ -81,6 +81,7 @@ subprojects { project -> repositories { jcenter() + google() } apply plugin: 'jacoco' diff --git a/deps.gradle b/deps.gradle index d5b0e735..7e923a95 100644 --- a/deps.gradle +++ b/deps.gradle @@ -9,6 +9,8 @@ ext.deps = [// Common android_plugin : 'com.android.tools.build:gradle:3.4.2', supportv4 : 'com.android.support:support-v4:28.0.0', androidxv7 : 'androidx.appcompat:appcompat:1.0.0', + androidxannotations : 'androidx.annotation:annotation:1.1.0', + fragment : 'androidx.fragment:fragment:1.1.0', design : 'com.google.android.material:material:1.0.0', coordlayout : 'androidx.coordinatorlayout:coordinatorlayout:1.0.0', diff --git a/smoothie-androidx/build.gradle b/smoothie-androidx/build.gradle index 08007cdb..d05c4a1d 100644 --- a/smoothie-androidx/build.gradle +++ b/smoothie-androidx/build.gradle @@ -1,9 +1,5 @@ apply plugin: 'com.android.library' -repositories { - google() -} - android { compileSdkVersion 28 diff --git a/smoothie-androidx/src/main/java/toothpick/smoothie/module/SmoothieAndroidXActivityModule.java b/smoothie-androidx/src/main/java/toothpick/smoothie/module/SmoothieAndroidXActivityModule.java index c65d167f..d1ecf293 100644 --- a/smoothie-androidx/src/main/java/toothpick/smoothie/module/SmoothieAndroidXActivityModule.java +++ b/smoothie-androidx/src/main/java/toothpick/smoothie/module/SmoothieAndroidXActivityModule.java @@ -18,6 +18,7 @@ import android.app.Activity; import android.view.LayoutInflater; +import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.loader.app.LoaderManager; @@ -27,7 +28,7 @@ import toothpick.smoothie.provider.LayoutInflaterProvider; public class SmoothieAndroidXActivityModule extends Module { - public SmoothieAndroidXActivityModule(FragmentActivity activity) { + public SmoothieAndroidXActivityModule(@NonNull FragmentActivity activity) { bind(Activity.class).toInstance(activity); bind(FragmentManager.class).toProviderInstance(new AndroidXFragmentManagerProvider(activity)); bind(LoaderManager.class).toProviderInstance(new AndroidXLoaderManagerProvider(activity)); diff --git a/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXFragmentManagerProvider.java b/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXFragmentManagerProvider.java index bc6db1db..937cfcab 100644 --- a/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXFragmentManagerProvider.java +++ b/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXFragmentManagerProvider.java @@ -17,6 +17,7 @@ package toothpick.smoothie.provider; import android.app.Activity; +import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import javax.inject.Provider; @@ -24,10 +25,11 @@ public class AndroidXFragmentManagerProvider implements Provider { Activity activity; - public AndroidXFragmentManagerProvider(Activity activity) { + public AndroidXFragmentManagerProvider(@NonNull Activity activity) { this.activity = activity; } + @NonNull @Override public FragmentManager get() { return ((FragmentActivity) activity).getSupportFragmentManager(); diff --git a/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXLoaderManagerProvider.java b/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXLoaderManagerProvider.java index 222eea5c..7f220ae0 100644 --- a/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXLoaderManagerProvider.java +++ b/smoothie-androidx/src/main/java/toothpick/smoothie/provider/AndroidXLoaderManagerProvider.java @@ -16,6 +16,7 @@ */ package toothpick.smoothie.provider; +import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import androidx.loader.app.LoaderManager; import javax.inject.Provider; @@ -23,10 +24,11 @@ public class AndroidXLoaderManagerProvider implements Provider { FragmentActivity activity; - public AndroidXLoaderManagerProvider(FragmentActivity activity) { + public AndroidXLoaderManagerProvider(@NonNull FragmentActivity activity) { this.activity = activity; } + @NonNull @Override public LoaderManager get() { return LoaderManager.getInstance(activity); diff --git a/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelProvider.java b/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelProvider.java index 82f9d4c2..b853e805 100644 --- a/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelProvider.java +++ b/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelProvider.java @@ -126,6 +126,7 @@ public ViewModelProvider(@NonNull Scope scope, @NonNull T viewModel) { this.scope = scope; } + @NonNull @Override public T get() { if (scope != null) { diff --git a/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelUtil.java b/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelUtil.java index be86dafa..f64cc1f8 100644 --- a/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelUtil.java +++ b/smoothie-lifecycle-viewmodel/src/main/java/toothpick/smoothie/viewmodel/ViewModelUtil.java @@ -113,7 +113,7 @@ public static void installViewModelBinding( private static class TPViewModelFactory implements Factory { private Scope scope; - private TPViewModelFactory(Scope scope) { + private TPViewModelFactory(@NonNull Scope scope) { this.scope = scope; } @@ -132,8 +132,7 @@ public T create(@NonNull Class modelClass) { private static class TPViewModel extends ViewModel { private Scope scope; - private TPViewModel(Scope scope) { - + private TPViewModel(@NonNull Scope scope) { this.scope = scope; } diff --git a/smoothie-support/build.gradle b/smoothie-support/build.gradle index 52d54b8e..2d58fb72 100644 --- a/smoothie-support/build.gradle +++ b/smoothie-support/build.gradle @@ -1,9 +1,5 @@ apply plugin: 'com.android.library' -repositories { - google() -} - android { compileSdkVersion 28 diff --git a/smoothie/build.gradle b/smoothie/build.gradle index cd218801..e31aab73 100644 --- a/smoothie/build.gradle +++ b/smoothie/build.gradle @@ -1,10 +1,5 @@ apply plugin: 'com.android.library' -repositories { - google() -} - - android { compileSdkVersion 28 @@ -30,6 +25,7 @@ android { dependencies { api project(':toothpick-runtime') compileOnly deps.inject + compileOnly deps.androidxannotations testImplementation project(':toothpick-testing') testImplementation deps.junit4 diff --git a/smoothie/src/main/java/toothpick/smoothie/module/SmoothieActivityModule.java b/smoothie/src/main/java/toothpick/smoothie/module/SmoothieActivityModule.java index 61d454dd..604fff0c 100644 --- a/smoothie/src/main/java/toothpick/smoothie/module/SmoothieActivityModule.java +++ b/smoothie/src/main/java/toothpick/smoothie/module/SmoothieActivityModule.java @@ -18,6 +18,7 @@ import android.app.Activity; import android.view.LayoutInflater; +import androidx.annotation.NonNull; import toothpick.config.Module; import toothpick.smoothie.provider.FragmentManagerProvider; import toothpick.smoothie.provider.LayoutInflaterProvider; @@ -25,7 +26,7 @@ @SuppressWarnings("deprecation") public class SmoothieActivityModule extends Module { - public SmoothieActivityModule(Activity activity) { + public SmoothieActivityModule(@NonNull Activity activity) { bind(Activity.class).toInstance(activity); bind(android.app.FragmentManager.class) .toProviderInstance(new FragmentManagerProvider(activity)); diff --git a/smoothie/src/main/java/toothpick/smoothie/module/SmoothieApplicationModule.java b/smoothie/src/main/java/toothpick/smoothie/module/SmoothieApplicationModule.java index 344a5c03..9a01b35e 100644 --- a/smoothie/src/main/java/toothpick/smoothie/module/SmoothieApplicationModule.java +++ b/smoothie/src/main/java/toothpick/smoothie/module/SmoothieApplicationModule.java @@ -60,6 +60,8 @@ import android.telephony.TelephonyManager; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import toothpick.config.Module; import toothpick.smoothie.provider.AccountManagerProvider; import toothpick.smoothie.provider.AssetManagerProvider; @@ -71,11 +73,12 @@ import toothpick.smoothie.provider.SystemServiceProvider; public class SmoothieApplicationModule extends Module { - public SmoothieApplicationModule(Application application) { + public SmoothieApplicationModule(@NonNull Application application) { this(application, null); } - public SmoothieApplicationModule(Application application, String preferencesName) { + public SmoothieApplicationModule( + @NonNull Application application, @Nullable String preferencesName) { bind(Application.class).toInstance(application); bind(AccountManager.class).toProviderInstance(new AccountManagerProvider(application)); bind(AssetManager.class).toProviderInstance(new AssetManagerProvider(application)); @@ -89,7 +92,7 @@ public SmoothieApplicationModule(Application application, String preferencesName bindPackageInfo(application); } - private void bindPackageInfo(Application application) { + private void bindPackageInfo(@NonNull Application application) { final PackageInfo packageInfo; try { packageInfo = application.getPackageManager().getPackageInfo(application.getPackageName(), 0); @@ -99,7 +102,7 @@ private void bindPackageInfo(Application application) { } } - private void bindSystemServices(Application application) { + private void bindSystemServices(@NonNull Application application) { bindSystemService(application, LocationManager.class, LOCATION_SERVICE); bindSystemService(application, WindowManager.class, WINDOW_SERVICE); bindSystemService(application, ActivityManager.class, ACTIVITY_SERVICE); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/AccountManagerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/AccountManagerProvider.java index 0f455207..0a3d253e 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/AccountManagerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/AccountManagerProvider.java @@ -18,15 +18,17 @@ import android.accounts.AccountManager; import android.app.Application; +import androidx.annotation.NonNull; import javax.inject.Provider; public class AccountManagerProvider implements Provider { Application application; - public AccountManagerProvider(Application application) { + public AccountManagerProvider(@NonNull Application application) { this.application = application; } + @NonNull @Override public AccountManager get() { return AccountManager.get(application); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/AssetManagerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/AssetManagerProvider.java index 087752b3..4c2d4ec3 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/AssetManagerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/AssetManagerProvider.java @@ -18,15 +18,17 @@ import android.app.Application; import android.content.res.AssetManager; +import androidx.annotation.NonNull; import javax.inject.Provider; public class AssetManagerProvider implements Provider { Application application; - public AssetManagerProvider(Application application) { + public AssetManagerProvider(@NonNull Application application) { this.application = application; } + @NonNull @Override public AssetManager get() { return application.getAssets(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/ContentResolverProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/ContentResolverProvider.java index 51bef794..144dd466 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/ContentResolverProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/ContentResolverProvider.java @@ -18,15 +18,17 @@ import android.app.Application; import android.content.ContentResolver; +import androidx.annotation.NonNull; import javax.inject.Provider; public class ContentResolverProvider implements Provider { Application application; - public ContentResolverProvider(Application application) { + public ContentResolverProvider(@NonNull Application application) { this.application = application; } + @NonNull @Override public ContentResolver get() { return application.getContentResolver(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/FragmentManagerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/FragmentManagerProvider.java index 9fee1e92..1c0cf7dc 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/FragmentManagerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/FragmentManagerProvider.java @@ -17,16 +17,18 @@ package toothpick.smoothie.provider; import android.app.Activity; +import androidx.annotation.NonNull; import javax.inject.Provider; @SuppressWarnings("deprecation") public class FragmentManagerProvider implements Provider { Activity activity; - public FragmentManagerProvider(Activity activity) { + public FragmentManagerProvider(@NonNull Activity activity) { this.activity = activity; } + @NonNull @Override public android.app.FragmentManager get() { return activity.getFragmentManager(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/HandlerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/HandlerProvider.java index 7b224873..1706ed56 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/HandlerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/HandlerProvider.java @@ -18,11 +18,13 @@ import android.os.Handler; import android.os.Looper; +import androidx.annotation.NonNull; import javax.inject.Provider; public class HandlerProvider implements Provider { public HandlerProvider() {} + @NonNull @Override public Handler get() { return new Handler(Looper.getMainLooper()); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/LayoutInflaterProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/LayoutInflaterProvider.java index 3f5a9299..96a388be 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/LayoutInflaterProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/LayoutInflaterProvider.java @@ -18,15 +18,17 @@ import android.app.Activity; import android.view.LayoutInflater; +import androidx.annotation.NonNull; import javax.inject.Provider; public class LayoutInflaterProvider implements Provider { Activity activity; - public LayoutInflaterProvider(Activity activity) { + public LayoutInflaterProvider(@NonNull Activity activity) { this.activity = activity; } + @NonNull @Override public LayoutInflater get() { return LayoutInflater.from(activity); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/LoaderManagerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/LoaderManagerProvider.java index 72a77d2b..53b55c69 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/LoaderManagerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/LoaderManagerProvider.java @@ -17,16 +17,18 @@ package toothpick.smoothie.provider; import android.app.Activity; +import androidx.annotation.NonNull; import javax.inject.Provider; @SuppressWarnings("deprecation") public class LoaderManagerProvider implements Provider { Activity activity; - public LoaderManagerProvider(Activity activity) { + public LoaderManagerProvider(@NonNull Activity activity) { this.activity = activity; } + @NonNull @Override public android.app.LoaderManager get() { return activity.getLoaderManager(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/PackageManagerProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/PackageManagerProvider.java index fde77e0b..fde206ac 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/PackageManagerProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/PackageManagerProvider.java @@ -18,15 +18,17 @@ import android.app.Application; import android.content.pm.PackageManager; +import androidx.annotation.NonNull; import javax.inject.Provider; public class PackageManagerProvider implements Provider { Application application; - public PackageManagerProvider(Application application) { + public PackageManagerProvider(@NonNull Application application) { this.application = application; } + @NonNull @Override public PackageManager get() { return application.getPackageManager(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/ResourcesProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/ResourcesProvider.java index f54ea3fd..6888044f 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/ResourcesProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/ResourcesProvider.java @@ -18,15 +18,17 @@ import android.app.Application; import android.content.res.Resources; +import androidx.annotation.NonNull; import javax.inject.Provider; public class ResourcesProvider implements Provider { Application application; - public ResourcesProvider(Application application) { + public ResourcesProvider(@NonNull Application application) { this.application = application; } + @NonNull @Override public Resources get() { return application.getResources(); diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/SharedPreferencesProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/SharedPreferencesProvider.java index 8617b0d3..aae2355f 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/SharedPreferencesProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/SharedPreferencesProvider.java @@ -20,21 +20,25 @@ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import javax.inject.Provider; public class SharedPreferencesProvider implements Provider { Application application; String preferencesName; - public SharedPreferencesProvider(Application application) { + public SharedPreferencesProvider(@NonNull Application application) { this(application, null); } - public SharedPreferencesProvider(Application application, String preferencesName) { + public SharedPreferencesProvider( + @NonNull Application application, @Nullable String preferencesName) { this.application = application; this.preferencesName = preferencesName; } + @NonNull @Override public SharedPreferences get() { if (preferencesName != null) { diff --git a/smoothie/src/main/java/toothpick/smoothie/provider/SystemServiceProvider.java b/smoothie/src/main/java/toothpick/smoothie/provider/SystemServiceProvider.java index 96c2937e..995a757c 100644 --- a/smoothie/src/main/java/toothpick/smoothie/provider/SystemServiceProvider.java +++ b/smoothie/src/main/java/toothpick/smoothie/provider/SystemServiceProvider.java @@ -17,18 +17,20 @@ package toothpick.smoothie.provider; import android.content.Context; +import androidx.annotation.NonNull; import javax.inject.Provider; public class SystemServiceProvider implements Provider { private Context context; private String serviceName; - public SystemServiceProvider(Context context, String serviceName) { + public SystemServiceProvider(@NonNull Context context, @NonNull String serviceName) { this.context = context; this.serviceName = serviceName; } @SuppressWarnings("unchecked") + @NonNull @Override public T get() { return (T) context.getSystemService(serviceName); diff --git a/toothpick-runtime/build.gradle b/toothpick-runtime/build.gradle index c893b13c..8152ef34 100644 --- a/toothpick-runtime/build.gradle +++ b/toothpick-runtime/build.gradle @@ -9,6 +9,7 @@ targetCompatibility = 1.7 dependencies { api project(':toothpick') api deps.inject + api deps.androidxannotations testImplementation deps.junit4 testImplementation deps.hamcrest diff --git a/toothpick-runtime/src/main/java/toothpick/InjectorImpl.java b/toothpick-runtime/src/main/java/toothpick/InjectorImpl.java index c047989e..998f7e3a 100644 --- a/toothpick-runtime/src/main/java/toothpick/InjectorImpl.java +++ b/toothpick-runtime/src/main/java/toothpick/InjectorImpl.java @@ -16,6 +16,7 @@ */ package toothpick; +import androidx.annotation.NonNull; import toothpick.locators.MemberInjectorLocator; /** Default implementation of an injector. */ @@ -28,7 +29,7 @@ public class InjectorImpl implements Injector { */ @Override @SuppressWarnings("unchecked") - public void inject(T obj, Scope scope) { + public void inject(@NonNull T obj, @NonNull Scope scope) { Class currentClass = (Class) obj.getClass(); do { MemberInjector memberInjector = diff --git a/toothpick-runtime/src/main/java/toothpick/Toothpick.java b/toothpick-runtime/src/main/java/toothpick/Toothpick.java index 37255c9e..1d6a4087 100644 --- a/toothpick-runtime/src/main/java/toothpick/Toothpick.java +++ b/toothpick-runtime/src/main/java/toothpick/Toothpick.java @@ -16,6 +16,7 @@ */ package toothpick; +import androidx.annotation.NonNull; import java.util.Collections; import java.util.concurrent.ConcurrentHashMap; import toothpick.Scope.ScopeConfig; @@ -58,7 +59,7 @@ protected Toothpick() { * * @return the root scope. */ - public static Scope openRootScope() { + public static @NonNull Scope openRootScope() { synchronized (ROOT_SCOPES) { if (ROOT_SCOPES.size() > 1) { throw new RuntimeException( @@ -84,7 +85,7 @@ public static Scope openRootScope() { * if the scope existed already. * @return the root scope. */ - public static Scope openRootScope(ScopeConfig scopeConfig) { + public static @NonNull Scope openRootScope(@NonNull ScopeConfig scopeConfig) { if (isRootScopeOpen()) { return openRootScope(); } @@ -108,7 +109,7 @@ public static boolean isRootScopeOpen() { * @param name the name of the scope. * @return true if the scope has been opened and not yet closed. */ - public static boolean isScopeOpen(Object name) { + public static boolean isScopeOpen(@NonNull Object name) { if (name == null) { throw new IllegalArgumentException("null scope names are not allowed."); } @@ -124,7 +125,7 @@ public static boolean isScopeOpen(Object name) { * @param names of the scopes to open hierarchically. * @return the last opened scope, leaf node of the created subtree of scopes. */ - public static Scope openScopes(Object... names) { + public static Scope openScopes(@NonNull Object... names) { if (names == null) { throw new IllegalArgumentException("null scope names are not allowed."); @@ -154,7 +155,7 @@ public static Scope openScopes(Object... names) { * @see #openScope(Object, ScopeConfig) * @see #closeScope(Object) */ - public static Scope openScope(Object name) { + public static @NonNull Scope openScope(@NonNull Object name) { return openScope(name, true); } @@ -170,7 +171,7 @@ public static Scope openScope(Object name) { * @see #openScope(Object) * @see #closeScope(Object) */ - public static Scope openScope(Object name, ScopeConfig scopeConfig) { + public static @NonNull Scope openScope(@NonNull Object name, ScopeConfig scopeConfig) { if (isScopeOpen(name)) { return openScope(name); } @@ -186,7 +187,7 @@ public static Scope openScope(Object name, ScopeConfig scopeConfig) { * @param name the name of the scope to open. * @param isRootScope whether or not this is a root scope */ - private static Scope openScope(Object name, boolean isRootScope) { + private static @NonNull Scope openScope(@NonNull Object name, boolean isRootScope) { synchronized (ROOT_SCOPES) { if (name == null) { throw new IllegalArgumentException("null scope names are not allowed."); @@ -215,7 +216,7 @@ private static Scope openScope(Object name, boolean isRootScope) { * * @param name the name of the scope to close. */ - public static void closeScope(Object name) { + public static void closeScope(@NonNull Object name) { synchronized (ROOT_SCOPES) { // we remove the scope first, so that other threads don't see it, and see the next snapshot of // the tree @@ -248,7 +249,7 @@ public static void reset() { * * @param scope the scope we want to reset. */ - public static void reset(Scope scope) { + public static void reset(@NonNull Scope scope) { ScopeNode scopeNode = (ScopeNode) scope; scopeNode.reset(); } @@ -259,7 +260,7 @@ public static void reset(Scope scope) { * * @param scope the scope we want to reset. */ - public static void release(Scope scope) { + public static void release(@NonNull Scope scope) { ScopeNode scopeNode = (ScopeNode) scope; scopeNode.release(); } @@ -271,7 +272,7 @@ public static void release(Scope scope) { * @param obj the object to be injected. * @param scope the scope in which all dependencies are obtained. */ - public static void inject(Object obj, Scope scope) { + public static void inject(@NonNull Object obj, @NonNull Scope scope) { injector.inject(obj, scope); } @@ -282,7 +283,7 @@ public static void inject(Object obj, Scope scope) { * We don't do anything else to the children nodes are they will be garbage collected soon. We * just cut a whole sub-graph in the references graph of the JVM normally. */ - private static void removeScopeAndChildrenFromMap(ScopeNode scope) { + private static void removeScopeAndChildrenFromMap(@NonNull ScopeNode scope) { MAP_KEY_TO_SCOPE.remove(scope.getName()); scope.close(); for (ScopeNode childScope : scope.childrenScopes.values()) { @@ -295,7 +296,7 @@ private static void removeScopeAndChildrenFromMap(ScopeNode scope) { * * @param configuration the configuration to use */ - public static void setConfiguration(Configuration configuration) { + public static void setConfiguration(@NonNull Configuration configuration) { ConfigurationHolder.configuration = configuration; } diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/Configuration.java b/toothpick-runtime/src/main/java/toothpick/configuration/Configuration.java index 4fe563b6..75f936bd 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/Configuration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/Configuration.java @@ -16,6 +16,7 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; import toothpick.config.Binding; @@ -44,6 +45,7 @@ public class Configuration * * @return a development configuration. */ + @NonNull public static Configuration forDevelopment() { final Configuration configuration = new Configuration(); configuration.runtimeCheckConfiguration = new RuntimeCheckOnConfiguration(); @@ -56,6 +58,7 @@ public static Configuration forDevelopment() { * * @return a production configuration. */ + @NonNull public static Configuration forProduction() { return new Configuration(); } @@ -65,6 +68,7 @@ public static Configuration forProduction() { * * @return a configuration that allows multiple root scopes. */ + @NonNull public Configuration allowMultipleRootScopes() { this.multipleRootScopeCheckConfiguration = new MultipleRootScopeCheckOffConfiguration(); return this; @@ -77,28 +81,29 @@ public Configuration allowMultipleRootScopes() { * * @return a configuration that allows a single root scope. */ + @NonNull public Configuration preventMultipleRootScopes() { this.multipleRootScopeCheckConfiguration = new MultipleRootScopeCheckOnConfiguration(); return this; } @Override - public void checkIllegalBinding(Binding binding, Scope scope) { + public void checkIllegalBinding(@NonNull Binding binding, @NonNull Scope scope) { runtimeCheckConfiguration.checkIllegalBinding(binding, scope); } @Override - public void checkCyclesStart(Class clazz, String name) { + public void checkCyclesStart(@NonNull Class clazz, @NonNull String name) { runtimeCheckConfiguration.checkCyclesStart(clazz, name); } @Override - public void checkCyclesEnd(Class clazz, String name) { + public void checkCyclesEnd(@NonNull Class clazz, @NonNull String name) { runtimeCheckConfiguration.checkCyclesEnd(clazz, name); } @Override - public void checkMultipleRootScopes(Scope scope) { + public void checkMultipleRootScopes(@NonNull Scope scope) { multipleRootScopeCheckConfiguration.checkMultipleRootScopes(scope); } diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckConfiguration.java index 9c95460b..13fe542d 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckConfiguration.java @@ -16,6 +16,7 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; import toothpick.config.Binding; @@ -27,7 +28,7 @@ interface RuntimeCheckConfiguration { * @param binding the binding being installed. * @param scope the scope where the binding is installed. */ - void checkIllegalBinding(Binding binding, Scope scope); + void checkIllegalBinding(@NonNull Binding binding, @NonNull Scope scope); /** * Called when the class {@code class} starts being injected using the qualifier {@code name}. @@ -37,7 +38,7 @@ interface RuntimeCheckConfiguration { * @param clazz the class to be injected. * @param name the name of the required injection. */ - void checkCyclesStart(Class clazz, String name); + void checkCyclesStart(@NonNull Class clazz, @NonNull String name); /** * Called when the class {@code class} ends being injected using the qualifier {@code name}. Will @@ -47,5 +48,5 @@ interface RuntimeCheckConfiguration { * @param clazz the class to be injected. * @param name the name of the required injection. */ - void checkCyclesEnd(Class clazz, String name); + void checkCyclesEnd(@NonNull Class clazz, @NonNull String name); } diff --git a/toothpick/build.gradle b/toothpick/build.gradle index 67188dee..a350317b 100644 --- a/toothpick/build.gradle +++ b/toothpick/build.gradle @@ -5,6 +5,7 @@ targetCompatibility = 1.7 dependencies { implementation deps.inject + implementation deps.androidxannotations testImplementation deps.junit4 } diff --git a/toothpick/src/main/java/toothpick/Injector.java b/toothpick/src/main/java/toothpick/Injector.java index f9669264..a99caef2 100644 --- a/toothpick/src/main/java/toothpick/Injector.java +++ b/toothpick/src/main/java/toothpick/Injector.java @@ -16,6 +16,8 @@ */ package toothpick; +import androidx.annotation.NonNull; + /** Allows to inject members of a given instance. An injector works with a scope. */ public interface Injector { /** @@ -36,5 +38,5 @@ public interface Injector { * @param scope the scope in which all dependencies are obtained. * @param the type of {@code clazz}. */ - void inject(T obj, Scope scope); + void inject(@NonNull T obj, @NonNull Scope scope); } diff --git a/toothpick/src/main/java/toothpick/MemberInjector.java b/toothpick/src/main/java/toothpick/MemberInjector.java index 6aa7098c..53b787f2 100644 --- a/toothpick/src/main/java/toothpick/MemberInjector.java +++ b/toothpick/src/main/java/toothpick/MemberInjector.java @@ -16,6 +16,8 @@ */ package toothpick; +import androidx.annotation.NonNull; + /** * Inject member of an instance of a class. All injected members are gonna be obtained in the scope * of the current scope. MemberInjector are discovered via the {@code MemberInjectorLocator}. @@ -32,5 +34,5 @@ public interface MemberInjector { * @param t the object in which to inject all dependencies. * @param scope the scope in which all dependencies of {@code t} will be looked for. */ - void inject(T t, Scope scope); + void inject(@NonNull T t, @NonNull Scope scope); } diff --git a/toothpick/src/main/java/toothpick/Scope.java b/toothpick/src/main/java/toothpick/Scope.java index 54042d3d..72e758d5 100644 --- a/toothpick/src/main/java/toothpick/Scope.java +++ b/toothpick/src/main/java/toothpick/Scope.java @@ -16,6 +16,8 @@ */ package toothpick; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.lang.annotation.Annotation; import javax.inject.Provider; import javax.inject.Singleton; @@ -68,9 +70,11 @@ public interface Scope { * @return the name of the scope. It is only used to access a node via the ToothPick class. The * name can't be null. */ + @NonNull Object getName(); /** @return the parentScope of this scope. Can be null for a root scope. */ + @Nullable Scope getParentScope(); /** @@ -84,12 +88,14 @@ public interface Scope { * Singleton}, the root scope is always returned. Thus the {@link Singleton} scope annotation * class doesn't need to be explicitely supported, it's built-in. */ - Scope getParentScope(Class scopeAnnotationClass); + @NonNull + Scope getParentScope(@NonNull Class scopeAnnotationClass); /** * @return the root scope of this scope. The root scope is the scope itself if the scope has no * parent. Otherwise, if it has parents, it is the highest parent in the hierarchy of parents. */ + @NonNull Scope getRootScope(); /** @@ -102,7 +108,8 @@ public interface Scope { * all root scopes (scopes with no parents). * @see #getParentScope(Class) */ - Scope supportScopeAnnotation(Class scopeAnnotationClass); + @NonNull + Scope supportScopeAnnotation(@NonNull Class scopeAnnotationClass); boolean isScopeAnnotationSupported(Class scopeAnnotationClass); @@ -112,7 +119,8 @@ public interface Scope { * @see #getInstance(Class, String) * @see toothpick.config.Module */ - T getInstance(Class clazz); + @NonNull + T getInstance(@NonNull Class clazz); /** * Returns the instance of {@code clazz} named {@code name} if one is scoped in the current scope, @@ -127,7 +135,8 @@ public interface Scope { * @return a scoped instance or a new one produced by the factory associated to {@code clazz}. * @see toothpick.config.Binding */ - T getInstance(Class clazz, String name); + @NonNull + T getInstance(@NonNull Class clazz, @Nullable String name); /** * Requests a provider via an unnamed binding. @@ -135,7 +144,8 @@ public interface Scope { * @see #getProvider(Class, String) * @see toothpick.config.Module */ - Provider getProvider(Class clazz); + @NonNull + Provider getProvider(@NonNull Class clazz); /** * Returns a named {@code Provider} named {@code name} of {@code clazz} if one is scoped in the @@ -151,7 +161,8 @@ public interface Scope { * providers are thread safe. * @see toothpick.config.Module */ - Provider getProvider(Class clazz, String name); + @NonNull + Provider getProvider(@NonNull Class clazz, @Nullable String name); /** * Requests a Lazy via an unnamed binding. @@ -159,7 +170,8 @@ public interface Scope { * @see #getLazy(Class, String) * @see toothpick.config.Module */ - Lazy getLazy(Class clazz); + @NonNull + Lazy getLazy(@NonNull Class clazz); /** * Returns a {@code Lazy} named {@code name} of {@code clazz} if one provider is scoped in the @@ -177,7 +189,8 @@ public interface Scope { * lazies are thread safe. * @see toothpick.config.Module */ - Lazy getLazy(Class clazz, String name); + @NonNull + Lazy getLazy(@NonNull Class clazz, @Nullable String name); /** * Allows to install modules. @@ -185,7 +198,8 @@ public interface Scope { * @param modules an array of modules that define bindings. * @see #installTestModules */ - Scope installModules(Module... modules); + @NonNull + Scope installModules(@NonNull Module... modules); /** * DO NOT USE IT IN PRODUCTION.
@@ -197,7 +211,8 @@ public interface Scope { * * @param modules an array of modules that define test bindings. */ - Scope installTestModules(Module... modules); + @NonNull + Scope installTestModules(@NonNull Module... modules); /** * Injects all dependencies (transitively) in {@code obj}, dependencies will be obtained in the @@ -205,7 +220,7 @@ public interface Scope { * * @param obj the object to be injected. */ - void inject(Object obj); + void inject(@NonNull Object obj); /** * Release all releasable singletons. Factories and internal providers won't be released. @@ -220,7 +235,8 @@ public interface Scope { * @param subScopeName the name of the scope. * @see #openSubScope(Object) */ - Scope openSubScope(Object subScopeName); + @NonNull + Scope openSubScope(@NonNull Object subScopeName); /** * Opens a sub scope of this scope. If a child scope by this {@code name} already exists, it is @@ -232,10 +248,11 @@ public interface Scope { * if the scope existed already. * @see #openSubScope(Object) */ - Scope openSubScope(Object subScopeName, ScopeConfig scopeConfig); + @NonNull + Scope openSubScope(@NonNull Object subScopeName, @NonNull ScopeConfig scopeConfig); @FunctionalInterface interface ScopeConfig { - void configure(Scope scope); + void configure(@NonNull Scope scope); } } diff --git a/toothpick/src/main/java/toothpick/config/Binding.java b/toothpick/src/main/java/toothpick/config/Binding.java index 46f14d30..81560df0 100644 --- a/toothpick/src/main/java/toothpick/config/Binding.java +++ b/toothpick/src/main/java/toothpick/config/Binding.java @@ -16,6 +16,7 @@ */ package toothpick.config; +import androidx.annotation.NonNull; import java.lang.annotation.Annotation; import javax.inject.Provider; import javax.inject.Qualifier; @@ -33,40 +34,48 @@ public class Binding { private boolean isProvidingSingleton; private boolean isProvidingReleasable; - public Binding(Class key) { + public Binding(@NonNull Class key) { this.key = key; mode = Mode.SIMPLE; } + @NonNull public CanBeReleasable singleton() { isCreatingSingleton = true; return new CanBeReleasable(); } + @NonNull public Mode getMode() { return mode; } + @NonNull public Class getKey() { return key; } + @NonNull public Class getImplementationClass() { return implementationClass; } + @NonNull public T getInstance() { return instance; } + @NonNull public Provider getProviderInstance() { return providerInstance; } + @NonNull public Class> getProviderClass() { return providerClass; } + @NonNull public String getName() { return name; } @@ -100,13 +109,15 @@ public enum Mode { // ************************* public class CanBeNamed extends CanBeBound { - public CanBeBound withName(String name) { + @NonNull + public CanBeBound withName(@NonNull String name) { Binding.this.name = name; return new CanBeBound(); } + @NonNull public
CanBeBound withName( - Class annotationClassWithQualifierAnnotation) { + @NonNull Class annotationClassWithQualifierAnnotation) { if (!annotationClassWithQualifierAnnotation.isAnnotationPresent(Qualifier.class)) { throw new IllegalArgumentException( String.format( @@ -124,17 +135,19 @@ public CanBeReleasable singleton() { return new CanBeReleasable(); } - public void toInstance(T instance) { + public void toInstance(@NonNull T instance) { Binding.this.instance = instance; mode = Mode.INSTANCE; } - public CanBeSingleton to(Class implClass) { + @NonNull + public CanBeSingleton to(@NonNull Class implClass) { Binding.this.implementationClass = implClass; mode = Mode.CLASS; return new CanBeSingleton(); } + @NonNull public CanProvideSingletonOrSingleton toProvider( Class> providerClass) { Binding.this.providerClass = providerClass; @@ -142,7 +155,8 @@ public CanProvideSingletonOrSingleton toProvider( return new CanProvideSingletonOrSingleton(); } - public CanProvideSingleton toProviderInstance(Provider providerInstance) { + @NonNull + public CanProvideSingleton toProviderInstance(@NonNull Provider providerInstance) { Binding.this.providerInstance = providerInstance; mode = Mode.PROVIDER_INSTANCE; return new CanProvideSingleton(); @@ -151,6 +165,7 @@ public CanProvideSingleton toProviderInstance(Provider providerInst public class CanBeSingleton { /** to provide a singleton using the binding's scope and reuse it inside the binding's scope */ + @NonNull public CanBeReleasable singleton() { Binding.this.isCreatingSingleton = true; return new CanBeReleasable(); @@ -165,6 +180,7 @@ public void releasable() { } public class CanProvideSingleton { + @NonNull public CanProvideReleasable providesSingleton() { isProvidingSingleton = true; return new CanProvideReleasable(); @@ -178,6 +194,7 @@ public void providesReleasable() { } public class CanProvideSingletonOrSingleton extends CanBeSingleton { + @NonNull public CanProvideReleasableAndThenOnlySingleton providesSingleton() { isProvidingSingleton = true; return new CanProvideReleasableAndThenOnlySingleton(); @@ -185,6 +202,7 @@ public CanProvideReleasableAndThenOnlySingleton providesSingleton() { } public class CanProvideReleasableAndThenOnlySingleton { + @NonNull public CanBeOnlySingleton providesReleasable() { Binding.this.isProvidingReleasable = true; return new CanBeOnlySingleton(); diff --git a/toothpick/src/main/java/toothpick/config/Module.java b/toothpick/src/main/java/toothpick/config/Module.java index 03bbe6cd..96c75535 100644 --- a/toothpick/src/main/java/toothpick/config/Module.java +++ b/toothpick/src/main/java/toothpick/config/Module.java @@ -16,6 +16,7 @@ */ package toothpick.config; +import androidx.annotation.NonNull; import java.util.HashSet; import java.util.Set; import toothpick.ProvidesSingleton; @@ -99,12 +100,14 @@ public class Module { private Set bindingSet = new HashSet<>(); - public Binding.CanBeNamed bind(Class key) { + @NonNull + public Binding.CanBeNamed bind(@NonNull Class key) { Binding binding = new Binding<>(key); bindingSet.add(binding); return binding.new CanBeNamed(); } + @NonNull public Set getBindingSet() { return bindingSet; } From 5b9c7a974d61283e0b19923488feb0de5cbaa776 Mon Sep 17 00:00:00 2001 From: Anthony Faucogney Date: Tue, 1 Dec 2020 09:15:14 +0100 Subject: [PATCH 2/2] missing commits --- .../src/main/java/toothpick/ScopeImpl.java | 33 ++++++++++++------- .../src/main/java/toothpick/ScopeNode.java | 9 +++-- .../MultipleRootScopeCheckConfiguration.java | 3 +- ...ultipleRootScopeCheckOffConfiguration.java | 3 +- ...MultipleRootScopeCheckOnConfiguration.java | 3 +- .../RuntimeCheckOffConfiguration.java | 7 ++-- .../RuntimeCheckOnConfiguration.java | 7 ++-- .../test/java/toothpick/ToothpickTest.java | 3 +- 8 files changed, 45 insertions(+), 23 deletions(-) diff --git a/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java b/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java index 15fa20b2..0e4b65d1 100644 --- a/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java +++ b/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java @@ -18,6 +18,7 @@ import static java.lang.String.format; +import androidx.annotation.NonNull; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -79,13 +80,15 @@ public ScopeImpl(Object name) { installBindingForScopeClass(); } + @NonNull @Override - public T getInstance(Class clazz) { + public T getInstance(@NonNull Class clazz) { return getInstance(clazz, null); } + @NonNull @Override - public T getInstance(Class clazz, String name) { + public T getInstance(@NonNull Class clazz, String name) { crashIfClosed(); ConfigurationHolder.configuration.checkCyclesStart(clazz, name); T t; @@ -97,30 +100,35 @@ public T getInstance(Class clazz, String name) { return t; } + @NonNull @Override - public Provider getProvider(Class clazz) { + public Provider getProvider(@NonNull Class clazz) { return getProvider(clazz, null); } + @NonNull @Override - public Provider getProvider(Class clazz, String name) { + public Provider getProvider(@NonNull Class clazz, String name) { crashIfClosed(); return new ThreadSafeProviderImpl<>(this, clazz, name, false); } + @NonNull @Override - public Lazy getLazy(Class clazz) { + public Lazy getLazy(@NonNull Class clazz) { return getLazy(clazz, null); } + @NonNull @Override - public Lazy getLazy(Class clazz, String name) { + public Lazy getLazy(@NonNull Class clazz, String name) { crashIfClosed(); return new ThreadSafeProviderImpl<>(this, clazz, name, true); } + @NonNull @Override - public synchronized Scope installTestModules(Module... modules) { + public synchronized Scope installTestModules(@NonNull Module... modules) { if (hasTestModules) { throw new IllegalStateException("TestModules can only be installed once per scope."); } @@ -129,14 +137,15 @@ public synchronized Scope installTestModules(Module... modules) { return this; } + @NonNull @Override - public Scope installModules(Module... modules) { + public Scope installModules(@NonNull Module... modules) { installModules(false, modules); return this; } @Override - public void inject(Object obj) { + public void inject(@NonNull Object obj) { Toothpick.inject(obj, this); } @@ -606,14 +615,16 @@ protected void reset() { installBindingForScopeClass(); } + @NonNull @Override - public Scope openSubScope(Object subScopeName) { + public Scope openSubScope(@NonNull Object subScopeName) { // we already check later that sub scope is a child of this return Toothpick.openScopes(getName(), subScopeName); } + @NonNull @Override - public Scope openSubScope(Object subScopeName, ScopeConfig scopeConfig) { + public Scope openSubScope(@NonNull Object subScopeName, @NonNull ScopeConfig scopeConfig) { // we already check later that sub scope is a child of this boolean wasOpen = Toothpick.isScopeOpen(subScopeName); Scope scope = Toothpick.openScopes(getName(), subScopeName); diff --git a/toothpick-runtime/src/main/java/toothpick/ScopeNode.java b/toothpick-runtime/src/main/java/toothpick/ScopeNode.java index bb7c279c..6d0f4b1c 100644 --- a/toothpick-runtime/src/main/java/toothpick/ScopeNode.java +++ b/toothpick-runtime/src/main/java/toothpick/ScopeNode.java @@ -18,6 +18,7 @@ import static java.lang.String.format; +import androidx.annotation.NonNull; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; @@ -96,6 +97,7 @@ public ScopeNode(Object name) { bindScopeAnnotationIfNameIsScopeAnnotation(); } + @NonNull @Override public Object getName() { return name; @@ -135,9 +137,10 @@ public ScopeNode getParentScope() { * Singleton}, the root scope is always returned. Thus the {@link Singleton} scope annotation * class doesn't need to be supported, it's built-in. */ + @NonNull @SuppressWarnings({"unused", "used by generated code"}) @Override - public ScopeNode getParentScope(Class scopeAnnotationClass) { + public ScopeNode getParentScope(@NonNull Class scopeAnnotationClass) { checkIsAnnotationScope(scopeAnnotationClass); if (scopeAnnotationClass == Singleton.class) { @@ -162,6 +165,7 @@ public ScopeNode getParentScope(Class scopeAnnotationC * @return the root scope of this scope. The root scope is the scope itself if the scope has no * parent. Otherwise, if it has parents, it is the highest parent in the hierarchy of parents. */ + @NonNull @SuppressWarnings({"unused", "used by generated code"}) @Override public ScopeNode getRootScope() { @@ -181,8 +185,9 @@ public ScopeNode getRootScope() { * all root scopes (scopes without parent). * @see #getParentScope(Class) */ + @NonNull @Override - public Scope supportScopeAnnotation(Class scopeAnnotationClass) { + public Scope supportScopeAnnotation(@NonNull Class scopeAnnotationClass) { checkIsAnnotationScope(scopeAnnotationClass); if (scopeAnnotationClass == Singleton.class) { throw new IllegalArgumentException( diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckConfiguration.java index 908e7bca..66a45ff4 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckConfiguration.java @@ -16,6 +16,7 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; /** Check strategy to detect when mutiple roots are created in TP scope forest. */ @@ -25,7 +26,7 @@ interface MultipleRootScopeCheckConfiguration { * * @param scope a newly created scope. */ - void checkMultipleRootScopes(Scope scope); + void checkMultipleRootScopes(@NonNull Scope scope); /** Reset the state of the detector. */ void onScopeForestReset(); diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOffConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOffConfiguration.java index 22d716a9..a0bd68dd 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOffConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOffConfiguration.java @@ -16,12 +16,13 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; /** Dummy implementation of the {@link MultipleRootScopeCheckConfiguration} strategy. */ class MultipleRootScopeCheckOffConfiguration implements MultipleRootScopeCheckConfiguration { @Override - public void checkMultipleRootScopes(Scope scope) {} + public void checkMultipleRootScopes(@NonNull Scope scope) {} @Override public void onScopeForestReset() {} diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOnConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOnConfiguration.java index e2e124c9..9d3150bc 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOnConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/MultipleRootScopeCheckOnConfiguration.java @@ -16,6 +16,7 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; /** @@ -26,7 +27,7 @@ class MultipleRootScopeCheckOnConfiguration implements MultipleRootScopeCheckCon private Scope rootScope; @Override - public synchronized void checkMultipleRootScopes(Scope newRootScope) { + public synchronized void checkMultipleRootScopes(@NonNull Scope newRootScope) { if (rootScope == null && newRootScope != null) { rootScope = newRootScope; return; diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOffConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOffConfiguration.java index 97f26e4d..d0955a1f 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOffConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOffConfiguration.java @@ -16,16 +16,17 @@ */ package toothpick.configuration; +import androidx.annotation.NonNull; import toothpick.Scope; import toothpick.config.Binding; class RuntimeCheckOffConfiguration implements RuntimeCheckConfiguration { @Override - public void checkIllegalBinding(Binding binding, Scope scope) {} + public void checkIllegalBinding(@NonNull Binding binding, @NonNull Scope scope) {} @Override - public void checkCyclesStart(Class clazz, String name) {} + public void checkCyclesStart(@NonNull Class clazz, @NonNull String name) {} @Override - public void checkCyclesEnd(Class clazz, String name) {} + public void checkCyclesEnd(@NonNull Class clazz, @NonNull String name) {} } diff --git a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOnConfiguration.java b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOnConfiguration.java index 8e60c298..da61c66d 100644 --- a/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOnConfiguration.java +++ b/toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOnConfiguration.java @@ -18,6 +18,7 @@ import static java.lang.String.format; +import androidx.annotation.NonNull; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; @@ -45,7 +46,7 @@ protected LinkedHashSet initialValue() { * @param scope the scope where the binding is installed. */ @Override - public void checkIllegalBinding(Binding binding, Scope scope) { + public void checkIllegalBinding(@NonNull Binding binding, @NonNull Scope scope) { Class clazz; switch (binding.getMode()) { case SIMPLE: @@ -76,7 +77,7 @@ public void checkIllegalBinding(Binding binding, Scope scope) { } @Override - public void checkCyclesStart(Class clazz, String name) { + public void checkCyclesStart(@NonNull Class clazz, @NonNull String name) { final Pair pair = new Pair(clazz, name); final LinkedHashSet linkedHashSet = cycleDetectionStack.get(); if (linkedHashSet.contains(pair)) { @@ -87,7 +88,7 @@ public void checkCyclesStart(Class clazz, String name) { } @Override - public void checkCyclesEnd(Class clazz, String name) { + public void checkCyclesEnd(@NonNull Class clazz, @NonNull String name) { cycleDetectionStack.get().remove(new Pair(clazz, name)); } diff --git a/toothpick-runtime/src/test/java/toothpick/ToothpickTest.java b/toothpick-runtime/src/test/java/toothpick/ToothpickTest.java index 0724ec6a..1fec5935 100644 --- a/toothpick-runtime/src/test/java/toothpick/ToothpickTest.java +++ b/toothpick-runtime/src/test/java/toothpick/ToothpickTest.java @@ -27,6 +27,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import androidx.annotation.NonNull; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.junit.After; @@ -464,7 +465,7 @@ private static class TestScopeConfig implements Scope.ScopeConfig { private boolean wasApplied = false; @Override - public void configure(Scope scope) { + public void configure(@NonNull Scope scope) { wasApplied = true; }