Debian Codegraph @

rhino/src/org/mozilla/javascript/jdk15/VMBridge_jdk15.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.jdk15;
8
9 import java.lang.reflect.Member;
10 import java.lang.reflect.Method;
11 import java.lang.reflect.Constructor;
12 import java.util.Iterator;
13 import org.mozilla.javascript.*;
14
15 public class VMBridge_jdk15 extends org.mozilla.javascript.jdk13.VMBridge_jdk13
16 {
17 public VMBridge_jdk15() throws SecurityException, InstantiationException {
18 try {
19 // Just try and see if we can access the isVarArgs method.
20 // We want to fail loading if the method does not exist
21 // so that we can load a bridge to an older JDK instead.
22 Method.class.getMethod("isVarArgs", (Class[]) null);
23 } catch (NoSuchMethodException e) {
24 // Throw a fitting exception that is handled by
25 // org.mozilla.javascript.Kit.newInstanceOrNull:
26 throw new InstantiationException(e.getMessage());
27 }
28 }
29
30 @Override
31 public boolean isVarArgs(Member member) {
32 if (member instanceof Method)
33 return ((Method) member).isVarArgs();
34 else if (member instanceof Constructor)
35 return ((Constructor<?>) member).isVarArgs();
36 else
37 return false;
38 }
39
40 /**
41 * If "obj" is a java.util.Iterator or a java.lang.Iterable, return a
42 * wrapping as a JavaScript Iterator. Otherwise, return null.
43 * This method is in VMBridge since Iterable is a JDK 1.5 addition.
44 */
45 @Override
46 public Iterator<?> getJavaIterator(Context cx, Scriptable scope, Object obj) {
47 if (obj instanceof Wrapper) {
48 Object unwrapped = ((Wrapper) obj).unwrap();
49 Iterator<?> iterator = null;
50 if (unwrapped instanceof Iterator)
51 iterator = (Iterator<?>) unwrapped;
52 if (unwrapped instanceof Iterable)
53 iterator = ((Iterable<?>)unwrapped).iterator();
54 return iterator;
55 }
56 return null;
57 }
58 }