| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.lang.reflect.*; |
| 10 |
import java.io.*; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class MemberBox implements Serializable |
| 21 |
{ |
| 22 |
static final long serialVersionUID = 6358550398665688245L; |
| 23 |
|
| 24 |
private transient Member memberObject; |
| 25 |
transient Class<?>[] argTypes; |
| 26 |
transient Object delegateTo; |
| 27 |
transient boolean vararg; |
| 28 |
|
| 29 |
|
| 30 |
MemberBox(Method method) |
| 31 |
{ |
| 32 |
init(method); |
| 33 |
} |
| 34 |
|
| 35 |
MemberBox(Constructor<?> constructor) |
| 36 |
{ |
| 37 |
init(constructor); |
| 38 |
} |
| 39 |
|
| 40 |
private void init(Method method) |
| 41 |
{ |
| 42 |
this.memberObject = method; |
| 43 |
this.argTypes = method.getParameterTypes(); |
| 44 |
this.vararg = VMBridge.instance.isVarArgs(method); |
| 45 |
} |
| 46 |
|
| 47 |
private void init(Constructor<?> constructor) |
| 48 |
{ |
| 49 |
this.memberObject = constructor; |
| 50 |
this.argTypes = constructor.getParameterTypes(); |
| 51 |
this.vararg = VMBridge.instance.isVarArgs(constructor); |
| 52 |
} |
| 53 |
|
| 54 |
Method method() |
| 55 |
{ |
| 56 |
return (Method)memberObject; |
| 57 |
} |
| 58 |
|
| 59 |
Constructor<?> ctor() |
| 60 |
{ |
| 61 |
return (Constructor<?>)memberObject; |
| 62 |
} |
| 63 |
|
| 64 |
Member member() |
| 65 |
{ |
| 66 |
return memberObject; |
| 67 |
} |
| 68 |
|
| 69 |
boolean isMethod() |
| 70 |
{ |
| 71 |
return memberObject instanceof Method; |
| 72 |
} |
| 73 |
|
| 74 |
boolean isCtor() |
| 75 |
{ |
| 76 |
return memberObject instanceof Constructor; |
| 77 |
} |
| 78 |
|
| 79 |
boolean isStatic() |
| 80 |
{ |
| 81 |
return Modifier.isStatic(memberObject.getModifiers()); |
| 82 |
} |
| 83 |
|
| 84 |
String getName() |
| 85 |
{ |
| 86 |
return memberObject.getName(); |
| 87 |
} |
| 88 |
|
| 89 |
Class<?> getDeclaringClass() |
| 90 |
{ |
| 91 |
return memberObject.getDeclaringClass(); |
| 92 |
} |
| 93 |
|
| 94 |
String toJavaDeclaration() |
| 95 |
{ |
| 96 |
StringBuilder sb = new StringBuilder(); |
| 97 |
if (isMethod()) { |
| 98 |
Method method = method(); |
| 99 |
sb.append(method.getReturnType()); |
| 100 |
sb.append(' '); |
| 101 |
sb.append(method.getName()); |
| 102 |
} else { |
| 103 |
Constructor<?> ctor = ctor(); |
| 104 |
String name = ctor.getDeclaringClass().getName(); |
| 105 |
int lastDot = name.lastIndexOf('.'); |
| 106 |
if (lastDot >= 0) { |
| 107 |
name = name.substring(lastDot + 1); |
| 108 |
} |
| 109 |
sb.append(name); |
| 110 |
} |
| 111 |
sb.append(JavaMembers.liveConnectSignature(argTypes)); |
| 112 |
return sb.toString(); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public String toString() |
| 117 |
{ |
| 118 |
return memberObject.toString(); |
| 119 |
} |
| 120 |
|
| 121 |
Object invoke(Object target, Object[] args) |
| 122 |
{ |
| 123 |
Method method = method(); |
| 124 |
try { |
| 125 |
try { |
| 126 |
return method.invoke(target, args); |
| 127 |
} catch (IllegalAccessException ex) { |
| 128 |
Method accessible = searchAccessibleMethod(method, argTypes); |
| 129 |
if (accessible != null) { |
| 130 |
memberObject = accessible; |
| 131 |
method = accessible; |
| 132 |
} else { |
| 133 |
if (!VMBridge.instance.tryToMakeAccessible(method)) { |
| 134 |
throw Context.throwAsScriptRuntimeEx(ex); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return method.invoke(target, args); |
| 139 |
} |
| 140 |
} catch (InvocationTargetException ite) { |
| 141 |
|
| 142 |
Throwable e = ite; |
| 143 |
do { |
| 144 |
e = ((InvocationTargetException) e).getTargetException(); |
| 145 |
} while ((e instanceof InvocationTargetException)); |
| 146 |
if (e instanceof ContinuationPending) |
| 147 |
throw (ContinuationPending) e; |
| 148 |
throw Context.throwAsScriptRuntimeEx(e); |
| 149 |
} catch (Exception ex) { |
| 150 |
throw Context.throwAsScriptRuntimeEx(ex); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Object newInstance(Object[] args) |
| 155 |
{ |
| 156 |
Constructor<?> ctor = ctor(); |
| 157 |
try { |
| 158 |
try { |
| 159 |
return ctor.newInstance(args); |
| 160 |
} catch (IllegalAccessException ex) { |
| 161 |
if (!VMBridge.instance.tryToMakeAccessible(ctor)) { |
| 162 |
throw Context.throwAsScriptRuntimeEx(ex); |
| 163 |
} |
| 164 |
} |
| 165 |
return ctor.newInstance(args); |
| 166 |
} catch (Exception ex) { |
| 167 |
throw Context.throwAsScriptRuntimeEx(ex); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private static Method searchAccessibleMethod(Method method, Class<?>[] params) |
| 172 |
{ |
| 173 |
int modifiers = method.getModifiers(); |
| 174 |
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) { |
| 175 |
Class<?> c = method.getDeclaringClass(); |
| 176 |
if (!Modifier.isPublic(c.getModifiers())) { |
| 177 |
String name = method.getName(); |
| 178 |
Class<?>[] intfs = c.getInterfaces(); |
| 179 |
for (int i = 0, N = intfs.length; i != N; ++i) { |
| 180 |
Class<?> intf = intfs[i]; |
| 181 |
if (Modifier.isPublic(intf.getModifiers())) { |
| 182 |
try { |
| 183 |
return intf.getMethod(name, params); |
| 184 |
} catch (NoSuchMethodException ex) { |
| 185 |
} catch (SecurityException ex) { } |
| 186 |
} |
| 187 |
} |
| 188 |
for (;;) { |
| 189 |
c = c.getSuperclass(); |
| 190 |
if (c == null) { break; } |
| 191 |
if (Modifier.isPublic(c.getModifiers())) { |
| 192 |
try { |
| 193 |
Method m = c.getMethod(name, params); |
| 194 |
int mModifiers = m.getModifiers(); |
| 195 |
if (Modifier.isPublic(mModifiers) |
| 196 |
&& !Modifier.isStatic(mModifiers)) |
| 197 |
{ |
| 198 |
return m; |
| 199 |
} |
| 200 |
} catch (NoSuchMethodException ex) { |
| 201 |
} catch (SecurityException ex) { } |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
private void readObject(ObjectInputStream in) |
| 210 |
throws IOException, ClassNotFoundException |
| 211 |
{ |
| 212 |
in.defaultReadObject(); |
| 213 |
Member member = readMember(in); |
| 214 |
if (member instanceof Method) { |
| 215 |
init((Method)member); |
| 216 |
} else { |
| 217 |
init((Constructor<?>)member); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
private void writeObject(ObjectOutputStream out) |
| 222 |
throws IOException |
| 223 |
{ |
| 224 |
out.defaultWriteObject(); |
| 225 |
writeMember(out, memberObject); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private static void writeMember(ObjectOutputStream out, Member member) |
| 236 |
throws IOException |
| 237 |
{ |
| 238 |
if (member == null) { |
| 239 |
out.writeBoolean(false); |
| 240 |
return; |
| 241 |
} |
| 242 |
out.writeBoolean(true); |
| 243 |
if (!(member instanceof Method || member instanceof Constructor)) |
| 244 |
throw new IllegalArgumentException("not Method or Constructor"); |
| 245 |
out.writeBoolean(member instanceof Method); |
| 246 |
out.writeObject(member.getName()); |
| 247 |
out.writeObject(member.getDeclaringClass()); |
| 248 |
if (member instanceof Method) { |
| 249 |
writeParameters(out, ((Method) member).getParameterTypes()); |
| 250 |
} else { |
| 251 |
writeParameters(out, ((Constructor<?>) member).getParameterTypes()); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private static Member readMember(ObjectInputStream in) |
| 259 |
throws IOException, ClassNotFoundException |
| 260 |
{ |
| 261 |
if (!in.readBoolean()) |
| 262 |
return null; |
| 263 |
boolean isMethod = in.readBoolean(); |
| 264 |
String name = (String) in.readObject(); |
| 265 |
Class<?> declaring = (Class<?>) in.readObject(); |
| 266 |
Class<?>[] parms = readParameters(in); |
| 267 |
try { |
| 268 |
if (isMethod) { |
| 269 |
return declaring.getMethod(name, parms); |
| 270 |
} else { |
| 271 |
return declaring.getConstructor(parms); |
| 272 |
} |
| 273 |
} catch (NoSuchMethodException e) { |
| 274 |
throw new IOException("Cannot find member: " + e); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
private static final Class<?>[] primitives = { |
| 279 |
Boolean.TYPE, |
| 280 |
Byte.TYPE, |
| 281 |
Character.TYPE, |
| 282 |
Double.TYPE, |
| 283 |
Float.TYPE, |
| 284 |
Integer.TYPE, |
| 285 |
Long.TYPE, |
| 286 |
Short.TYPE, |
| 287 |
Void.TYPE |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private static void writeParameters(ObjectOutputStream out, Class<?>[] parms) |
| 297 |
throws IOException |
| 298 |
{ |
| 299 |
out.writeShort(parms.length); |
| 300 |
outer: |
| 301 |
for (int i=0; i < parms.length; i++) { |
| 302 |
Class<?> parm = parms[i]; |
| 303 |
boolean primitive = parm.isPrimitive(); |
| 304 |
out.writeBoolean(primitive); |
| 305 |
if (!primitive) { |
| 306 |
out.writeObject(parm); |
| 307 |
continue; |
| 308 |
} |
| 309 |
for (int j=0; j < primitives.length; j++) { |
| 310 |
if (parm.equals(primitives[j])) { |
| 311 |
out.writeByte(j); |
| 312 |
continue outer; |
| 313 |
} |
| 314 |
} |
| 315 |
throw new IllegalArgumentException("Primitive " + parm + |
| 316 |
" not found"); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private static Class<?>[] readParameters(ObjectInputStream in) |
| 324 |
throws IOException, ClassNotFoundException |
| 325 |
{ |
| 326 |
Class<?>[] result = new Class[in.readShort()]; |
| 327 |
for (int i=0; i < result.length; i++) { |
| 328 |
if (!in.readBoolean()) { |
| 329 |
result[i] = (Class<?>) in.readObject(); |
| 330 |
continue; |
| 331 |
} |
| 332 |
result[i] = primitives[in.readByte()]; |
| 333 |
} |
| 334 |
return result; |
| 335 |
} |
| 336 |
} |
| 337 |
|