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

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

Issue 2655463008: Add new pattern for creating downstream objects (Closed)
Patch Set: Rebase Created 3 years, 11 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 508246a8f3877b6c18395cdff21c855a76719212..d71e5e6270443872456b21fd5fbb39246b636fee 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,30 @@ public class ChromeApplication extends ContentApplication {
public AccountManagerDelegate createAccountManagerDelegate() {
return new SystemAccountManagerDelegate(this);
}
+
+ /**
+ * 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 (required!)
+ * e.g., public MyType() {},
+ * - generate the downstream object in createObjectImpl in the ChromeApplication subclass.
+ * e.g., if (klass.getName().equals(MyType.class.getName()) return (T)new MySubType(), and
+ * - invoke this method on the appropriate class,
+ * e.g., ChromeApplication.createObject(MyType.class).
+ * @param klass The class that the Chrome Application should create an instance of.
+ */
+ public static <T> T createObject(Class<T> klass) {
+ return ((ChromeApplication) ContextUtils.getApplicationContext()).createObjectImpl(klass);
+ }
+
+ protected <T> T createObjectImpl(Class<T> klass) {
+ try {
+ return klass.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);
+ }
+ }
}
« 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