All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperledger.composer.bna.model.FunctionModel Maven / Gradle / Ivy

The newest version!
/*
 * Copyright IBM Corp. 2017 All Rights Reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package org.hyperledger.composer.bna.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class FunctionModel {

  private final Method method;
  private Object thisObj;

  /**
   * constructor.
   *
   * @param method the {@code Method} object
   */
  public FunctionModel(Method method) {
    this.method = method;
    this.method.setAccessible(true);
    if ((method.getModifiers() & Modifier.STATIC) == 0) {
      try {
        Constructor constructor = method.getDeclaringClass().getDeclaredConstructor();
        constructor.setAccessible(true);
        this.thisObj = constructor.newInstance();
      } catch (Exception e) {
        throw new RuntimeException("declaring class for non-static transaction process method:"
            + method.getName() + " should have a default constructor", e);
      }
    }
  }

  /**
   * invoke the method holding by this {@code FunctionModel}.
   *
   * @param args arguments
   * @return result
   * @throws Throwable when error occurs
   */
  public Object invoke(Object... args) throws Throwable {
    try {
      return method.invoke(thisObj, args);
    } catch (InvocationTargetException e) {
      throw e.getTargetException();
    }
  }

  @Override
  public String toString() {
    return method.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy