Debian Codegraph @

rhino/src/org/mozilla/javascript/jdk13/VMBridge_jdk13.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.jdk13;
8
9 import java.lang.reflect.AccessibleObject;
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationHandler;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.Member;
15 import java.lang.reflect.Proxy;
16
17 import org.mozilla.javascript.*;
18
19 public class VMBridge_jdk13 extends VMBridge
20 {
21 private ThreadLocal<Object[]> contextLocal = new ThreadLocal<Object[]>();
22
23 @Override
24 protected Object getThreadContextHelper()
25 {
26 // To make subsequent batch calls to getContext/setContext faster
27 // associate permanently one element array with contextLocal
28 // so getContext/setContext would need just to read/write the first
29 // array element.
30 // Note that it is necessary to use Object[], not Context[] to allow
31 // garbage collection of Rhino classes. For details see comments
32 // by Attila Szegedi in
33 // https://bugzilla.mozilla.org/show_bug.cgi?id=281067#c5
34
35 Object[] storage = contextLocal.get();
36 if (storage == null) {
37 storage = new Object[1];
38 contextLocal.set(storage);
39 }
40 return storage;
41 }
42
43 @Override
44 protected Context getContext(Object contextHelper)
45 {
46 Object[] storage = (Object[])contextHelper;
47 return (Context)storage[0];
48 }
49
50 @Override
51 protected void setContext(Object contextHelper, Context cx)
52 {
53 Object[] storage = (Object[])contextHelper;
54 storage[0] = cx;
55 }
56
57 @Override
58 protected ClassLoader getCurrentThreadClassLoader()
59 {
60 return Thread.currentThread().getContextClassLoader();
61 }
62
63 @Override
64 protected boolean tryToMakeAccessible(Object accessibleObject)
65 {
66 if (!(accessibleObject instanceof AccessibleObject)) {
67 return false;
68 }
69 AccessibleObject accessible = (AccessibleObject)accessibleObject;
70 if (accessible.isAccessible()) {
71 return true;
72 }
73 try {
74 accessible.setAccessible(true);
75 } catch (Exception ex) { }
76
77 return accessible.isAccessible();
78 }
79
80 @Override
81 protected Object getInterfaceProxyHelper(ContextFactory cf,
82 Class<?>[] interfaces)
83 {
84 // XXX: How to handle interfaces array withclasses from different
85 // class loaders? Using cf.getApplicationClassLoader() ?
86 ClassLoader loader = interfaces[0].getClassLoader();
87 Class<?> cl = Proxy.getProxyClass(loader, interfaces);
88 Constructor<?> c;
89 try {
90 c = cl.getConstructor(new Class[] { InvocationHandler.class });
91 } catch (NoSuchMethodException ex) {
92 // Should not happen
93 throw Kit.initCause(new IllegalStateException(), ex);
94 }
95 return c;
96 }
97
98 @Override
99 protected Object newInterfaceProxy(Object proxyHelper,
100 final ContextFactory cf,
101 final InterfaceAdapter adapter,
102 final Object target,
103 final Scriptable topScope)
104 {
105 Constructor<?> c = (Constructor<?>)proxyHelper;
106
107 InvocationHandler handler = new InvocationHandler() {
108 public Object invoke(Object proxy,
109 Method method,
110 Object[] args)
111 {
112 // In addition to methods declared in the interface, proxies
113 // also route some java.lang.Object methods through the
114 // invocation handler.
115 if (method.getDeclaringClass() == Object.class) {
116 String methodName = method.getName();
117 if (methodName.equals("equals")) {
118 Object other = args[0];
119 // Note: we could compare a proxy and its wrapped function
120 // as equal here but that would break symmetry of equal().
121 // The reason == suffices here is that proxies are cached
122 // in ScriptableObject (see NativeJavaObject.coerceType())
123 return Boolean.valueOf(proxy == other);
124 }
125 if (methodName.equals("hashCode")) {
126 return Integer.valueOf(target.hashCode());
127 }
128 if (methodName.equals("toString")) {
129 return "Proxy[" + target.toString() + "]";
130 }
131 }
132 return adapter.invoke(cf, target, topScope, proxy, method, args);
133 }
134 };
135 Object proxy;
136 try {
137 proxy = c.newInstance(handler);
138 } catch (InvocationTargetException ex) {
139 throw Context.throwAsScriptRuntimeEx(ex);
140 } catch (IllegalAccessException ex) {
141 // Should not happen
142 throw Kit.initCause(new IllegalStateException(), ex);
143 } catch (InstantiationException ex) {
144 // Should not happen
145 throw Kit.initCause(new IllegalStateException(), ex);
146 }
147 return proxy;
148 }
149
150 @Override
151 protected boolean isVarArgs(Member member) {
152 return false;
153 }
154 }