| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
if (method.getDeclaringClass() == Object.class) { |
| 116 |
String methodName = method.getName(); |
| 117 |
if (methodName.equals("equals")) { |
| 118 |
Object other = args[0]; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 142 |
throw Kit.initCause(new IllegalStateException(), ex); |
| 143 |
} catch (InstantiationException ex) { |
| 144 |
|
| 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 |
} |