public IntegerValue sizeof(Context ctxt) {
    IntegerValue size = new IntegerValue(0);
    IntegerValue eight = new IntegerValue(8);

    for (int i = 0; i < fields.size(); i++) {
      Field fi = (Field) fields.elementAt(i);
      /* TODO:
      try
      {
          StdIntegerType b = StdIntegerType.getBuiltinType(fi.getFieldType());
          if (b instanceof BitFieldType)
          {
              size = size.add(new IntegerValue(((BitFieldType) b)
                      .getLength()));
              continue;
          }
      }
      catch (ClassCastException _)
      {
      }
          */
      size = size.add(fi.sizeof(ctxt).multiply(eight));
    }
    return size.divide(eight);
  }
 public IntegerValue sizeof(Context ctxt) {
   IntegerValue eight = new IntegerValue(8);
   IntegerValue size = bitsizeof(ctxt);
   if (size.remainder(eight).compareTo(new IntegerValue(0)) != 0) {
     throw new RuntimeException("sizeof not integer: " + size);
   }
   return size.divide(eight);
 }
Ejemplo n.º 3
0
  void ckIntValue(Value retValue) {
    Field theValueField = targetClass.fieldByName("eintValue");
    IntegerValue theValue = (IntegerValue) targetClass.getValue(theValueField);

    int vv = theValue.value();
    int rv = ((IntegerValue) retValue).value();
    if (vv != rv) {
      failure("failure: int: expected " + vv + ", got " + rv);
    } else {
      System.out.println("Passed: int " + rv);
      earlyReturns++;
    }
  }
Ejemplo n.º 4
0
  /**
   * Factory method which tries to parse an untyped value.
   *
   * @param pTyped
   * @param pUriStr
   * @return <code>Value</code> instance
   * @throws IllegalArgumentException if parsing failed.
   */
  public static Value parse(boolean pTyped, URIString pUriStr) throws IllegalArgumentException {
    // TODO: tracing TRC.debug(uriStr.toString());
    if (pTyped) return parseTypedValue(pUriStr);
    URIString uriStr = pUriStr.deepCopy();
    Value value;
    try {
      StringValue strVal = (StringValue) StringValue.parse(uriStr);

      // string like value
      // is this string an instance reference?
      try {
        URI ref = URI.parseRef(new URIString(strVal.toString()), false);
        value = new ReferenceValue(ref);
      } catch (IllegalArgumentException e) {
        if ((value = DateTimeValue.parse(strVal.toString())) == null) {
          // if not dateTimeValue, it is stringvalue
          value = strVal;
        }
      }
      pUriStr.set(uriStr);
      return value;
    } catch (IllegalArgumentException e) {
      // non string like value
      if ((value = IntegerValue.parse(uriStr)) != null
          || (value = RealValue.parse(uriStr)) != null
          || (value = BooleanValue.parse(uriStr)) != null
          || (value = CharValue.parse(uriStr)) != null) {
        pUriStr.set(uriStr);
        return value;
      }
      String msg = "Failed to parse untyped value!\n" + uriStr.markPosition();
      throw new IllegalArgumentException(msg);
    }
  }
Ejemplo n.º 5
0
  void ckArrayValue(Value retValue) {
    Field theValueField = targetClass.fieldByName("eintArrayValue");
    ArrayReference theValue = (ArrayReference) targetClass.getValue(theValueField);
    IntegerValue theElem2 = (IntegerValue) theValue.getValue(2);

    ArrayReference theRetValue = (ArrayReference) retValue;
    IntegerValue retElem2 = (IntegerValue) theRetValue.getValue(2);
    int vv = theElem2.value();
    int rv = retElem2.value();
    if (vv != rv) {
      failure("failure: in[2]: expected " + vv + ", got " + rv);
    } else {
      System.out.println("Passed: int[2]: " + rv);
      earlyReturns++;
    }
  }
