1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.internal;
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.InvocationTargetException;
9 import java.lang.reflect.Method;
11 import java.util.HashMap;
13 import android.content.Context;
14 import android.content.pm.PackageManager;
17 * This class is used to encapsulate the reflection invoking for bridge and wrapper.
19 * org.xwalk.core.internal.ReflectionHelper is for bridge to use. A copy will be
20 * made to org.xwalk.core.ReflectionHelper at build time for wrapper to use.
22 public class ReflectionHelper {
24 * This class contains the identical information of a constructor.
26 * Constructor of bridge and wrapper class will be initialized in the
28 * It might happen before the RefletionHelper itself being initialized.
29 * For those cases, record the information of the Constructor and load
30 * them after ReflectionHelper initialized.
32 static class ConstructorHelper {
33 private String fullClassName;
34 private Object[] paramTypes;
36 Constructor<?> loadConstructor() {
37 Class<?> clazz = loadClass(fullClassName);
38 if (clazz == null) return null;
39 Class<?>[] params = new Class<?>[paramTypes.length];
40 for (int i = 0; i < paramTypes.length; i++) {
41 Object type = paramTypes[i];
42 // paramTypes can be string or Class<?>, if it's string,
43 // it means it's not in bridge/wrapper's classLoader,
44 // we need to load it from wrapper/bridge's loader.
45 if (type instanceof Class<?>) {
46 params[i] = (Class<?>) type;
47 } else if (type instanceof String) {
48 params[i] = loadClass((String) type);
52 return clazz.getConstructor(params);
53 } catch (NoSuchMethodException e) {
54 ReflectionHelper.handleException(e);
59 ConstructorHelper(String className, Object... paramTypes) {
60 this.fullClassName = className;
61 this.paramTypes = paramTypes;
65 private static Map<Class<?>, Method> sBridgeWrapperMap = new HashMap<Class<?>, Method>();
66 private static Map<String, Constructor<?>> sConstructorMap = new HashMap<String, Constructor<?>>();
67 private static Map<String, ConstructorHelper> sConstructorHelperMap =
68 new HashMap<String, ConstructorHelper>();
69 private static ClassLoader sBridgeOrWrapperLoader = null;
70 private static Context sBridgeContext = null;
71 private static boolean sIsWrapper;
72 private final static String INTERNAL_PACKAGE = "org.xwalk.core.internal";
73 private final static String LIBRARY_APK_PACKAGE = "org.xwalk.core";
75 private static boolean sAllowCrossPackage = false;
76 private static SharedXWalkExceptionHandler sExceptionHandler = null;
78 static void setExceptionHandler(SharedXWalkExceptionHandler handler) {
79 sExceptionHandler = handler;
82 public static boolean shouldUseLibrary() {
83 // TODO(wang16): There are many other conditions here.
84 // e.g. Whether application uses the ApplicationClass we provided,
85 // Whether native library arch is correct.
88 ReflectionHelper.class.getClassLoader().loadClass(
89 INTERNAL_PACKAGE + "." + "ReflectionHelper");
91 } catch (ClassNotFoundException e) {
97 public static Context getBridgeContext() {
98 return sBridgeContext;
102 public static void allowCrossPackage() {
103 sAllowCrossPackage = true;
107 public static void init() {
110 if (shouldUseLibrary()) {
111 if (!sAllowCrossPackage) {
112 handleException("Use SharedXWalkView if you want to support shared mode");
114 XWalkApplication app = XWalkApplication.getApplication();
116 // TODO(wang16): Handle this well.
117 handleException("Shared mode requires XWalkApplication");
121 sBridgeContext = app.createPackageContext(
123 Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
124 } catch (PackageManager.NameNotFoundException e) {
127 if (sBridgeContext != null) {
128 app.addResource(sBridgeContext.getResources());
129 initClassLoader(sBridgeContext.getClassLoader(), sBridgeContext);
132 initClassLoader(ReflectionHelper.class.getClassLoader(), null);
137 public static void initClassLoader(ClassLoader loader, Context bridgeContext) {
138 sBridgeOrWrapperLoader = loader;
139 sBridgeContext = bridgeContext;
140 sBridgeWrapperMap.clear();
141 sConstructorMap.clear();
143 for (String name : sConstructorHelperMap.keySet()) {
144 ConstructorHelper helper = sConstructorHelperMap.get(name);
145 if (helper != null) sConstructorMap.put(name, helper.loadConstructor());
148 // Load the helper in bridge side and invoke the initClassLoader method of it
149 // with wrapper's classloader via reflection.
150 Class<?> helperInBridge =
151 sBridgeOrWrapperLoader.loadClass(INTERNAL_PACKAGE + "." + "ReflectionHelper");
152 Method initInBridge = helperInBridge.getMethod(
153 "initClassLoader", ClassLoader.class, Context.class);
154 initInBridge.invoke(null, ReflectionHelper.class.getClassLoader(), sBridgeContext);
156 // JavascriptInterface is an annotation class bridge will use but declared in
158 Class<?> javascriptInterface =
159 sBridgeOrWrapperLoader.loadClass("org.xwalk.core.JavascriptInterface");
160 Class<?> xwalkContentInInternal =
161 ReflectionHelper.class.getClassLoader().loadClass(
162 INTERNAL_PACKAGE + "." + "XWalkContent");
163 Method setJavascriptInterface = xwalkContentInInternal.getDeclaredMethod(
164 "setJavascriptInterfaceClass", javascriptInterface.getClass());
165 setJavascriptInterface.invoke(null, javascriptInterface);
167 } catch (Exception e) {
172 public static void registerConstructor(String name, String clazz, Object... params) {
173 sConstructorHelperMap.put(name, new ConstructorHelper(clazz, params));
176 public static Class<?> loadClass(String clazz) {
177 // Any embedder using Embedding API should only use the exposed APIs which are
178 // in wrapper, so the initialization process is always starting from wrapper.
179 if (sBridgeOrWrapperLoader == null) init();
180 if (sBridgeOrWrapperLoader == null) return null;
182 return sBridgeOrWrapperLoader.loadClass(clazz);
183 } catch (ClassNotFoundException e) {
189 public static Method loadMethod(Class<?> clazz, String name, Object... paramTypes) {
190 if (sBridgeOrWrapperLoader == null) return null;
191 Class<?>[] params = new Class<?>[paramTypes.length];
192 for (int i = 0; i < paramTypes.length; i++) {
193 Object type = paramTypes[i];
194 if (type instanceof Class<?>) {
195 params[i] = (Class<?>) type;
196 } else if (type instanceof String) {
197 params[i] = loadClass((String) type);
201 return clazz.getMethod(name, params);
202 } catch (NoSuchMethodException e) {
208 public static void handleException(Exception e) {
211 if (isWrapper() && sExceptionHandler != null) {
212 if (sExceptionHandler.handleException(e)) return;
215 throw new RuntimeException(e);
218 public static void handleException(String e) {
219 handleException(new RuntimeException(e));
222 public static Object createInstance(String name, Object... parameters) {
224 Constructor<?> creator = sConstructorMap.get(name);
225 if (creator == null) {
226 ConstructorHelper helper = sConstructorHelperMap.get(name);
227 if (helper != null) {
228 creator = helper.loadConstructor();
229 sConstructorMap.put(name, creator);
232 if (creator != null) {
234 ret = creator.newInstance(parameters);
235 } catch (IllegalArgumentException e) {
237 } catch (InstantiationException e) {
239 } catch (IllegalAccessException e) {
241 } catch (InvocationTargetException e) {
248 public static Object invokeMethod(Method m, Object instance, Object... parameters) {
249 if (sBridgeOrWrapperLoader == null) return null;
253 ret = m.invoke(instance, parameters);
254 } catch (IllegalArgumentException e) {
256 } catch (IllegalAccessException e) {
258 } catch (InvocationTargetException e) {
260 } catch (NullPointerException e) {
267 // Convert between wrapper and bridge instance.
268 public static Object getBridgeOrWrapper(Object instance) {
269 if (sBridgeOrWrapperLoader == null) return null;
270 if (instance == null) return null;
271 Class<?> clazz = instance.getClass();
272 Method method = sBridgeWrapperMap.get(clazz);
273 if (method == null) {
274 String methodName = "getBridge";
276 methodName = "getWrapper";
279 method = clazz.getMethod(methodName);
280 } catch (NoSuchMethodException e) {
283 if (method != null) sBridgeWrapperMap.put(clazz, method);
285 return invokeMethod(method, instance);
288 private static boolean isWrapper() {
289 return !ReflectionHelper.class.getPackage().getName().equals(INTERNAL_PACKAGE);
293 sIsWrapper = isWrapper();