| Index: chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
|
| index 17120e871caffbdcfa4cbda7b11184ede7e02aff..a93694822d631ff5aee07a77345b0f57019a0e4e 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
|
| @@ -428,4 +428,44 @@ public class ChromeApplication extends ContentApplication {
|
| public AccountManagerDelegate createAccountManagerDelegate() {
|
| return new SystemAccountManagerDelegate(this);
|
| }
|
| +
|
| + /**
|
| + * Selects an implementation of a class appropriate to this {@link ChromeApplication}.
|
| + * This should be overridden downstream.
|
| + * @param klass The class that the {@link ChromeApplication} may be asked to create an
|
| + * implementaiton of.
|
| + */
|
| + protected <T> Class<? extends T> getImplementation(Class<T> klass) {
|
| + return klass;
|
| + }
|
| +
|
| + /**
|
| + * Instantiates an object of a given type.
|
| + * This method exists as a utility to generate objects of types that have different
|
| + * implementations upstream and downstream. To use this,
|
| + * - give the upstream class a public parameterless constructor,
|
| + * - give the downstream class a public parameterless constructor,
|
| + * - register the downstream implementation in the downstream getImplementation(), and
|
| + * - invoke this method on the appropriate class.
|
| + * @param klass The class for which the {@link ChromeApplication} should create an instance.
|
| + */
|
| + @SuppressWarnings("unchecked")
|
| + public static <T> T createObject(Class<T> klass) {
|
| + Class newKlass =
|
| + ((ChromeApplication) ContextUtils.getApplicationContext()).getImplementation(klass);
|
| +
|
| + Object obj;
|
| + try {
|
| + obj = newKlass.newInstance();
|
| + } catch (InstantiationException e) {
|
| + throw new RuntimeException("Asked to create unexpected class: " + klass.getName(), e);
|
| + } catch (IllegalAccessException e) {
|
| + throw new RuntimeException("Asked to create unexpected class: " + klass.getName(), e);
|
| + }
|
| + if (!klass.isInstance(obj)) {
|
| + throw new RuntimeException("Created an instance of type " + obj.getClass().getName()
|
| + + " when expected an instance of type " + klass.getName());
|
| + }
|
| + return (T) obj;
|
| + }
|
| }
|
|
|