Beispiel #1
0
  /**
   * Loads configuration. It verifies the constraints defined in {@link WireByMethod}.
   *
   * @param prefix the configuration prefix for this class
   */
  public WireByMethod(String prefix) {
    super(prefix);

    // get the method
    try {
      final Class wire =
          Configuration.getClass(
              prefix + "." + PAR_CLASS, Class.forName("peersim.graph.GraphFactory"));
      method =
          WireByMethod.getMethod(wire, Configuration.getString(prefix + "." + PAR_METHOD, "wire"));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    // set the constant args (those other than 0th)
    Class[] argt = method.getParameterTypes();
    args = new Object[argt.length];
    for (int i = 1; i < args.length; ++i) {

      if (argt[i] == int.class) args[i] = Configuration.getInt(prefix + "." + PAR_ARG + i);
      else if (argt[i] == long.class) args[i] = Configuration.getLong(prefix + "." + PAR_ARG + i);
      else if (argt[i] == double.class)
        args[i] = Configuration.getDouble(prefix + "." + PAR_ARG + i);
      else if (i == args.length - 1 && argt[i].isInstance(CommonState.r)) args[i] = CommonState.r;
      else {
        // we should neve get here
        throw new RuntimeException(
            "Unexpected error, please " + "report this problem to the peersim team");
      }
    }
  }
Beispiel #2
0
  /**
   * Search a wiring method in the specified class.
   *
   * @param cl the class where to find the method
   * @param methodName the method to be searched
   * @return the requested method, if it fully conforms to the definition of the wiring methods.
   */
  private static Method getMethod(Class cl, String methodName)
      throws NoSuchMethodException, ClassNotFoundException {

    // Search methods
    Method[] methods = cl.getMethods();
    ArrayList<Method> list = new ArrayList<Method>();
    for (Method m : methods) {
      if (m.getName().equals(methodName)) {
        list.add(m);
      }
    }

    if (list.size() == 0) {
      throw new NoSuchMethodException(
          "No method " + methodName + " in class " + cl.getSimpleName());
    } else if (list.size() > 1) {
      throw new NoSuchMethodException(
          "Multiple methods called " + methodName + " in class " + cl.getSimpleName());
    }

    // Found a single method with the right name; check if
    // it is a setter.
    final Class graphClass = Class.forName("peersim.graph.Graph");
    final Class randomClass = Class.forName("java.util.Random");
    Method method = list.get(0);
    Class[] pars = method.getParameterTypes();
    if (pars.length < 1 || !pars[0].isAssignableFrom(graphClass))
      throw new NoSuchMethodException(
          method.getName()
              + " of class "
              + cl.getSimpleName()
              + " is not a valid graph wiring method,"
              + " it has to have peersim.graph.Graph as first argument type");
    for (int i = 1; i < pars.length; ++i)
      if (!(pars[i] == int.class
          || pars[i] == long.class
          || pars[i] == double.class
          || (i == pars.length - 1 && pars[i].isAssignableFrom(randomClass))))
        throw new NoSuchMethodException(
            method.getName()
                + " of class "
                + cl.getSimpleName()
                + " is not a valid graph wiring method");

    if (method.toString().indexOf("static") < 0)
      throw new NoSuchMethodException(
          method.getName()
              + " of class "
              + cl.getSimpleName()
              + " is not a valid graph wiring method; it is not static");

    return method;
  }