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 Class<?>[] params = new Class<?>[paramTypes.length];
39 for (int i = 0; i < paramTypes.length; i++) {
40 Object type = paramTypes[i];
41 // paramTypes can be string or Class<?>, if it's string,
42 // it means it's not in bridge/wrapper's classLoader,
43 // we need to load it from wrapper/bridge's loader.
44 if (type instanceof Class<?>) {
45 params[i] = (Class<?>) type;
46 } else if (type instanceof String) {
47 params[i] = loadClass((String) type);
51 return clazz.getConstructor(params);
52 } catch (NoSuchMethodException e) {
53 ReflectionHelper.handleException(e);
58 ConstructorHelper(String className, Object... paramTypes) {
59 this.fullClassName = className;
60 this.paramTypes = paramTypes;
64 private static Map<Class<?>, Method> sBridgeWrapperMap = new HashMap<Class<?>, Method>();
65 private static Map<String, Constructor<?>> sConstructorMap = new HashMap<String, Constructor<?>>();
66 private static Map<String, ConstructorHelper> sConstructorHelperMap =
67 new HashMap<String, ConstructorHelper>();
68 private static ClassLoader sBridgeOrWrapperLoader = null;
69 private static Context sBridgeContext = null;
70 private static boolean sIsWrapper;
71 private final static String INTERNAL_PACKAGE = "org.xwalk.core.internal";
72 private final static String LIBRARY_APK_PACKAGE = "org.xwalk.core";
74 private static boolean sAllowCrossPackage = false;
75 private static SharedXWalkExceptionHandler sExceptionHandler = null;
77 static void setExceptionHandler(SharedXWalkExceptionHandler handler) {
78 sExceptionHandler = handler;
81 public static boolean shouldUseLibrary() {
82 // TODO(wang16): There are many other conditions here.
83 // e.g. Whether application uses the ApplicationClass we provided,
84 // Whether native library arch is correct.
87 ReflectionHelper.class.getClassLoader().loadClass(
88 INTERNAL_PACKAGE + "." + "ReflectionHelper");
90 } catch (ClassNotFoundException e) {
96 public static Context getBridgeContext() {
97 return sBridgeContext;
101 public static void allowCrossPackage() {
102 sAllowCrossPackage = true;
106 public static void init() {
109 if (shouldUseLibrary()) {
110 if (!sAllowCrossPackage) {
111 handleException("Use SharedXWalkView if you want to support shared mode");
113 XWalkApplication app = XWalkApplication.getApplication();
115 // TODO(wang16): Handle this well.
116 handleException("Shared mode requires XWalkApplication");
120 sBridgeContext = app.createPackageContext(
122 Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
123 } catch (PackageManager.NameNotFoundException e) {
126 if (sBridgeContext != null) {
127 app.addResource(sBridgeContext.getResources());
128 initClassLoader(sBridgeContext.getClassLoader(), sBridgeContext);
131 initClassLoader(ReflectionHelper.class.getClassLoader(), null);
136 public static void initClassLoader(ClassLoader loader, Context bridgeContext) {
137 sBridgeOrWrapperLoader = loader;
138 sBridgeContext = bridgeContext;
139 sBridgeWrapperMap.clear();
140 sConstructorMap.clear();
142 for (String name : sConstructorHelperMap.keySet()) {
143 ConstructorHelper helper = sConstructorHelperMap.get(name);
144 if (helper != null) sConstructorMap.put(name, helper.loadConstructor());
147 // Load the helper in bridge side and invoke the initClassLoader method of it
148 // with wrapper's classloader via reflection.
149 Class<?> helperInBridge =
150 sBridgeOrWrapperLoader.loadClass(INTERNAL_PACKAGE + "." + "ReflectionHelper");
151 Method initInBridge = helperInBridge.getMethod(
152 "initClassLoader", ClassLoader.class, Context.class);
153 initInBridge.invoke(null, ReflectionHelper.class.getClassLoader(), sBridgeContext);
155 // JavascriptInterface is an annotation class bridge will use but declared in
157 Class<?> javascriptInterface =
158 sBridgeOrWrapperLoader.loadClass("org.xwalk.core.JavascriptInterface");
159 Class<?> xwalkContentInInternal =
160 ReflectionHelper.class.getClassLoader().loadClass(
161 INTERNAL_PACKAGE + "." + "XWalkContent");
162 Method setJavascriptInterface = xwalkContentInInternal.getDeclaredMethod(
163 "setJavascriptInterfaceClass", javascriptInterface.getClass());
164 setJavascriptInterface.invoke(null, javascriptInterface);
166 } catch (Exception e) {
171 public static void registerConstructor(String name, String clazz, Object... params) {
172 sConstructorHelperMap.put(name, new ConstructorHelper(clazz, params));
175 public static Class<?> loadClass(String clazz) {
176 // Any embedder using Embedding API should only use the exposed APIs which are
177 // in wrapper, so the initialization process is always starting from wrapper.
178 if (sBridgeOrWrapperLoader == null) init();
179 if (sBridgeOrWrapperLoader == null) return null;
181 return sBridgeOrWrapperLoader.loadClass(clazz);
182 } catch (ClassNotFoundException e) {
188 public static Method loadMethod(Class<?> clazz, String name, Object... paramTypes) {
189 if (sBridgeOrWrapperLoader == null) return null;
190 Class<?>[] params = new Class<?>[paramTypes.length];
191 for (int i = 0; i < paramTypes.length; i++) {
192 Object type = paramTypes[i];
193 if (type instanceof Class<?>) {
194 params[i] = (Class<?>) type;
195 } else if (type instanceof String) {
196 params[i] = loadClass((String) type);
200 return clazz.getMethod(name, params);
201 } catch (NoSuchMethodException e) {
207 public static void handleException(Exception e) {
210 if (isWrapper() && sExceptionHandler != null) {
211 if (sExceptionHandler.handleException(e)) return;
214 throw new RuntimeException(e);
217 public static void handleException(String e) {
218 handleException(new RuntimeException(e));
221 public static Object createInstance(String name, Object... parameters) {
223 Constructor<?> creator = sConstructorMap.get(name);
224 if (creator == null) {
225 ConstructorHelper helper = sConstructorHelperMap.get(name);
226 if (helper != null) {
227 creator = helper.loadConstructor();
228 sConstructorMap.put(name, creator);
231 if (creator != null) {
233 ret = creator.newInstance(parameters);
234 } catch (IllegalArgumentException e) {
236 } catch (InstantiationException e) {
238 } catch (IllegalAccessException e) {
240 } catch (InvocationTargetException e) {
247 public static Object invokeMethod(Method m, Object instance, Object... parameters) {
248 if (sBridgeOrWrapperLoader == null) return null;
252 ret = m.invoke(instance, parameters);
253 } catch (IllegalArgumentException e) {
255 } catch (IllegalAccessException e) {
257 } catch (InvocationTargetException e) {
259 } catch (NullPointerException e) {
266 // Convert between wrapper and bridge instance.
267 public static Object getBridgeOrWrapper(Object instance) {
268 if (sBridgeOrWrapperLoader == null) return null;
269 if (instance == null) return null;
270 Class<?> clazz = instance.getClass();
271 Method method = sBridgeWrapperMap.get(clazz);
272 if (method == null) {
273 String methodName = "getBridge";
275 methodName = "getWrapper";
278 method = clazz.getMethod(methodName);
279 } catch (NoSuchMethodException e) {
282 if (method != null) sBridgeWrapperMap.put(clazz, method);
284 return invokeMethod(method, instance);
287 private static boolean isWrapper() {
288 return !ReflectionHelper.class.getPackage().getName().equals(INTERNAL_PACKAGE);
292 sIsWrapper = isWrapper();