Debian Codegraph @

rhino/src/org/mozilla/javascript/WrapFactory.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 // API class
8
9 package org.mozilla.javascript;
10
11 /**
12 * Embeddings that wish to provide their own custom wrappings for Java
13 * objects may extend this class and call
14 * {@link Context#setWrapFactory(WrapFactory)}
15 * Once an instance of this class or an extension of this class is enabled
16 * for a given context (by calling setWrapFactory on that context), Rhino
17 * will call the methods of this class whenever it needs to wrap a value
18 * resulting from a call to a Java method or an access to a Java field.
19 *
20 * @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory)
21 * @since 1.5 Release 4
22 */
23 public class WrapFactory
24 {
25 /**
26 * Wrap the object.
27 * <p>
28 * The value returned must be one of
29 * <UL>
30 * <LI>java.lang.Boolean</LI>
31 * <LI>java.lang.String</LI>
32 * <LI>java.lang.Number</LI>
33 * <LI>org.mozilla.javascript.Scriptable objects</LI>
34 * <LI>The value returned by Context.getUndefinedValue()</LI>
35 * <LI>null</LI>
36 * </UL>
37 * @param cx the current Context for this thread
38 * @param scope the scope of the executing script
39 * @param obj the object to be wrapped. Note it can be null.
40 * @param staticType type hint. If security restrictions prevent to wrap
41 object based on its class, staticType will be used instead.
42 * @return the wrapped value.
43 */
44 public Object wrap(Context cx, Scriptable scope,
45 Object obj, Class<?> staticType)
46 {
47 if (obj == null || obj == Undefined.instance
48 || obj instanceof Scriptable)
49 {
50 return obj;
51 }
52 if (staticType != null && staticType.isPrimitive()) {
53 if (staticType == Void.TYPE)
54 return Undefined.instance;
55 if (staticType == Character.TYPE)
56 return Integer.valueOf(((Character) obj).charValue());
57 return obj;
58 }
59 if (!isJavaPrimitiveWrap()) {
60 if (obj instanceof String || obj instanceof Number
61 || obj instanceof Boolean)
62 {
63 return obj;
64 } else if (obj instanceof Character) {
65 return String.valueOf(((Character)obj).charValue());
66 }
67 }
68 Class<?> cls = obj.getClass();
69 if (cls.isArray()) {
70 return NativeJavaArray.wrap(scope, obj);
71 }
72 return wrapAsJavaObject(cx, scope, obj, staticType);
73 }
74
75 /**
76 * Wrap an object newly created by a constructor call.
77 * @param cx the current Context for this thread
78 * @param scope the scope of the executing script
79 * @param obj the object to be wrapped
80 * @return the wrapped value.
81 */
82 public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj)
83 {
84 if (obj instanceof Scriptable) {
85 return (Scriptable)obj;
86 }
87 Class<?> cls = obj.getClass();
88 if (cls.isArray()) {
89 return NativeJavaArray.wrap(scope, obj);
90 }
91 return wrapAsJavaObject(cx, scope, obj, null);
92 }
93
94 /**
95 * Wrap Java object as Scriptable instance to allow full access to its
96 * methods and fields from JavaScript.
97 * <p>
98 * {@link #wrap(Context, Scriptable, Object, Class)} and
99 * {@link #wrapNewObject(Context, Scriptable, Object)} call this method
100 * when they can not convert <tt>javaObject</tt> to JavaScript primitive
101 * value or JavaScript array.
102 * <p>
103 * Subclasses can override the method to provide custom wrappers
104 * for Java objects.
105 * @param cx the current Context for this thread
106 * @param scope the scope of the executing script
107 * @param javaObject the object to be wrapped
108 * @param staticType type hint. If security restrictions prevent to wrap
109 object based on its class, staticType will be used instead.
110 * @return the wrapped value which shall not be null
111 */
112 public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
113 Object javaObject, Class<?> staticType)
114 {
115 return new NativeJavaObject(scope, javaObject, staticType);
116 }
117
118 /**
119 * Wrap a Java class as Scriptable instance to allow access to its static
120 * members and fields and use as constructor from JavaScript.
121 * <p>
122 * Subclasses can override this method to provide custom wrappers for
123 * Java classes.
124 *
125 * @param cx the current Context for this thread
126 * @param scope the scope of the executing script
127 * @param javaClass the class to be wrapped
128 * @return the wrapped value which shall not be null
129 * @since 1.7R3
130 */
131 public Scriptable wrapJavaClass(Context cx, Scriptable scope,
132 Class<?> javaClass)
133 {
134 return new NativeJavaClass(scope, javaClass);
135 }
136
137 /**
138 * Return <code>false</code> if result of Java method, which is instance of
139 * <code>String</code>, <code>Number</code>, <code>Boolean</code> and
140 * <code>Character</code>, should be used directly as JavaScript primitive
141 * type.
142 * By default the method returns true to indicate that instances of
143 * <code>String</code>, <code>Number</code>, <code>Boolean</code> and
144 * <code>Character</code> should be wrapped as any other Java object and
145 * scripts can access any Java method available in these objects.
146 * Use {@link #setJavaPrimitiveWrap(boolean)} to change this.
147 */
148 public final boolean isJavaPrimitiveWrap()
149 {
150 return javaPrimitiveWrap;
151 }
152
153 /**
154 * @see #isJavaPrimitiveWrap()
155 */
156 public final void setJavaPrimitiveWrap(boolean value)
157 {
158 Context cx = Context.getCurrentContext();
159 if (cx != null && cx.isSealed()) {
160 Context.onSealedMutation();
161 }
162 javaPrimitiveWrap = value;
163 }
164
165 private boolean javaPrimitiveWrap = true;
166
167 }