Beispiel #1
0
 public static Object convertArrayElements(Class<?> arrayType, Object array) {
   Class<?> src = array.getClass().getComponentType();
   Class<?> dst = arrayType.getComponentType();
   if (src == null || dst == null) throw new IllegalArgumentException("not array type");
   Wrapper sw = (src.isPrimitive() ? Wrapper.forPrimitiveType(src) : null);
   Wrapper dw = (dst.isPrimitive() ? Wrapper.forPrimitiveType(dst) : null);
   int length;
   if (sw == null) {
     Object[] a = (Object[]) array;
     length = a.length;
     if (dw == null) return Arrays.copyOf(a, length, arrayType.asSubclass(Object[].class));
     Object res = dw.makeArray(length);
     dw.copyArrayUnboxing(a, 0, res, 0, length);
     return res;
   }
   length = j86.java.lang.reflect.Array.getLength(array);
   Object[] res;
   if (dw == null) {
     res = Arrays.copyOf(NO_ARGS_ARRAY, length, arrayType.asSubclass(Object[].class));
   } else {
     res = new Object[length];
   }
   sw.copyArrayBoxing(array, 0, res, 0, length);
   if (dw == null) return res;
   Object a = dw.makeArray(length);
   dw.copyArrayUnboxing(res, 0, a, 0, length);
   return a;
 }
 /**
  * Constructs a <code>BatchUpdateException</code> object initialized with a given <code>reason
  * </code>, <code>SQLState</code>, <code>vendorCode</code> <code>cause</code> and <code>
  * updateCounts</code>.
  *
  * <p>This constructor should be used when the returned update count may exceed {@link
  * Integer#MAX_VALUE}.
  *
  * <p>
  *
  * @param reason a description of the error
  * @param SQLState an XOPEN or SQL:2003 code identifying the exception
  * @param vendorCode an exception code used by a particular database vendor
  * @param updateCounts an array of <code>long</code>, with each element indicating the update
  *     count, <code>Statement.SUCCESS_NO_INFO</code> or <code>Statement.EXECUTE_FAILED</code> for
  *     each SQL command in the batch for JDBC drivers that continue processing after a command
  *     failure; an update count or <code>Statement.SUCCESS_NO_INFO</code> for each SQL command in
  *     the batch prior to the failure for JDBC drivers that stop processing after a command
  *     failure
  * @param cause the underlying reason for this <code>SQLException</code> (which is saved for later
  *     retrieval by the <code>getCause()</code> method); may be null indicating the cause is
  *     non-existent or unknown.
  * @since 1.8
  */
 public BatchUpdateException(
     String reason, String SQLState, int vendorCode, long[] updateCounts, Throwable cause) {
   super(reason, SQLState, vendorCode, cause);
   this.longUpdateCounts =
       (updateCounts == null) ? null : Arrays.copyOf(updateCounts, updateCounts.length);
   this.updateCounts = (longUpdateCounts == null) ? null : copyUpdateCount(longUpdateCounts);
 }
Beispiel #3
0
 /**
  * Return a method handle that takes the indicated number of typed arguments and returns an array
  * of them. The type argument is the array type.
  */
 public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
   Class<?> elemType = arrayType.getComponentType();
   if (elemType == null) throw new IllegalArgumentException("not an array: " + arrayType);
   // FIXME: Need more special casing and caching here.
   if (nargs >= MAX_JVM_ARITY / 2 - 1) {
     int slots = nargs;
     final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1; // 1 for receiver MH
     if (arrayType == double[].class || arrayType == long[].class) slots *= 2;
     if (slots > MAX_ARRAY_SLOTS)
       throw new IllegalArgumentException(
           "too many arguments: " + arrayType.getSimpleName() + ", length " + nargs);
   }
   if (elemType == Object.class) return varargsArray(nargs);
   // other cases:  primitive arrays, subtypes of Object[]
   MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
   MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
   if (mh != null) return mh;
   if (elemType.isPrimitive()) {
     MethodHandle builder = FILL_NEW_ARRAY;
     MethodHandle producer = buildArrayProducer(arrayType);
     mh = buildVarargsArray(builder, producer, nargs);
   } else {
     @SuppressWarnings("unchecked")
     Class<? extends Object[]> objArrayType = (Class<? extends Object[]>) arrayType;
     Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
     MethodHandle builder = FILL_NEW_TYPED_ARRAY.bindTo(example);
     MethodHandle producer = ARRAY_IDENTITY;
     mh = buildVarargsArray(builder, producer, nargs);
   }
   mh =
       mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
   assert (assertCorrectArity(mh, nargs));
   if (nargs < cache.length) cache[nargs] = mh;
   return mh;
 }
