コード例 #1
0
ファイル: Reflections.java プロジェクト: CODE4US/rewrite
  /**
   * Invoke the method on the instance, with any arguments specified, casting the result of invoking
   * the method to the expected return type.
   *
   * <p>
   *
   * <p>This method wraps {@link Method#invoke(Object, Object...)}, converting the checked
   * exceptions that {@link Method#invoke(Object, Object...)} specifies to runtime exceptions.
   *
   * <p>
   *
   * <p>If instructed, this method attempts to set the accessible flag of the method in a {@link
   * PrivilegedAction} before invoking the method.
   *
   * @param setAccessible flag indicating whether method should first be set as accessible
   * @param method the method to invoke
   * @param instance the instance to invoke the method
   * @param args the arguments to the method
   * @return the result of invoking the method, or null if the method's return type is void
   * @throws RuntimeException if this <code>Method</code> object enforces Java language access
   *     control and the underlying method is inaccessible or if the underlying method throws an
   *     exception or if the initialization provoked by this method fails.
   * @throws IllegalArgumentException if the method is an instance method and the specified <code>
   *     instance</code> argument is not an instance of the class or interface declaring the
   *     underlying method (or of a subclass or implementor thereof); if the number of actual and
   *     formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if,
   *     after possible unwrapping, a parameter value cannot be converted to the corresponding
   *     formal parameter type by a method invocation conversion.
   * @throws NullPointerException if the specified <code>instance</code> is null and the method is
   *     an instance method.
   * @throws ClassCastException if the result of invoking the method cannot be cast to the
   *     expectedReturnType
   * @throws ExceptionInInitializerError if the initialization provoked by this method fails.
   * @see Method#invoke(Object, Object...)
   */
  public static <T> T invokeMethod(
      boolean setAccessible,
      Method method,
      Class<T> expectedReturnType,
      Object instance,
      Object... args) {
    if (setAccessible && !method.isAccessible()) {
      setAccessible(method);
    }

    try {
      return expectedReturnType.cast(method.invoke(instance, args));
    } catch (IllegalAccessException ex) {
      throw new RuntimeException(buildInvokeMethodErrorMessage(method, instance, args), ex);
    } catch (IllegalArgumentException ex) {
      throw new IllegalArgumentException(buildInvokeMethodErrorMessage(method, instance, args), ex);
    } catch (InvocationTargetException ex) {
      throw new RuntimeException(
          buildInvokeMethodErrorMessage(method, instance, args), ex.getCause());
    } catch (NullPointerException ex) {
      NullPointerException ex2 =
          new NullPointerException(buildInvokeMethodErrorMessage(method, instance, args));
      ex2.initCause(ex.getCause());
      throw ex2;
    } catch (ExceptionInInitializerError e) {
      ExceptionInInitializerError e2 =
          new ExceptionInInitializerError(buildInvokeMethodErrorMessage(method, instance, args));
      e2.initCause(e.getCause());
      throw e2;
    }
  }
コード例 #2
0
ファイル: Reflections.java プロジェクト: CODE4US/rewrite
 /**
  * Get the value of the field, on the specified instance, casting the value of the field to the
  * expected type.
  *
  * <p>
  *
  * <p>This method wraps {@link Field#get(Object)}, converting the checked exceptions that {@link
  * Field#get(Object)} specifies to runtime exceptions.
  *
  * @param <T> the type of the field's value
  * @param field the field to operate on
  * @param instance the instance from which to retrieve the value
  * @param expectedType the expected type of the field's value
  * @return the value of the field
  * @throws RuntimeException if the underlying field is inaccessible.
  * @throws IllegalArgumentException if the specified <code>instance</code> is not an instance of
  *     the class or interface declaring the underlying field (or a subclass or implementor
  *     thereof).
  * @throws NullPointerException if the specified <code>instance</code> is null and the field is an
  *     instance field.
  * @throws ExceptionInInitializerError if the initialization provoked by this method fails.
  */
 public static <T> T getFieldValue(Field field, Object instance, Class<T> expectedType) {
   try {
     return Reflections.cast(field.get(instance));
   } catch (IllegalAccessException e) {
     throw new RuntimeException(buildGetFieldValueErrorMessage(field, instance), e);
   } catch (NullPointerException ex) {
     NullPointerException ex2 =
         new NullPointerException(buildGetFieldValueErrorMessage(field, instance));
     ex2.initCause(ex.getCause());
     throw ex2;
   } catch (ExceptionInInitializerError e) {
     ExceptionInInitializerError e2 =
         new ExceptionInInitializerError(buildGetFieldValueErrorMessage(field, instance));
     e2.initCause(e.getCause());
     throw e2;
   }
 }
コード例 #3
0
ファイル: Reflections.java プロジェクト: CODE4US/rewrite
  /**
   * Sets the value of a field on the instance to the specified value.
   *
   * <p>
   *
   * <p>This method wraps {@link Field#set(Object, Object)}, converting the checked exceptions that
   * {@link Field#set(Object, Object)} specifies to runtime exceptions.
   *
   * <p>
   *
   * <p>If instructed, this method attempts to set the accessible flag of the method in a {@link
   * PrivilegedAction} before invoking the method.
   *
   * @param field the field on which to operate, or null if the field is static
   * @param instance the instance on which the field value should be set upon
   * @param value the value to set the field to
   * @throws RuntimeException if the underlying field is inaccessible.
   * @throws IllegalArgumentException if the specified <code>instance</code> is not an instance of
   *     the class or interface declaring the underlying field (or a subclass or implementor
   *     thereof), or if an unwrapping conversion fails.
   * @throws NullPointerException if the specified <code>instance</code> is null and the field is an
   *     instance field.
   * @throws ExceptionInInitializerError if the initialization provoked by this method fails.
   * @see Field#set(Object, Object)
   */
  public static void setFieldValue(
      boolean setAccessible, Field field, Object instance, Object value) {
    if (setAccessible && !field.isAccessible()) {
      setAccessible(field);
    }

    try {
      field.set(instance, value);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(buildSetFieldValueErrorMessage(field, instance, value), e);
    } catch (NullPointerException ex) {
      NullPointerException ex2 =
          new NullPointerException(buildSetFieldValueErrorMessage(field, instance, value));
      ex2.initCause(ex.getCause());
      throw ex2;
    } catch (ExceptionInInitializerError e) {
      ExceptionInInitializerError e2 =
          new ExceptionInInitializerError(buildSetFieldValueErrorMessage(field, instance, value));
      e2.initCause(e.getCause());
      throw e2;
    }
  }