Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2160)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java

Issue 2668183002: Add new pattern for creating downstream objects (Closed)
Patch Set: ? extends Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
+ }
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698