Skip to content

Commit

Permalink
Avoid calling Class.getCanonicalName(), which is slow on Android, dur…
Browse files Browse the repository at this point in the history
…ing normal execution.

Make Preconditions.checkNotNull call Class.getCanonicalName() for Class arguments. Otherwise, ensure it's called only when debugging or in error cases.

Fixes #819.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=172789896
  • Loading branch information
netdpb authored and ronshapiro committed Oct 24, 2017
1 parent 2c6d3da commit c19f0c2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
33 changes: 14 additions & 19 deletions java/dagger/android/AndroidInjection.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.android;

import static android.util.Log.DEBUG;
import static dagger.internal.Preconditions.checkNotNull;

import android.app.Activity;
Expand Down Expand Up @@ -53,10 +54,7 @@ public static void inject(Activity activity) {

AndroidInjector<Activity> activityInjector =
((HasActivityInjector) application).activityInjector();
checkNotNull(
activityInjector,
"%s.activityInjector() returned null",
application.getClass().getCanonicalName());
checkNotNull(activityInjector, "%s.activityInjector() returned null", application.getClass());

activityInjector.inject(activity);
}
Expand Down Expand Up @@ -85,18 +83,18 @@ public static void inject(Activity activity) {
public static void inject(Fragment fragment) {
checkNotNull(fragment, "fragment");
HasFragmentInjector hasFragmentInjector = findHasFragmentInjector(fragment);
Log.d(
TAG,
String.format(
"An injector for %s was found in %s",
fragment.getClass().getCanonicalName(),
hasFragmentInjector.getClass().getCanonicalName()));
if (Log.isLoggable(TAG, DEBUG)) {
Log.d(
TAG,
String.format(
"An injector for %s was found in %s",
fragment.getClass().getCanonicalName(),
hasFragmentInjector.getClass().getCanonicalName()));
}

AndroidInjector<Fragment> fragmentInjector = hasFragmentInjector.fragmentInjector();
checkNotNull(
fragmentInjector,
"%s.fragmentInjector() returned null",
hasFragmentInjector.getClass().getCanonicalName());
fragmentInjector, "%s.fragmentInjector() returned null", hasFragmentInjector.getClass());

fragmentInjector.inject(fragment);
}
Expand Down Expand Up @@ -138,10 +136,7 @@ public static void inject(Service service) {
}

AndroidInjector<Service> serviceInjector = ((HasServiceInjector) application).serviceInjector();
checkNotNull(
serviceInjector,
"%s.serviceInjector() returned null",
application.getClass().getCanonicalName());
checkNotNull(serviceInjector, "%s.serviceInjector() returned null", application.getClass());

serviceInjector.inject(service);
}
Expand Down Expand Up @@ -170,7 +165,7 @@ public static void inject(BroadcastReceiver broadcastReceiver, Context context)
checkNotNull(
broadcastReceiverInjector,
"%s.broadcastReceiverInjector() returned null",
application.getClass().getCanonicalName());
application.getClass());

broadcastReceiverInjector.inject(broadcastReceiver);
}
Expand Down Expand Up @@ -198,7 +193,7 @@ public static void inject(ContentProvider contentProvider) {
checkNotNull(
contentProviderInjector,
"%s.contentProviderInjector() returned null",
application.getClass().getCanonicalName());
application.getClass());

contentProviderInjector.inject(contentProvider);
}
Expand Down
4 changes: 1 addition & 3 deletions java/dagger/android/DispatchingAndroidInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public boolean maybeInject(T instance) {
try {
AndroidInjector<T> injector =
checkNotNull(
factory.create(instance),
"%s.create(I) should not return null.",
factory.getClass().getCanonicalName());
factory.create(instance), "%s.create(I) should not return null.", factory.getClass());

injector.inject(instance);
return true;
Expand Down
17 changes: 10 additions & 7 deletions java/dagger/android/support/AndroidSupportInjection.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.android.support;

import static android.util.Log.DEBUG;
import static dagger.internal.Preconditions.checkNotNull;

import android.app.Activity;
Expand Down Expand Up @@ -54,19 +55,21 @@ public final class AndroidSupportInjection {
public static void inject(Fragment fragment) {
checkNotNull(fragment, "fragment");
HasSupportFragmentInjector hasSupportFragmentInjector = findHasFragmentInjector(fragment);
Log.d(
TAG,
String.format(
"An injector for %s was found in %s",
fragment.getClass().getCanonicalName(),
hasSupportFragmentInjector.getClass().getCanonicalName()));
if (Log.isLoggable(TAG, DEBUG)) {
Log.d(
TAG,
String.format(
"An injector for %s was found in %s",
fragment.getClass().getCanonicalName(),
hasSupportFragmentInjector.getClass().getCanonicalName()));
}

AndroidInjector<Fragment> fragmentInjector =
hasSupportFragmentInjector.supportFragmentInjector();
checkNotNull(
fragmentInjector,
"%s.supportFragmentInjector() returned null",
hasSupportFragmentInjector.getClass().getCanonicalName());
hasSupportFragmentInjector.getClass());

fragmentInjector.inject(fragment);
}
Expand Down
12 changes: 8 additions & 4 deletions java/dagger/internal/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static <T> T checkNotNull(T reference, String errorMessage) {
* message is formed by replacing the single {@code %s} placeholder in the template with
* {@code errorMessageArg}.
* @param errorMessageArg the argument to be substituted into the message template. Converted to a
* string using {@link String#valueOf(Object)}.
* string using {@link String#valueOf(Object)}, except for {@link Class} objects, which use
* {@link Class#getCanonicalName()}.
* @return the non-null reference that was validated
* @throws NullPointerException if {@code reference} is null
* @throws IllegalArgumentException if {@code errorMessageTemplate} doesn't contain exactly one
Expand All @@ -67,16 +68,19 @@ public static <T> T checkNotNull(T reference, String errorMessage) {
public static <T> T checkNotNull(
T reference, String errorMessageTemplate, Object errorMessageArg) {
if (reference == null) {
// Poor-persons version of String.format, which is not GWT-compatible
// Simple implementation of String.format, which is not GWT-compatible
if (!errorMessageTemplate.contains("%s")) {
throw new IllegalArgumentException("errorMessageTemplate has no format specifiers");
}
if (errorMessageTemplate.indexOf("%s") != errorMessageTemplate.lastIndexOf("%s")) {
throw new IllegalArgumentException(
"errorMessageTemplate has more than one format specifier");
}
throw new NullPointerException(
errorMessageTemplate.replaceFirst("%s", String.valueOf(errorMessageArg)));
String argString =
errorMessageArg instanceof Class
? ((Class) errorMessageArg).getCanonicalName()
: String.valueOf(errorMessageArg);
throw new NullPointerException(errorMessageTemplate.replace("%s", argString));
}
return reference;
}
Expand Down

0 comments on commit c19f0c2

Please sign in to comment.