Ejemplo n.º 6
0
 /**
  * Compare the value to another dateTime value, following the XPath comparison semantics
  *
  * @param other The other dateTime value
  * @param context XPath dynamic evaluation context
  * @return negative value if this one is the earler, 0 if they are chronologically equal, positive
  *     value if this one is the later. For this purpose, dateTime values with an unknown timezone
  *     are considered to be values in the implicit timezone (the Comparable interface requires a
  *     total ordering).
  * @throws ClassCastException if the other value is not a DateTimeValue (the parameter is declared
  *     as CalendarValue to satisfy the interface)
  * @throws NoDynamicContextException if the implicit timezone is needed and is not available
  */
 public int compareTo(CalendarValue other, XPathContext context) throws NoDynamicContextException {
   if (!(other instanceof DateTimeValue)) {
     throw new ClassCastException("DateTime values are not comparable to " + other.getClass());
   }
   DateTimeValue v2 = (DateTimeValue) other;
   if (getTimezoneInMinutes() == v2.getTimezoneInMinutes()) {
     // both values are in the same timezone (explicitly or implicitly)
     if (year != v2.year) {
       return IntegerValue.signum(year - v2.year);
     }
     if (month != v2.month) {
       return IntegerValue.signum(month - v2.month);
     }
     if (day != v2.day) {
       return IntegerValue.signum(day - v2.day);
     }
     if (hour != v2.hour) {
       return IntegerValue.signum(hour - v2.hour);
     }
     if (minute != v2.minute) {
       return IntegerValue.signum(minute - v2.minute);
     }
     if (second != v2.second) {
       return IntegerValue.signum(second - v2.second);
     }
     if (microsecond != v2.microsecond) {
       return IntegerValue.signum(microsecond - v2.microsecond);
     }
     return 0;
   }
   return normalize(context).compareTo(v2.normalize(context), context);
 }
  public IntegerValue bitsizeof(Context ctxt) {
    IntegerValue size = new IntegerValue(0);

    for (int i = 0; i < fields.size(); i++) {
      Field fi = (Field) fields.elementAt(i);
      /*
       * TODO:
       * try
       * {
       *     StdIntegerType b = StdIntegerType.getBuiltinType(fi.getFieldType());
       *     if (b instanceof BitFieldType)
       *     {
       *         size = size.add(new IntegerValue(((BitFieldType) b).getLength()));
       *         continue;
       *     }
       * }
       * catch (ClassCastException _)
       * {
       * }
       */
      size = size.add(fi.bitsizeof(ctxt));
    }
    return size;
  }
Ejemplo n.º 8
0
  /* (non-Javadoc)
   * @see org.exist.xquery.value.Item#toJavaObject(java.lang.Class)
   */
  public Object toJavaObject(Class target) throws XPathException {
    if (target.isAssignableFrom(UntypedAtomicValue.class)) return this;
    else if (target == Object.class || target == String.class || target == CharSequence.class)
      return value;
    else if (target == double.class || target == Double.class) {
      DoubleValue v = (DoubleValue) convertTo(Type.DOUBLE);
      return new Double(v.getValue());
    } else if (target == float.class || target == Float.class) {
      FloatValue v = (FloatValue) convertTo(Type.FLOAT);
      return new Float(v.value);
    } else if (target == long.class || target == Long.class) {
      IntegerValue v = (IntegerValue) convertTo(Type.LONG);
      return new Long(v.getInt());
    } else if (target == int.class || target == Integer.class) {
      IntegerValue v = (IntegerValue) convertTo(Type.INT);
      return new Integer(v.getInt());
    } else if (target == short.class || target == Short.class) {
      IntegerValue v = (IntegerValue) convertTo(Type.SHORT);
      return new Short((short) v.getInt());
    } else if (target == byte.class || target == Byte.class) {
      IntegerValue v = (IntegerValue) convertTo(Type.BYTE);
      return new Byte((byte) v.getInt());
    } else if (target == boolean.class || target == Boolean.class) {
      return Boolean.valueOf(effectiveBooleanValue());
    } else if (target == char.class || target == Character.class) {
      if (value.length() > 1 || value.length() == 0)
        throw new XPathException(
            "cannot convert string with length = 0 or length > 1 to Java character");
      return new Character(value.charAt(0));
    }

    throw new XPathException(
        "cannot convert value of type "
            + Type.getTypeName(getType())
            + " to Java object of type "
            + target.getName());
  }
 public int hashCode() {
   return super.hashCode() ^ value.hashCode();
 }
