Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
   *
   * This Source Code Form is subject to the terms of the Mozilla Public
   * License, v. 2.0. If a copy of the MPL was not distributed with this
   * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  
  // API class
  
  package org.mozilla.javascript;

Embeddings that wish to provide their own custom wrappings for Java objects may extend this class and call Context.setWrapFactory(org.mozilla.javascript.WrapFactory) Once an instance of this class or an extension of this class is enabled for a given context (by calling setWrapFactory on that context), Rhino will call the methods of this class whenever it needs to wrap a value resulting from a call to a Java method or an access to a Java field.

 
 public class WrapFactory
 {
    
Wrap the object.

The value returned must be one of

  • java.lang.Boolean
  • java.lang.String
  • java.lang.Number
  • org.mozilla.javascript.Scriptable objects
  • The value returned by Context.getUndefinedValue()
  • null

Parameters:
cx the current Context for this thread
scope the scope of the executing script
obj the object to be wrapped. Note it can be null.
staticType type hint. If security restrictions prevent to wrap object based on its class, staticType will be used instead.
Returns:
the wrapped value.
 
     public Object wrap(Context cxScriptable scope,
                        Object objClass<?> staticType)
     {
         if (obj == null || obj == .
             || obj instanceof Scriptable)
         {
             return obj;
         }
         if (staticType != null && staticType.isPrimitive()) {
             if (staticType == .)
                 return .;
             if (staticType == .)
                 return Integer.valueOf(((Characterobj).charValue());
             return obj;
         }
         if (!isJavaPrimitiveWrap()) {
             if (obj instanceof String || obj instanceof Number
                 || obj instanceof Boolean)
             {
                 return obj;
             } else if (obj instanceof Character) {
                 return String.valueOf(((Character)obj).charValue());
             }
         }
         Class<?> cls = obj.getClass();
         if (cls.isArray()) {
             return NativeJavaArray.wrap(scopeobj);
         }
         return wrapAsJavaObject(cxscopeobjstaticType);
     }

    
Wrap an object newly created by a constructor call.

Parameters:
cx the current Context for this thread
scope the scope of the executing script
obj the object to be wrapped
Returns:
the wrapped value.
 
     public Scriptable wrapNewObject(Context cxScriptable scopeObject obj)
     {
         if (obj instanceof Scriptable) {
             return (Scriptable)obj;
         }
         Class<?> cls = obj.getClass();
         if (cls.isArray()) {
             return NativeJavaArray.wrap(scopeobj);
         }
         return wrapAsJavaObject(cxscopeobjnull);
     }

    
Wrap Java object as Scriptable instance to allow full access to its methods and fields from JavaScript.

wrap(org.mozilla.javascript.Context,org.mozilla.javascript.Scriptable,java.lang.Object,java.lang.Class) and wrapNewObject(org.mozilla.javascript.Context,org.mozilla.javascript.Scriptable,java.lang.Object) call this method when they can not convert javaObject to JavaScript primitive value or JavaScript array.

Subclasses can override the method to provide custom wrappers for Java objects.

Parameters:
cx the current Context for this thread
scope the scope of the executing script
javaObject the object to be wrapped
staticType type hint. If security restrictions prevent to wrap object based on its class, staticType will be used instead.
Returns:
the wrapped value which shall not be null
    public Scriptable wrapAsJavaObject(Context cxScriptable scope,
                                       Object javaObjectClass<?> staticType)
    {
        return new NativeJavaObject(scopejavaObjectstaticType);
    }

    
Wrap a Java class as Scriptable instance to allow access to its static members and fields and use as constructor from JavaScript.

Subclasses can override this method to provide custom wrappers for Java classes.

Parameters:
cx the current Context for this thread
scope the scope of the executing script
javaClass the class to be wrapped
Returns:
the wrapped value which shall not be null
Since:
1.7R3
    public Scriptable wrapJavaClass(Context cxScriptable scope,
                                    Class javaClass)
    {
        return new NativeJavaClass(scopejavaClass);
    }

    
Return false if result of Java method, which is instance of String, Number, Boolean and Character, should be used directly as JavaScript primitive type. By default the method returns true to indicate that instances of String, Number, Boolean and Character should be wrapped as any other Java object and scripts can access any Java method available in these objects. Use setJavaPrimitiveWrap(boolean) to change this.
    public final boolean isJavaPrimitiveWrap()
    {
        return ;
    }

    
    public final void setJavaPrimitiveWrap(boolean value)
    {
        Context cx = Context.getCurrentContext();
        if (cx != null && cx.isSealed()) {
            Context.onSealedMutation();
        }
         = value;
    }
    private boolean javaPrimitiveWrap = true;
New to GrepCode? Check out our FAQ X