Beispiel #4
0
  /**
   * Facilitates the creation of simple "function objects" that implement one or more interfaces by
   * delegation to a provided {@link MethodHandle}, after appropriate type adaptation and partial
   * evaluation of arguments. Typically used as a <em>bootstrap method</em> for {@code
   * invokedynamic} call sites, to support the <em>lambda expression</em> and <em>method reference
   * expression</em> features of the Java Programming Language.
   *
   * <p>This is the general, more flexible metafactory; a streamlined version is provided by {@link
   * #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}. A general description of
   * the behavior of this method is provided {@link LambdaMetafactory above}.
   *
   * <p>The argument list for this method includes three fixed parameters, corresponding to the
   * parameters automatically stacked by the VM for the bootstrap method in an {@code invokedynamic}
   * invocation, and an {@code Object[]} parameter that contains additional parameters. The declared
   * argument list for this method is:
   *
   * <pre>{@code
   * CallSite altMetafactory(MethodHandles.Lookup caller,
   *                         String invokedName,
   *                         MethodType invokedType,
   *                         Object... args)
   * }</pre>
   *
   * <p>but it behaves as if the argument list is as follows:
   *
   * <pre>{@code
   * CallSite altMetafactory(MethodHandles.Lookup caller,
   *                         String invokedName,
   *                         MethodType invokedType,
   *                         MethodType samMethodType,
   *                         MethodHandle implMethod,
   *                         MethodType instantiatedMethodType,
   *                         int flags,
   *                         int markerInterfaceCount,  // IF flags has MARKERS set
   *                         Class... markerInterfaces, // IF flags has MARKERS set
   *                         int bridgeCount,           // IF flags has BRIDGES set
   *                         MethodType... bridges      // IF flags has BRIDGES set
   *                         )
   * }</pre>
   *
   * <p>Arguments that appear in the argument list for {@link #metafactory(MethodHandles.Lookup,
   * String, MethodType, MethodType, MethodHandle, MethodType)} have the same specification as in
   * that method. The additional arguments are interpreted as follows:
   *
   * <ul>
   *   <li>{@code flags} indicates additional options; this is a bitwise OR of desired flags.
   *       Defined flags are {@link #FLAG_BRIDGES}, {@link #FLAG_MARKERS}, and {@link
   *       #FLAG_SERIALIZABLE}.
   *   <li>{@code markerInterfaceCount} is the number of additional interfaces the function object
   *       should implement, and is present if and only if the {@code FLAG_MARKERS} flag is set.
   *   <li>{@code markerInterfaces} is a variable-length list of additional interfaces to implement,
   *       whose length equals {@code markerInterfaceCount}, and is present if and only if the
   *       {@code FLAG_MARKERS} flag is set.
   *   <li>{@code bridgeCount} is the number of additional method signatures the function object
   *       should implement, and is present if and only if the {@code FLAG_BRIDGES} flag is set.
   *   <li>{@code bridges} is a variable-length list of additional methods signatures to implement,
   *       whose length equals {@code bridgeCount}, and is present if and only if the {@code
   *       FLAG_BRIDGES} flag is set.
   * </ul>
   *
   * <p>Each class named by {@code markerInterfaces} is subject to the same restrictions as {@code
   * Rd}, the return type of {@code invokedType}, as described {@link LambdaMetafactory above}. Each
   * {@code MethodType} named by {@code bridges} is subject to the same restrictions as {@code
   * samMethodType}, as described {@link LambdaMetafactory above}.
   *
   * <p>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects will implement {@code
   * Serializable}, and will have a {@code writeReplace} method that returns an appropriate {@link
   * SerializedLambda}. The {@code caller} class must have an appropriate {@code
   * $deserializeLambda$} method, as described in {@link SerializedLambda}.
   *
   * <p>When the target of the {@code CallSite} returned from this method is invoked, the resulting
   * function objects are instances of a class with the following properties:
   *
   * <ul>
   *   <li>The class implements the interface named by the return type of {@code invokedType} and
   *       any interfaces named by {@code markerInterfaces}
   *   <li>The class declares methods with the name given by {@code invokedName}, and the signature
   *       given by {@code samMethodType} and additional signatures given by {@code bridges}
   *   <li>The class may override methods from {@code Object}, and may implement methods related to
   *       serialization.
   * </ul>
   *
   * @param caller Represents a lookup context with the accessibility privileges of the caller. When
   *     used with {@code invokedynamic}, this is stacked automatically by the VM.
   * @param invokedName The name of the method to implement. When used with {@code invokedynamic},
   *     this is provided by the {@code NameAndType} of the {@code InvokeDynamic} structure and is
   *     stacked automatically by the VM.
   * @param invokedType The expected signature of the {@code CallSite}. The parameter types
   *     represent the types of capture variables; the return type is the interface to implement.
   *     When used with {@code invokedynamic}, this is provided by the {@code NameAndType} of the
   *     {@code InvokeDynamic} structure and is stacked automatically by the VM. In the event that
   *     the implementation method is an instance method and this signature has any parameters, the
   *     first parameter in the invocation signature must correspond to the receiver.
   * @param args An {@code Object[]} array containing the required arguments {@code samMethodType},
   *     {@code implMethod}, {@code instantiatedMethodType}, {@code flags}, and any optional
   *     arguments, as described {@link #altMetafactory(MethodHandles.Lookup, String, MethodType,
   *     Object...)} above}
   * @return a CallSite whose target can be used to perform capture, generating instances of the
   *     interface named by {@code invokedType}
   * @throws LambdaConversionException If any of the linkage invariants described {@link
   *     LambdaMetafactory above} are violated
   */
  public static CallSite altMetafactory(
      MethodHandles.Lookup caller, String invokedName, MethodType invokedType, Object... args)
      throws LambdaConversionException {
    MethodType samMethodType = (MethodType) args[0];
    MethodHandle implMethod = (MethodHandle) args[1];
    MethodType instantiatedMethodType = (MethodType) args[2];
    int flags = (Integer) args[3];
    Class<?>[] markerInterfaces;
    MethodType[] bridges;
    int argIndex = 4;
    if ((flags & FLAG_MARKERS) != 0) {
      int markerCount = (Integer) args[argIndex++];
      markerInterfaces = new Class<?>[markerCount];
      System.arraycopy(args, argIndex, markerInterfaces, 0, markerCount);
      argIndex += markerCount;
    } else markerInterfaces = EMPTY_CLASS_ARRAY;
    if ((flags & FLAG_BRIDGES) != 0) {
      int bridgeCount = (Integer) args[argIndex++];
      bridges = new MethodType[bridgeCount];
      System.arraycopy(args, argIndex, bridges, 0, bridgeCount);
      argIndex += bridgeCount;
    } else bridges = EMPTY_MT_ARRAY;

    boolean isSerializable = ((flags & FLAG_SERIALIZABLE) != 0);
    if (isSerializable) {
      boolean foundSerializableSupertype =
          Serializable.class.isAssignableFrom(invokedType.returnType());
      for (Class<?> c : markerInterfaces)
        foundSerializableSupertype |= Serializable.class.isAssignableFrom(c);
      if (!foundSerializableSupertype) {
        markerInterfaces = Arrays.copyOf(markerInterfaces, markerInterfaces.length + 1);
        markerInterfaces[markerInterfaces.length - 1] = Serializable.class;
      }
    }

    AbstractValidatingLambdaMetafactory mf =
        new InnerClassLambdaMetafactory(
            caller,
            invokedType,
            invokedName,
            samMethodType,
            implMethod,
            instantiatedMethodType,
            isSerializable,
            markerInterfaces,
            bridges);
    mf.validateMetafactoryArgs();
    return mf.buildCallSite();
  }
