public Object getDefaultValue(Class<?> hint) {
   Object value;
   if (hint == null) {
     if (javaObject instanceof Boolean) {
       hint = ScriptRuntime.BooleanClass;
     }
   }
   if (hint == null || hint == ScriptRuntime.StringClass) {
     value = javaObject.toString();
   } else {
     String converterName;
     if (hint == ScriptRuntime.BooleanClass) {
       converterName = "booleanValue";
     } else if (hint == ScriptRuntime.NumberClass) {
       converterName = "doubleValue";
     } else {
       throw Context.reportRuntimeError0("msg.default.value");
     }
     Object converterObject = get(converterName, this);
     if (converterObject instanceof Function) {
       Function f = (Function) converterObject;
       value = f.call(Context.getContext(), f.getParentScope(), this, ScriptRuntime.emptyArgs);
     } else {
       if (hint == ScriptRuntime.NumberClass && javaObject instanceof Boolean) {
         boolean b = ((Boolean) javaObject).booleanValue();
         value = ScriptRuntime.wrapNumber(b ? 1.0 : 0.0);
       } else {
         value = javaObject.toString();
       }
     }
   }
   return value;
 }
Exemplo n.º 2
0
  /**
   * Utility method which dynamically binds a Context to the current thread, if none already exists.
   */
  public static Object callMethod(
      ContextFactory factory,
      final Scriptable thisObj,
      final Function f,
      final Object[] args,
      final long argsToWrap) {
    if (f == null) {
      // See comments in getFunction
      return Undefined.instance;
    }
    if (factory == null) {
      factory = ContextFactory.getGlobal();
    }

    final Scriptable scope = f.getParentScope();
    if (argsToWrap == 0) {
      return Context.call(factory, f, scope, thisObj, args);
    }

    Context cx = Context.getCurrentContext();
    if (cx != null) {
      return doCall(cx, scope, thisObj, f, args, argsToWrap);
    } else {
      return factory.call(
          new ContextAction() {
            public Object run(Context cx) {
              return doCall(cx, scope, thisObj, f, args, argsToWrap);
            }
          });
    }
  }
Exemplo n.º 3
0
 /** evaluate implicit function syntax; e.g., A1(5) or A1(A2) */
 public static Data implicit(Function f, Real r) {
   Data value = null;
   try {
     value = f.evaluate(r);
   } catch (VisADException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
   } catch (RemoteException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
   }
   return value;
 }
Exemplo n.º 4
0
 /** evaluate the derive function */
 public static Data derive(Function f, VRealType rt) {
   Data val = null;
   try {
     val = f.derivative(rt.getRealType(), Data.NO_ERRORS);
   } catch (VisADException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
   } catch (RemoteException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
   }
   return val;
 }
  /**
   * Returns the class this method was called 'framesToSkip' frames up the caller hierarchy. JDK
   * used to have {@link sun.reflect.Reflection#getCallerClass(int)} until jdk 1.8 build 87. So we
   * have to resort to slow stack unwinding.
   *
   * <p>NOTE: <b>Extremely expensive! Please consider not using it. These aren't the droids you're
   * looking for!</b>
   */
  @SuppressWarnings("JavadocReference")
  @Nullable
  public static Class getCallerClass(int framesToSkip) {
    int frames = framesToSkip;

    StackTraceElement[] stackTrace = new Throwable().getStackTrace();
    String className = null;
    for (int i = 1; i <= frames; i++) {
      if (i >= stackTrace.length) {
        break;
      }
      StackTraceElement element = stackTrace[i];
      className = element.getClassName();
      if (className.equals("java.lang.reflect.Method")
          || className.equals("sun.reflect.NativeMethodAccessorImpl")
          || className.equals("sun.reflect.DelegatingMethodAccessorImpl")) {
        frames++;
        continue;
      }
      if (i == frames) {
        break;
      }
    }

    if (className == null) {
      // some plugins incorrectly expect not-null class too far up the caller chain,
      // so provide some not-null class for them
      className = ReflectionUtil.class.getName();
    }

    try {
      return Class.forName(className);
    } catch (ClassNotFoundException ignored) {
    }
    ClassLoader pluginClassLoader = PLUGIN_CLASS_LOADER_DETECTOR.fun(className);
    if (pluginClassLoader != null) {
      try {
        return Class.forName(className, false, pluginClassLoader);
      } catch (ClassNotFoundException ignored) {
      }
    }
    LOG.error(
        "Could not load class '"
            + className
            + "' using classLoader "
            + ReflectionUtil.class.getClassLoader()
            + "."
            + (stackTrace[1].getClassName().equals("com.intellij.openapi.util.IconLoader")
                ? " Use getIcon(String, Class) instead."
                : ""));
    return null;
  }
Exemplo n.º 6
0
 private static Object doCall(
     Context cx,
     Scriptable scope,
     Scriptable thisObj,
     Function f,
     Object[] args,
     long argsToWrap) {
   // Wrap the rest of objects
   for (int i = 0; i != args.length; ++i) {
     if (0 != (argsToWrap & (1 << i))) {
       Object arg = args[i];
       if (!(arg instanceof Scriptable)) {
         args[i] = cx.getWrapFactory().wrap(cx, scope, arg, null);
       }
     }
   }
   return f.call(cx, scope, thisObj, args);
 }