Ejemplo n.º 10
0
 private static Value parseTypedValue(URIString pUriStr) throws IllegalArgumentException {
   URIString uriStr = pUriStr.deepCopy();
   int typeInfoPos = uriStr.getPos();
   String typeInfo = parseTypeInfo(uriStr);
   if (typeInfo == null) {
     String msg = "typeInfo expected!\n" + uriStr.markPosition();
     throw new IllegalArgumentException(msg);
   }
   int valuePos = uriStr.getPos();
   Value val;
   try {
     if (typeInfo.equalsIgnoreCase(MOF.DT_STR)) {
       val = StringValue.parse(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.REFERENCE)) {
       val = parseTypedReference(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_DATETIME)) {
       val = parseTypedDateTime(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_CHAR16)) {
       val = CharValue.parse(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_BOOL)) {
       val = BooleanValue.parse(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT8)) {
       val = IntegerValue.parseSigned(uriStr, 8);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT16)) {
       val = IntegerValue.parseSigned(uriStr, 16);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT32)) {
       val = IntegerValue.parseSigned(uriStr, 32);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT64)) {
       val = IntegerValue.parseSigned(uriStr, 64);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT8)) {
       val = IntegerValue.parseUnsigned(uriStr, 8);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT16)) {
       val = IntegerValue.parseUnsigned(uriStr, 16);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT32)) {
       val = IntegerValue.parseUnsigned(uriStr, 32);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT64)) {
       val = IntegerValue.parseUnsigned(uriStr, 64);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL32)) {
       val = RealValue.parseFloat(uriStr);
     } else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL64)) {
       val = RealValue.parseDouble(uriStr);
     } else {
       val = null;
     }
   } catch (IllegalArgumentException e) {
     String msg =
         "Failed to parse "
             + typeInfo
             + " value!\n"
             + uriStr.markPosition(valuePos)
             + "Nested message:\n"
             + e.getMessage();
     throw new IllegalArgumentException(msg);
   }
   if (val == null) {
     String msg = "Unknown type:" + typeInfo + "!\n" + uriStr.markPosition(typeInfoPos);
     throw new IllegalArgumentException(msg);
   }
   pUriStr.set(uriStr);
   return val;
 }
Ejemplo n.º 11
0
  /**
   * Move through a list of stack frames, searching for references to code found in the current
   * sketch. Return with a RunnerException that contains the location of the error, or if nothing is
   * found, just return with a RunnerException that wraps the error message itself.
   */
  protected SketchException findException(
      String message, ObjectReference or, ThreadReference thread) {
    try {
      // use to dump the stack for debugging
      //      for (StackFrame frame : thread.frames()) {
      //        System.out.println("frame: " + frame);
      //      }

      List<StackFrame> frames = thread.frames();
      for (StackFrame frame : frames) {
        try {
          Location location = frame.location();
          String filename = null;
          filename = location.sourceName();
          int lineNumber = location.lineNumber() - 1;
          SketchException rex = build.placeException(message, filename, lineNumber);
          if (rex != null) {
            return rex;
          }
        } catch (AbsentInformationException e) {
          // Any of the thread.blah() methods can throw an AbsentInformationEx
          // if that bit of data is missing. If so, just write out the error
          // message to the console.
          // e.printStackTrace();  // not useful
          exception = new SketchException(message);
          exception.hideStackTrace();
          listener.statusError(exception);
        }
      }
    } catch (IncompatibleThreadStateException e) {
      // This shouldn't happen, but if it does, print the exception in case
      // it's something that needs to be debugged separately.
      e.printStackTrace();
    }
    // before giving up, try to extract from the throwable object itself
    // since sometimes exceptions are re-thrown from a different context
    try {
      // assume object reference is Throwable, get stack trace
      Method method =
          ((ClassType) or.referenceType())
              .concreteMethodByName("getStackTrace", "()[Ljava/lang/StackTraceElement;");
      ArrayReference result =
          (ArrayReference)
              or.invokeMethod(
                  thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
      // iterate through stack frames and pull filename and line number for each
      for (Value val : result.getValues()) {
        ObjectReference ref = (ObjectReference) val;
        method =
            ((ClassType) ref.referenceType())
                .concreteMethodByName("getFileName", "()Ljava/lang/String;");
        StringReference strref =
            (StringReference)
                ref.invokeMethod(
                    thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
        String filename = strref == null ? "Unknown Source" : strref.value();
        method = ((ClassType) ref.referenceType()).concreteMethodByName("getLineNumber", "()I");
        IntegerValue intval =
            (IntegerValue)
                ref.invokeMethod(
                    thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
        int lineNumber = intval.intValue() - 1;
        SketchException rex = build.placeException(message, filename, lineNumber);
        if (rex != null) {
          return rex;
        }
      }
      //      for (Method m : ((ClassType) or.referenceType()).allMethods()) {
      //        System.out.println(m + " | " + m.signature() + " | " + m.genericSignature());
      //      }
      // Implemented for 2.0b9, writes a stack trace when there's an internal error inside core.
      method = ((ClassType) or.referenceType()).concreteMethodByName("printStackTrace", "()V");
      //      System.err.println("got method " + method);
      or.invokeMethod(
          thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);

    } catch (Exception e) {
      e.printStackTrace();
    }
    // Give up, nothing found inside the pile of stack frames
    SketchException rex = new SketchException(message);
    // exception is being created /here/, so stack trace is not useful
    rex.hideStackTrace();
    return rex;
  }