Beispiel #5
0
 byte[] getData() {
   return Arrays.copyOf(data, data.length);
 }
Beispiel #6
0
  /**
   * Constructs a MessageToken from an InputStream. Bytes will be read on demand and the thread
   * might block if there are not enough bytes to complete the token. Please note there is no
   * accurate way to find out the size of a token, but we try our best to make sure there is enough
   * bytes to construct one.
   *
   * @param tokenId the token id that should be contained in this token as it is read.
   * @param context the Kerberos context associated with this token
   * @param is the InputStream from which to read
   * @param prop the MessageProp structure in which the properties of the token should be stored.
   * @throws GSSException if there is a problem reading from the InputStream or parsing the token
   */
  MessageToken_v2(int tokenId, Krb5Context context, InputStream is, MessageProp prop)
      throws GSSException {
    init(tokenId, context);

    try {
      if (!confState) {
        prop.setPrivacy(false);
      }
      tokenHeader = new MessageTokenHeader(is, prop, tokenId);

      // set key_usage
      if (tokenId == Krb5Token.WRAP_ID_v2) {
        key_usage = (!initiator ? KG_USAGE_INITIATOR_SEAL : KG_USAGE_ACCEPTOR_SEAL);
      } else if (tokenId == Krb5Token.MIC_ID_v2) {
        key_usage = (!initiator ? KG_USAGE_INITIATOR_SIGN : KG_USAGE_ACCEPTOR_SIGN);
      }

      int minSize = 0; // minimal size for token data
      if (tokenId == Krb5Token.WRAP_ID_v2 && prop.getPrivacy()) {
        minSize = CONFOUNDER_SIZE + TOKEN_HEADER_SIZE + cipherHelper.getChecksumLength();
      } else {
        minSize = cipherHelper.getChecksumLength();
      }

      // Read token data
      if (tokenId == Krb5Token.MIC_ID_v2) {
        // The only case we can precisely predict the token data length
        tokenDataLen = minSize;
        tokenData = new byte[minSize];
        readFully(is, tokenData);
      } else {
        tokenDataLen = is.available();
        if (tokenDataLen >= minSize) { // read in one shot
          tokenData = new byte[tokenDataLen];
          readFully(is, tokenData);
        } else {
          byte[] tmp = new byte[minSize];
          readFully(is, tmp);
          // Hope while blocked in the read above, more data would
          // come and is.available() below contains the whole token.
          int more = is.available();
          tokenDataLen = minSize + more;
          tokenData = Arrays.copyOf(tmp, tokenDataLen);
          readFully(is, tokenData, minSize, more);
        }
      }

      if (tokenId == Krb5Token.WRAP_ID_v2) {
        rotate();
      }

      if (tokenId == Krb5Token.MIC_ID_v2
          || (tokenId == Krb5Token.WRAP_ID_v2 && !prop.getPrivacy())) {
        // Read checksum
        int chkLen = cipherHelper.getChecksumLength();
        checksum = new byte[chkLen];
        System.arraycopy(tokenData, tokenDataLen - chkLen, checksum, 0, chkLen);

        // validate EC for Wrap tokens without confidentiality
        if (tokenId == Krb5Token.WRAP_ID_v2 && !prop.getPrivacy()) {
          if (chkLen != ec) {
            throw new GSSException(
                GSSException.DEFECTIVE_TOKEN, -1, getTokenName(tokenId) + ":" + "EC incorrect!");
          }
        }
      }
    } catch (IOException e) {
      throw new GSSException(
          GSSException.DEFECTIVE_TOKEN, -1, getTokenName(tokenId) + ":" + e.getMessage());
    }
  }
 /**
  * Retrieves the update count for each update statement in the batch update that executed
  * successfully before this exception occurred. A driver that implements batch updates may or may
  * not continue to process the remaining commands in a batch when one of the commands fails to
  * execute properly. If the driver continues processing commands, the array returned by this
  * method will have as many elements as there are commands in the batch; otherwise, it will
  * contain an update count for each command that executed successfully before the <code>
  * BatchUpdateException</code> was thrown.
  *
  * <p>This method should be used when {@code Statement.executeLargeBatch} is invoked and the
  * returned update count may exceed {@link Integer#MAX_VALUE}.
  *
  * <p>
  *
  * @return an array of <code>long</code> containing the update counts for the updates that were
  *     executed successfully before this error occurred. Or, if the driver continues to process
  *     commands after an error, one of the following for every command in the batch:
  *     <OL>
  *       <LI>an update count
  *       <LI><code>Statement.SUCCESS_NO_INFO</code> to indicate that the command executed
  *           successfully but the number of rows affected is unknown
  *       <LI><code>Statement.EXECUTE_FAILED</code> to indicate that the command failed to execute
  *           successfully
  *     </OL>
  *
  * @since 1.8
  */
 public long[] getLargeUpdateCounts() {
   return (longUpdateCounts == null)
       ? null
       : Arrays.copyOf(longUpdateCounts, longUpdateCounts.length);
 }
 /**
  * Retrieves the update count for each update statement in the batch update that executed
  * successfully before this exception occurred. A driver that implements batch updates may or may
  * not continue to process the remaining commands in a batch when one of the commands fails to
  * execute properly. If the driver continues processing commands, the array returned by this
  * method will have as many elements as there are commands in the batch; otherwise, it will
  * contain an update count for each command that executed successfully before the <code>
  * BatchUpdateException</code> was thrown.
  *
  * <p>The possible return values for this method were modified for the Java 2 SDK, Standard
  * Edition, version 1.3. This was done to accommodate the new option of continuing to process
  * commands in a batch update after a <code>BatchUpdateException</code> object has been thrown.
  *
  * @return an array of <code>int</code> containing the update counts for the updates that were
  *     executed successfully before this error occurred. Or, if the driver continues to process
  *     commands after an error, one of the following for every command in the batch:
  *     <OL>
  *       <LI>an update count
  *       <LI><code>Statement.SUCCESS_NO_INFO</code> to indicate that the command executed
  *           successfully but the number of rows affected is unknown
  *       <LI><code>Statement.EXECUTE_FAILED</code> to indicate that the command failed to execute
  *           successfully
  *     </OL>
  *
  * @since 1.3
  * @see #getLargeUpdateCounts()
  */
 public int[] getUpdateCounts() {
   return (updateCounts == null) ? null : Arrays.copyOf(updateCounts, updateCounts.length);
 }
Beispiel #9
0
 private static Object[] copyAsReferenceArray(Class<? extends Object[]> arrayType, Object... a) {
   return Arrays.copyOf(a, a.length, arrayType);
 }
Beispiel #10
0
 private static Object[] fillNewTypedArray(
     Object[] example, Integer len, Object[] /*not ...*/ args) {
   Object[] a = Arrays.copyOf(example, len);
   fillWithArguments(a, 0, args);
   return a;
 }